Author Topic: Question about Float rounding  (Read 327 times)

Legacy_SKIPPNUTTZ

  • Full Member
  • ***
  • Posts: 148
  • Karma: +0/-0
Question about Float rounding
« on: December 06, 2011, 04:39:42 am »


               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?
               
               

               
            

Legacy_Shadooow

  • Hero Member
  • *****
  • Posts: 7698
  • Karma: +0/-0
Question about Float rounding
« Reply #1 on: December 07, 2011, 05:36:18 pm »


               Use SpeakString(FloatToString(fFloat)) and you will see - it will print exact value.
               
               

               
            

Legacy_Xardex

  • Sr. Member
  • ****
  • Posts: 414
  • Karma: +0/-0
Question about Float rounding
« Reply #2 on: December 07, 2011, 05:50:04 pm »


               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.
               
               

               


                     Modifié par Xardex, 07 décembre 2011 - 05:53 .
                     
                  


            

Legacy_wyldhunt1

  • Sr. Member
  • ****
  • Posts: 443
  • Karma: +0/-0
Question about Float rounding
« Reply #3 on: December 07, 2011, 10:18:05 pm »


               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.
               
               

               


                     Modifié par wyldhunt1, 07 décembre 2011 - 11:03 .