Playmaker Forum
PlayMaker Help & Tips => PlayMaker Help => Topic started by: ryf9059 on September 09, 2013, 02:48:45 PM
-
I'm doing some calculation in my game and I found that float subtraction can lead to weird result. For example 0.2 - 0.2 = 2.980232E-08, that's way off the chart.
(http://i.imgur.com/A9HIhEu.png)
I doubt this might have something to do with the 32 bit float structure that leads to this error. But I haven't encounter this in any other programming languages even using float subtraction.
Is there something wrong? How to work around this?
-
Just tried the same thing and it works fine for me.
Are you specifying 0.2 for the float you're getting before you Get it? Just wondering how it was populated with 0.2 before the variable was filled. It may be that you're Getting a number that is different than you expect from the Meta FSM.
-
Just tried the same thing and it works fine for me.
Are you specifying 0.2 for the float you're getting before you Get it? Just wondering how it was populated with 0.2 before the variable was filled. It may be that you're Getting a number that is different than you expect from the Meta FSM.
I specify it after I get it.
This is the procedure I take.
Base number is 30, and float to subtract is 5, with scaler 0.8 on the subtract number.
So it will be 30 - 5 x 0.8 = 30 - 4 = 26
After that each time the scaler is decreased by 0.2 and the base number is unchanged. So I have the follower calculation
30 - 5 x 0.8 = 30 - 4 = 26
30 - 5 x 0.6 = 30 - 3 = 27
30 - 5 x 0.4 = 30 - 2 = 28
30 - 5 x 0.2 = 30 - 1 = 29
30 - 5 x 2.980232e-08 = 30 - 1.4901166e-07 = 30 (where problem happens)
30 - 5 x (-0.2) = 30 - (-0.9999999) = 31
30 - 5 x (-0.4) = 30 - (-2) = 32
As you can see the problem happens at 30 and 31. The final outcome is correct, but the byproduct is not. And it's because this peculiar outcome that makes me doubt that to be the problem of floating point itself. And that disturbs me: what if I were to use those by product in game? I surely don't want the number 2.980232e-08 to show up while it's really supposed to be 0.
-
Hi,
Don't worry, it's all fine.
because float operation under the hood is never exact, you always get approximation. but don't worry, this is very very tiny.
2.980232e-08 is infat :0.0000000298032 and for the computer it's like 0...
now, the problem is that Unity doesn't really process float values to make it look good, so you do get sometimes these weird numbers, but they really mean 0.
Bye,
Jean
-
Don't worry, it's all fine.
because float operation under the hood is never exact, you always get approximation. but don't worry, this is very very tiny.
2.980232e-08 is infat :0.0000000298032 and for the computer it's like 0...
now, the problem is that Unity doesn't really process float values to make it look good, so you do get sometimes these weird numbers, but they really mean 0.
So if I were to display then on a in game UI I do need to manually round them right? Is there a floor/cell action pre-made? Just wondering
-
Hi,
Yes, there is a "float round" action.
Bye,
Jean