I know integers round down to the nearest whole number, but could someone clarify for me how floats work. Im guessing if you arent converting to an int, then it will just keep the whole fraction value.
-----
float fFloat = 150/190;
how would that be returned?
Question about Float rounding
Débuté par
SKIPPNUTTZ
, déc. 06 2011 04:39
#1
Posté 06 décembre 2011 - 04:39
#2
Posté 07 décembre 2011 - 05:36
Use SpeakString(FloatToString(fFloat)) and you will see - it will print exact value.
#3
Posté 07 décembre 2011 - 05:50
Floating point numbers include decimals as they are.
Integers always round down, not to the nearest whole number.
If you convert a int to float, decimals will all be zeroes (.00...).
If you convert a float to int, decimals will be removed. (Rounded down)
Its good programming practice to avoid using floats (also double's, outside nwn) unless absolutely necessary (pi, square roots, etc). When decimals are in the play, there is a good chance that your calculations go off thanks to roundoff errors caused by binary madness.
Integers always round down, not to the nearest whole number.
If you convert a int to float, decimals will all be zeroes (.00...).
If you convert a float to int, decimals will be removed. (Rounded down)
Its good programming practice to avoid using floats (also double's, outside nwn) unless absolutely necessary (pi, square roots, etc). When decimals are in the play, there is a good chance that your calculations go off thanks to roundoff errors caused by binary madness.
Modifié par Xardex, 07 décembre 2011 - 05:53 .
#4
Posté 07 décembre 2011 - 10:18
Here is exactly how it works and why there can be minor accuracy issues.
It'll always be close, but it won't always be exact.
http://en.wikipedia....curacy_problems
EDIT:
And, here is how it gets interpreted as a float, concerning rounding and such:
http://en.wikipedia....ng-point_format
And, yes. NWN just drops the decimal when you convert to an int.
So, FloatToInt(13.9999f) will return 13.
It'll always be close, but it won't always be exact.
http://en.wikipedia....curacy_problems
EDIT:
And, here is how it gets interpreted as a float, concerning rounding and such:
http://en.wikipedia....ng-point_format
And, yes. NWN just drops the decimal when you convert to an int.
So, FloatToInt(13.9999f) will return 13.
Modifié par wyldhunt1, 07 décembre 2011 - 11:03 .





Retour en haut






