I'm not normally one to be confused by number formats, but I found this unexpected: >> a = 5i; ar = real(a); >> b = -5i; br = real(b); Now I would guess that ar and br are identical, and Matlab seems to think so as well: >> ar == br ans = 1 However it seems that they are not quite identical! >> sprintf('%e %e %g %g',ar,br,ar,br) ans = 0.000000e+000 -0.000000e+000 0 -0 How does sprintf detect a difference between ar and br? ~Paul

0 |

12/17/2016 8:12:04 PM

https://en.wikipedia.org/wiki/IEEE_floating_point With IEEE coding can have identical 0 for mantisa 0 for exponent, but sign can bo 0 or 1. It gives codding of two signed zeros >> typecast(0,'uint8') ans = 1×8 uint8 row vector 0 0 0 0 0 0 0 0 >> typecast(-0,'uint8') ans = 1×8 uint8 row vector 0 0 0 0 0 0 0 128 >> sprintf('%e %e',0,-0) ans = 0.000000e+00 -0.000000e+00 >>

0 |

12/17/2016 9:49:03 PM

"Bruno Luong" wrote in > With IEEE coding can have identical 0 for mantisa 0 for exponent, but sign can bo 0 or 1. > It gives codding of two signed zeros Actually I was aware of that, but the deeper question is why Matlab uses it. What is the reason for using a different representation for zero for the real parts of 5i and -5i? ~Paul

0 |

12/17/2016 10:36:04 PM

"Paul Mennen" wrote in message <o34ekk$q1u$1@newscl01ah.mathworks.com>... > > Actually I was aware of that, but the deeper question is why Matlab uses it. The reverse the question : why MATLAB shouldn't use it. It's IEEE standard. The subject line seems to indicate you were surprise by two kind of signed 0s, not the way to generate them via complex numbers. > > What is the reason for using a different representation for zero for the real parts of 5i and -5i? > The reason is the perhaps parser, when you type "b=-5i", MATLAB parse as -(0 + 5i) Meaning it puts +0 in real part, 5.0 in imaginary, then reverse the sign of both in B. Then when you type "br=real(b)", it deletes the imaginary part then br has -0. That's possibly how the parser codded. much ado about nothing Bruno

0 |

12/17/2016 10:58:07 PM

Just to have a record how sign bit 0 of the real part is set depending on the way to enter : >> typecast(real(complex(0,-5)), 'uint8') ans = 1×8 uint8 row vector 0 0 0 0 0 0 0 0 >> typecast(real(complex(-0,-5)), 'uint8') ans = 1×8 uint8 row vector 0 0 0 0 0 0 0 128 >> typecast(real(0-5i), 'uint8') ans = 1×8 uint8 row vector 0 0 0 0 0 0 0 0 >> typecast(real(-5i), 'uint8') ans = 1×8 uint8 row vector 0 0 0 0 0 0 0 128 >>

0 |

12/17/2016 11:17:04 PM