Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 995
Chrissy Avatar asked Dec 19 '22 11:12

Chrissy


2 Answers

(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.

  • Integers are susceptible to overflow.
  • Adding a very small floating-point number m to a very large number n returns n. Have a read of What Every Computer Scientist Should Know About Floating-Point Arithmetic.
like image 157
NPE Avatar answered Jan 11 '23 23:01

NPE


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".

like image 37
unwind Avatar answered Jan 11 '23 22:01

unwind