V
V
versidue2021-07-21 18:52:38
C++ / C#
versidue, 2021-07-21 18:52:38

Why does the 0.0 / 0.0 operation throw an error?

The book says that 0.0 / 0.0 returns the literal nan , but it gives me a "divide or mod by zero" error.
Same with the (-)n / 0.0 operations, which should have returned inf , but throws an error. What is the problem? Visual Studio 2019 Compiler.

std::cout << 0.0 / 0.0 << "\n\n";

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wataru, 2021-07-21
@versidue

What book says about nan? According to the C++ standard, division by zero is undefined behavior:

If the second operand is zero, the behavior is undefined,

True, there is one exception:
except that if floating-point division is taking place and the type supports IEEE floating-point arithmetic

But this IEEE 754 standard is not postulated by the C++ standard (because it depends on the hardware implementation of floating point numbers).
Some compilers with some level of optimization may actually produce nan. But not all and not always.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question