- Home /
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
just so i follow this.
its not 30- 30
its a float thats approx thirty but is in fact not thirty forced into a int which makes it 30
but then you take that 30 and subtract this complex float
there is no reason to believe that float is 30. I see no debug that says it is. I see a debug that says when you force convert it to an int it becomes 30 but i don't see a debug log that shows the value of that float that your subtracting 30 from
if that float is 31.907349e-06 than your number makes perfect sense
I see no unanticipated behavior here
just weird force a float to be an int and then subtract it from itself what on earth are you trying to do?
@sparkzbarca, 31.907349e-06 is not 30.000... , it is 0.0000319...
it's this simple ... the OP does not realise that floats, on computers, are, well, floats... they are not exact.
sdgd, it's that simple O$$anonymous$$? Quite simply, floats on computers are NOT exact. (Of course not because they are ... floats! heh)
Normally computer code is peppered with 100s of lines of code to deal with this. Simple examples are you would rarely say "if equals zero" but rather "if less than or equal to zero", and you constantly have to use $$anonymous$$athf.Approximately, and you very frequently round numbers or floor numbers, in computer code.
Its just an absolutely normal, central part of computing.
Floats ... real numbers if you will .., are not "exact", they are floats.
Answer by Chronos-L · Mar 19, 2013 at 08:02 AM
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
Answer by xt-xylophone · Mar 19, 2013 at 08:03 AM
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 ==
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Dumb this down for me? 1 Answer
How do you make a password script in c#? 0 Answers
Acces C# via Unityscript 2 Answers