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 ?
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.
Since multiplying by zero always gives zero, we really cannot divide anything non-zero 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.
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.
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)
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With