playMaker

Author Topic: [!Solved!] Adding/subtracting to a float variable.  (Read 1564 times)

krysber

  • Playmaker Newbie
  • *
  • Posts: 12
[!Solved!] Adding/subtracting to a float variable.
« on: December 10, 2015, 11:31:15 PM »
I have a loop that repeats thirty two times subtracting .01 from a float variable. The problem is that when the variable is 2.72, I end up with 2.080001 after deleting .01 thirty two times. This happens at different values where I end up with odd numbers like -4.649992 and such. Is there a better way to do this math?
« Last Edit: December 11, 2015, 03:03:00 PM by krysber »

Gida

  • Playmaker Newbie
  • *
  • Posts: 48
Re: Adding/subtracting to a float variable.
« Reply #1 on: December 11, 2015, 02:41:42 AM »
Sound like a rounding issue to me. Have you tried doin it with bigger numbers? Instead of subtracting constantly 0.01 you could multiply your floats with 100 before the math operations. if required, divide by 100 at the end.

krysber

  • Playmaker Newbie
  • *
  • Posts: 12
Re: Adding/subtracting to a float variable.
« Reply #2 on: December 11, 2015, 03:02:33 PM »
I tried with the same effect. I solved it by using integers to do the math then converting to float.