Le 20/01/12 23:32, James Kuyper a écrit :
Computer arithmetic has nothing to do with maths. For instance:
A+B is different from A for all B different than zero.
That's mathematically correct, but not what happens in a computer,
since, if A is of type unsigned int, then A + UINT_MAX + 1 is actually
equal to A.
A+B is equal to A if A is a floating point number and B is smaller than
the floating point epsilon for that type.
Not quite. A+B is equal to A if A is a floating point number and abs(B)
is smaller than the floating point epsilon for that type _times_A_. Or
even more accurate: scaled by the exponent of A.
Or A + B is different than B + A.
Erm... no, that actually can't happen. There was a discussion in the
German C newsgroup about this a year ago or so, which resulted in
finding that it follows from the C standard that the addition is indeed
commutative, but not associative. That is
a + b + c == (a + b) + c == c + (b + a) !=* c + b + a
* = i.e. not always equal. There may be instances of a, b and c where
this holds, but it doesn't have to.
And I could go on for a while.
If you have a signed number s and an unsigned number u, and you want to
know if s is smaller than u, the expression for that is
(s < 0 || s < u)
because if s < 0 the comparison s < u might produce unexpected results.
(Because if s == -1, then you are actually comparing UINT_MAX to u.)
Mathematics is concerned about numbers as abstract entities that have
abstract properties that can't be reproduced in a finite machine with
a finite and time limited operator.
Actually much of it can. Sure, real numbers are a PITA. Like, all my
approximations for the sine can only produce algebraical numbers while
most values of the sine are actually transcendental. But the computer
doesn't care since it cannot accurately save all algebraical or
transcendental numbers.
But the definition of the C comparison operators defies much mathematics
by staying simple: The "usual arithmetic conversions" take place before
applying _any_ operator, so why not on the comparison operators?
Those are the basics. Now, some systems like Maple or Maxima approach
MORE the maths we are used to, and should be used when you do maths.
Or perhaps you should think about what you really want from your
computer before writing a program? Just killing the problem by throwing
a library at it without thinking isn't really good style.
C is a computer language that doesn't shield you from the concrete
machine you are using. It is the lowest "high level" language in
existence, always just a bit above assembler.
That argument is so overused: C is actually pretty far removed from the
machine it runs on. I mean, every CPU I know has a flags register, and
if I want to know, whether that addition that just executed overflowed,
I can check it. In assembly. Not in C. In C, an overflow is IIRC
undefined behaviour and may even result in an interrupt. Even if it does
not, it is a PITA to find out whether the addition overflowed if you
didn't save the values beforehand. Not to talk about multiplications!
And really not to talk about complex expressions that may still be
critical. AFAIK I'd have to hand-compile them into three-operand-form
and do an overflow check between each step!
To expect to find here "sane" mathematical laws shows only a certain
ignorance from Mr Press, specially if he uses unsigned numbers, and
then he mixes them with negative values...
Yeah, that's true.
Unsigned numbers correspond to the set 0-UINT_MAX, and can't be used
with negative values. That should be obvious too but apparently
Mr Press doesn't know that.
Perhaps he's just sulking that his compiler should have warned him or
something...
If he is disappointed with C I would
recommend him to use Maxima, a free mathematical system available
for all, written in Lisp.
If he's disappointed with C I suggest him using Python or any other
language out there that does a better job of hiding the computer and
emulating the mathematics machine.
Because that's the trouble with C: It actually does exactly what the
programmer tells it to, no more, no less; whereas languages like Python
and C++ do so much stuff behind the back of the programmer he doesn't
need to care about (they say). It's another philosophy.
Ciao,
Markus