Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is floating point multiplication by zero guaranteed to produce zero?

I understand floating point has rounding errors, but I'm wondering if there are certain situations where the error does not apply, such as multiplication by zero .

Does zero times any number = zero for all floating points ?

like image 212
Thomas Avatar asked May 14 '15 16:05

Thomas


People also ask

What happens if you times by 0?

The rule is that anything multiplied by 0 is equal to 0. Remember this rule, and all multiplication by 0 problems will become instantly easier. If you see 0 in your multiplication problem, then your answer is 0.

Is multiplication by zero possible?

Since multiplying by zero always gives zero, we really cannot divide anything non-zero by zero.

What is special about multiplying by zero?

One of zero's unique rules is called the multiplication property. The multiplication property states that the product of any number and zero is zero. It doesn't matter what the number is, when you multiply it to zero, you get zero as the answer.

What is a floating point multiplication?

A floating point multiplication between two numbers and can be expressed as. Thus it can be said that in a floating point multiplication, mantissas are multiplied and exponents are added. The major steps for a floating point division are. Extract the sign of the result from the two sign bits.


2 Answers

False:

0f * NAN == NAN
0f * INFINITY == NAN

and ...

0f * -1f == -0f (negative 0f), with 0f == -0f :-)

(on Intel, VC++, and probably on any platform that uses IEEE 754-1985 floating points)

Example on ideone (that uses GCC on some Intel compatible platform probably)

like image 59
xanatos Avatar answered Oct 31 '22 21:10

xanatos


In addition to @xanatos fine answer, consider some of OP's middle-of-the-post concerns:

I'm wondering if there are certain situations where the (rounding) error does not apply

Candidates include some_double_y = some_double_x * 1.0 and some_double_y = some_double_x + 0.0 may never incur a rounding error.

Yet even those are suspect due to a compiler may evaluate double at higher precision considering the FLT_EVAL_METHOD == 2 where "evaluate all operations and constants to the range and precision of the long double type." In that case, an intermediate some_double_x may exist as a long double differing from an apparent double value of 0.0 or 1.0.

like image 31
chux - Reinstate Monica Avatar answered Oct 31 '22 21:10

chux - Reinstate Monica