Home » Java » why 0.0f/0.0f doesn't generate any runtime error?

why 0.0f/0.0f doesn't generate any runtime error?

Posted by: admin December 28, 2021 Leave a comment

Questions:

Recently i tested following program & i was expecting runtime error but it shows “nan” as output. Why & How? I am using g++ compiler.

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

Same way i also tried similar type of program in Java & C# & it shows again “nan” as output.

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 that how floating point arithmetic is implemented & how it differes from integer arithmetic. Is it undefined behaviour in case of C++? If yes, why?
Please help me.

Answers:

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

It would be less reasonable to do this with floating point numbers because floating point calculations are not exact. For example, a calculation could result in 0.0f because 0.0f is the closest floating point value to the answer, not because the true value of the answer is mathematically 0. Therefore trying to divide by the floating point value 0.0f might not be a consequence of an incorrect algorithm. nan essentially means that the answer to your calculation could not be determined because it involved the division of two numbers so small it was impossible for the computer to distinguish them from 0.

###

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

Operations generating NaN

There are three kinds of operations that can return NaN:

  • Operations with a NaN as at least one operand.
  • Indeterminate forms
    • The divisions 0/0 and ±∞/±∞
    • The multiplications 0×±∞ and ±∞×0
    • The additions ∞ + (−∞), (−∞) + ∞ and equivalent subtractions

… etc.

###

It is because that by default, the C++ run-time libraries mask all floating-point exceptions. Therefore, computations result in NAN or INFINITY, rather than an exception. If you want exceptions, you can use function _controlfp_s on Windows to unmask the floating-point. exceptions

The following code raise the exception. Compiled and Run 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;
}

###

you must know that division on zero is allowed with floats so this:

1.0/0.0

is equal to infinity and this:

1.0/-0.0

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

0.0/0.0

is NAN which means not a number, this is exactly undetermined in mathematics abbreviation.
that’s with floats but ints will throw a runtime error, you may ask why, it’s because of how 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