Why Does This Subtraction Not Equal Zero

Why does this subtraction not equal zero?

This is because decimal numbers that "look" round in base 10, are not exactly representable in base 2 (which is what computers use to represent floating point numbers). Please see the article What Every Computer Scientist Should Know About Floating-Point Arithmetic for a detailed explanation of this problem and workarounds.

Why does subtracting '0' in C result in the number that the char is representing?

Because the char are all represented by a number and '0' is the first of them all.

On the table below you see that:

'0' => 48
'1' => 49

'9' => 57.

As a result: ('9' - '0') = (57 − 48) = 9

Sample Image
Source: http://www.asciitable.com

Is it possible to get 0 by subtracting two unequal floating point numbers?

In Java, a - b is never equal to 0 if a != b. This is because Java mandates IEEE 754 floating point operations which support denormalized numbers. From the spec:

In particular, the Java programming language requires support of IEEE 754 denormalized floating-point numbers and gradual underflow, which make it easier to prove desirable properties of particular numerical algorithms. Floating-point operations do not "flush to zero" if the calculated result is a denormalized number.

If an FPU works with denormalized numbers, subtracting unequal numbers can never produce zero (unlike multiplication), also see this question.

For other languages, it depends. In C or C++, for example, IEEE 754 support is optional.

That said, it is possible for the expression 2 / (a - b) to overflow, for example with a = 5e-308 and b = 4e-308.

How can i make a subtraction if one cell in sheet is not equal to 0

Excel offers the possibility in a formula only to do something "IF" a condition is met, using the IF() formula. More information can be found under this URL.

You can make a formula, like this (in cell C1):

=IF(A1 <> 0; B1-A1;B1)

Which means: in case A1 is not zero, subtract it from B1 and put the result in C1, otherwise put B1 value in C1.



Related Topics



Leave a reply



Submit