Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Division z / (x/n) when n is 0

Tags:

c++

I have an arithmetic expression, for example:

float z = 8.0
float x = 3.0;
float n = 0;
cout << z / (x/n) + 1 << endl;

Why I get normal answer equal to 1, when it should be "nan", "1.#inf", etc.?

like image 721
user2849212 Avatar asked Mar 23 '23 03:03

user2849212


2 Answers

I assume you're using floating point arithmetic (though one can't be sure, because you're not telling us).

IEEE754 floating point semantics work on the extended real line and include infinities on both ends. This makes divisions with non-zero numerator well-defined for any (non-NaN) denominator, "consistent with" (i.e. extending continuously) the usual arithmetic rules: x / n is infinity, and z divided by infinity is zero — just as if you had simplified the expression as n * z / x.

The only genuinely undefined quantities are 0/0 and inf/inf, which are represented by the special value NaN.

like image 164
Kerrek SB Avatar answered Mar 29 '23 23:03

Kerrek SB


The IEEE 754 specifies that 3/0 = Inf (or anything positive instead of 3). 8/Inf gives 0. If you add 1 you'll receive 1. This is because 0 denotes "0 or something very close to it" and Inf "Infinity or very big number". It also allows to perform some operations on limits as it effectively extends the real numbers into by infinities. NaN's are reserved when the limit is not achievable (or not easily computable by simple implementation).

As a side effect you have some strange effects like 0 == -0 but 1/0 == Inf and 1/-0 == -Inf. It is important to remember that FP arithmetic is not normal - for example cos(x) * cos(x) + sin(x) * sin(x) - 1 != 0 even if x != NaN && x != Inf && x != -Inf. For floats and x == 1 the result is -5.9604645e-8. Therefore not all expectation can be easily transferred to it - like division by 0 in this case.

While C/C++ does not mandate that IEE 754 specification will be used for floating point numbers it is the specification right now and is implemented on virtually any hardware and for that reason used by most C/C++ implementations.

like image 34
Maciej Piechotka Avatar answered Mar 29 '23 22:03

Maciej Piechotka