Why does 0.0f / 0.0f not generate any runtime error?

I recently tested the following program, and I was expecting a runtime error, but it showed " nan " as output. Why and how? I am using the g ++ compiler.

#include <iostream> int main() { float f=0.0f/0.0f; std::cout<<f; return 0; } 

I also tried the same type of program in Java and C #, and it shows "nan" again.

 class Test { public static void main(String args[]) { float f=0.0f/0.0f; System.out.println(f); } } class Test { public static void Main(string[] args) { float f=0.0f/0.0f; Console.Write(f); } } 

I want to know how floating point arithmetic is implemented and how it differs from integer arithmetic. Is this behavior undefined in case of C ++? If so, why? Please help me.

+6
source share
4 answers

nan means not a number. Dividing by the integer 0 is always a mistake, because you cannot divide by 0 in math. Therefore, many languages ​​throw an exception when you divide by an integer 0.

It would be wiser to do this with floating point numbers, because floating point calculations are not accurate. For example, a calculation may result in 0.0f because 0.0f is the closest floating point value to the answer, and not because the true value of the answer is mathematically 0. Therefore, trying to divide by a floating point 0.0f may not be the result of an incorrect algorithm. nan essentially means that the answer to your calculation cannot be determined because it involves dividing two numbers so small that the computer could not distinguish them from 0.

+9
source

From http://en.wikipedia.org/wiki/NaN#Operations_generating_NaN :

NaN generating operations

There are three kinds of operations that can return NaN:

  • Operations with NaN with at least one operand.
  • Undefined forms
    • Sections 0/0 and Β± ∞ / Β± ∞
    • Multiplications 0 Γ— Β± ∞ and Β± ∞ Γ— 0
    • Additions ∞ + (-∞), (-∞) + ∞ and equivalent subtractions

... etc.

+2
source

This is because, by default, C ++ runtime libraries mask all floating point exceptions. Therefore, calculations result in NAN or INFINITY rather than an exception. If you want exceptions, you can use the _ controlfp_s function on Windows to expose the floating point. Exceptions

The following code throws an exception. Compiled and launched with VS2010, Win32 configuration.

 #include <float.h> int main() { _controlfp_s(NULL, 0, _MCW_EM); // enable all floating point exceptions float n = 0; float f= 0.0f/n; return 0; } 
+2
source

you should know that division by zero is allowed with float, so:

 1.0/0.0 

equal to infinity, and this:

 1.0/-0.0 

equal to negative infinity (-0.0 negative zero, like 1 / 0- in mathematics), and this:

 0.0/0.0 

- this is NAN, which means not a number, it is not exactly defined in the abbreviation of mathematics. that with float, but ints will throw a runtime error, you may ask why, due to the way floats are represented in binary form, you can read about it here:
http://floating-point-gui.de/formats/fp/
in C # you can say Double.PositiveInfinity, Double.NegativeInfinity and Double.NaN

0
source

Source: https://habr.com/ru/post/980132/


All Articles