playMaker

Author Topic: Float subtraction leads to weird number?  (Read 3954 times)

ryf9059

  • Full Member
  • ***
  • Posts: 100
Float subtraction leads to weird number?
« 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.



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?

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: Float subtraction leads to weird number?
« Reply #1 on: September 09, 2013, 03:03:00 PM »
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.
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

ryf9059

  • Full Member
  • ***
  • Posts: 100
Re: Float subtraction leads to weird number?
« Reply #2 on: September 09, 2013, 04:20:55 PM »
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.

« Last Edit: September 09, 2013, 04:23:48 PM by ryf9059 »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Float subtraction leads to weird number?
« Reply #3 on: September 10, 2013, 01:54:33 AM »
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

ryf9059

  • Full Member
  • ***
  • Posts: 100
Re: Float subtraction leads to weird number?
« Reply #4 on: September 10, 2013, 02:21:55 AM »
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

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Float subtraction leads to weird number?
« Reply #5 on: September 10, 2013, 02:27:34 AM »
Hi,
 
Yes, there is a "float round" action.

 Bye,

 Jean