Horde3D
http://horde3d.org/forums/

Why my code returns -0 instead of 0 ?
http://horde3d.org/forums/viewtopic.php?f=8&t=519
Page 1 of 1

Author:  Siavash [ 03.10.2008, 04:02 ]
Post subject:  Why my code returns -0 instead of 0 ?

I don't know you know or not, but currently I'm busy with optimizing utMath.Recently I've checked the SSE optimized utMath [utMath_rc3] for problems and comparing the returned results from original utMath with utMath_rc3.But sometimes [not too much] utMath_rc3 returns -0 instead of 0.This happend a few times on Matrix4f class : Inverted() .

I've tested the code with MinGW/GCC 3.4.5 compiler and there isn't any -0 , only MSVC [2005] returns -0 !

Do you have any idea about why MSVC compiled code generates -0 ?

[I'm using winxp sp2 + PentiumIII 750MHz + 256MB RAM SD]

Author:  swiftcoder [ 03.10.2008, 16:12 ]
Post subject:  Re: Why my code returns -0 instead of 0 ?

-0 is the same as 0, it is just an artefact of the binary representation of numbers. Some standard library implementations print -0 as 0, and some don't.

Author:  Siavash [ 03.10.2008, 16:20 ]
Post subject:  Re: Why my code returns -0 instead of 0 ?

I fear that this will be problematic in future and generate runtime errors because they are float types :|

Author:  swiftcoder [ 03.10.2008, 18:52 ]
Post subject:  Re: Why my code returns -0 instead of 0 ?

Siavash wrote:
I fear that this will be problematic in future and generate runtime errors because they are float types :|
Why would it cause any problems? They are exactly the same number, just with a slightly different representation in bits. For a float you have some number of bits containing the value, and an additional bit for the sign. Flipping the sign bit negates the number, so flipping the sign bit on 0 gives you -0.

You can check for yourself that the sign on zero doesn't matter for any basic operations:

n + 0.0 = n and n - 0.0 = n
n * 0.0 = 0.0 and n * (-0.0) = -0.0
n / 0.0 = INF and n/(-0.0) = INF
0.0 == -0.0 => True

Remember, -0 does not exist - it is merely an artefact of the binary representation of floating point numbers, and affects nothing except the print function. If you look at the IEEE spec for floating point, you will also see that there can be multiple representations for INF and NAN.

Author:  marciano [ 03.10.2008, 19:02 ]
Post subject:  Re: Why my code returns -0 instead of 0 ?

From Wikipedia:

Quote:
−0 is the representation of negative zero or minus zero, a number that, in computing, exists in some signed number representations for integers, and in most floating point number representations. In mathematical terms there is no concept of a negative (or positive) zero, and −0 is equal to, and represented as, zero.

In common usage, −0 may be used to denote a negative number rounded to zero, or a number approaching zero from the negative direction. In statistical mechanics, certain systems in a state of population inversion may be considered to have an absolute temperature of −0, the hottest possible absolute temperature (see negative temperature).

Author:  Siavash [ 03.10.2008, 20:34 ]
Post subject:  Re: Why my code returns -0 instead of 0 ?

Thank you very much dear marciano and swiftcoder :idea:

Author:  kal [ 04.10.2008, 09:51 ]
Post subject:  Re: Why my code returns -0 instead of 0 ?

I am more concerned about matrix multiplication and floats :?

Author:  DDd [ 08.10.2008, 23:43 ]
Post subject:  Re: Why my code returns -0 instead of 0 ?

If i may be so bold as to recommend you read Essential Mathematics for Games (2nd edition) first chapter deals with numerical representations (i think this chapter is available for free, but i am not certain). It tackles this topic and others that you may find interesting.

Author:  Siavash [ 09.10.2008, 16:10 ]
Post subject:  Re: Why my code returns -0 instead of 0 ?

Thanks a lot dear DDd :wink:

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/