Why fprintf('%3.1f',round(-eps)) prints -0.0?
7 views (last 30 days)
Show older comments
Sandor Toth on 17 Jan 2017
Commented: James Tursa on 17 Jan 2017
I found some very strange behaviour of the round function in Matlab R2016b (at least for me). I would expect that the round function returns 0 for -eps, but surprisingly the command:
num2hex(0) == 0000000000000000
This causes fprintf() to print -0.0. Is this a bug or I just miss something?
James Tursa on 17 Jan 2017
Side Note: This seems to be consistent among the related functions ceil and fix as well. E.g.,
Stephen23 on 17 Jan 2017
Edited: Stephen23 on 17 Jan 2017
You are missing the fact that MATLAB has two zeros: positive zero and negative zero, which have different HEX values. This is a result of the IEEE 754 floating point standard that MATLAB implements, which uses one bit to indicate the number sign: a corollary of this is that even a zero can be positive or negative.
It is not possible to use < to directly test if zero is negative (by definition negative zero is equal to positive zero, so this makes sense):
but here is one simple trick that will test for negative zero:
Which relies on this behavior:
>> 1/round(-eps) % your question
You can find more interesting information on negative zeros here:
More Answers (1)
Adam on 17 Jan 2017
Edited: Adam on 17 Jan 2017
Code Generation Fixed-Point Designer Fixed-Point and Floating-Point Basics Fixed-Point Concepts Quantization Precision
Find more on Precision in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!