wired calculation I don't get it 30-30 = 1.907349E-06

IF I use this:

float TempPCF = CreateFloor.PerCreateFake*100f;
float TempPSF = CreateFloor.PerStopFake*100f;
int TempPCFi = (int)TempPCF;
int TempPSFi = (int)TempPSF;
Debug.Log(""+TempPCFi);
Debug.Log(""+TempPSFi);
Debug.Log(""+TempPCF + " " + TempPSF);

I get resault

50
30
50 30

I like this resault

BUT arghhh with this:

float TempPCF = CreateFloor.PerCreateFake*100f;
float TempPSF = CreateFloor.PerStopFake*100f;
int TempPCFi = (int)TempPCF;
int TempPSFi = (int)TempPSF;
	TempPCF -= TempPCFi;
	TempPSF -= TempPSFi;
Debug.Log(""+TempPCFi);
Debug.Log(""+TempPSFi);
Debug.Log(""+TempPCF + " " + TempPSF);

I get resault:

50
30
0 1.907349E-06

ARGHHH HOW DA **** can he calculate 30-30 = 1.907349E-06

and I don’t belive 2 static have any kind of problems here as I’ve assign 2 temporary variables just for them

@sparkzbarca, 31.907349e-06 is not 30.000... , it is 0.0000319...

yea my bad i got the answer write though. :P

2 Answers

2

1.907349E-06 is equal to 1.907349 x 10 ^ -6, which is equivalent to 0.000001907349 and very close to 0. It is caused by floating point imprecision. Unity even has a Mathf.Approximately to deal with checking the equality between 2 float.

hey where did 1 answer go I have puted both thumbs up as they were both pretty much same and thanks I thought it's 1.9 not 0.000019 it makes me breath more easily as now I can check if it's 0.1 bigger not 0

I think sometimes floats that say they are whole numbers are actually really close and not exact. It’s kinda like a rounding error. In your case it’s an error of only 0.000001907349

Casting won’t help with this. Maybe try a rounding function before you do the maths?

Or if on the future you want to check if 2 floats are equal, try checking if the difference is less than a reeeaally small number instead of ==

Mathf.Approximately exists for that purpose.

^ awesome, didn't know that one!