Chrissy
Chrissy

Reputation: 65

Do the rules of arithmetic hold for addition in C?

I'm new to C, and I'm having such a hard time understanding this material. I really need help! Please someone help.

In arithmetic, the sum of any two positive integers is great than either:

(n+m) > n for n, m > 0
(n+m) > m for n, m > 0

C has an addition operator +. Does this arithmetic rule hold in C?

I know this is False. But can please someone explain to me why so, I can understand it? Please provide counter-example?

Thank you in advance.

Upvotes: 4

Views: 243

Answers (3)

Jubatian
Jubatian

Reputation: 2191

I brief search did not show up nice answers describing these, so I rather attempt to answer this nicely here, for beginners.

The answer is false, of course, but why so?

Integers

In C, or any programming language providing some kind of integer type, this type does not mean it in the mathematical sense. In mathematical sense non-negative integers range from 0 to infinity. A computer, however has only limited storage, so integers necessarily are constrained to something less than infinity.

This alone proves that a + b > a and a + b > b can not be true all the time, since it can be set up so both a and b is less than the largest number the computer can represent in it's storage, but a + b is larger than that.

What exactly happens here, depends. Some mentioned wraparound, but that's not necessarily the case. The C language the first place defines integer overflow to be an undefined behaviour, that whatever, including fire and smoke, may happen if the code happens to step on it (of course in the reality that won't happen, but interpreting the standard strictly it could, as well as the breach of the space-time continuum).

I won't describe how wraparound works here since it is beyond the scope of the problem itself.

Floating point

The case here is again just the same like for integers: the key to understand why mathematics don't fully apply here is that the computer has a limited storage.

Floating numbers in the computers memory are represented much like scientific notation: a mantissa, and an exponent. Both of these have a fixed limited range depending on the type of the floating point variable.

In base 10, you may conceive this like you have the exponent ranging from 10 ^ -10 to 10 ^ 10, and the mantissa having like 4 fraction digits after the decimal point, always normalized.

With this in mind, check these example additions:

1.2345 * (10 ^ 0) + 1.0237 * (10 ^ 5)
5.2345 * (10 ^ 10) + 6.7891 * (10 ^ 10)

The first is an example where the result will equal one of the input numbers while both were larger than zero. The second is an example where the result is out of range.

The floating point representation computers use however is capable to represent infinity, and two at that: positive infinity and negative infinity. So while the first example passes as a proof, the second does not, since that addition's result is positive infinity.

However with this in mind, you could produce an another proofing example:

3.1416 * (10 ^ 0) + (+ infinity)

Of course the result is positive infinity, no matter what you add it to. And of course positive infinity is not larger than positive infinity, so proved again.

Upvotes: 0

unwind
unwind

Reputation: 400109

It doesn't hold, since C's integers are not "abstract" infinititely-sized integers that the real integers (in mathematics) are.

In C, integers are discrete and digital, and implemented using a fixed number of bits. This leads to limited range, and problems when you go (try to) out of range. Typically integers will wrap, which is very "un-natural".

Upvotes: 4

NPE
NPE

Reputation: 500893

(I won't solve this for you, but will provide some pointers.)

It is false for both integer and floating-point arithmetic, for different reasons.

Upvotes: 7

Related Questions