- Home /
float precision, wrong computations
when I run following code:
float v = 100.0f - 99.2f;
I get v = 0,8000031 instead of 0.8.
how to increase float precision?
That's the inevitable imprecise nature of floating-point arithmetic. If you say what you want the value v to represent, we might be able to suggest alternatives - does that 0.000031 error really matter?
Answer by Azrapse · Nov 06, 2013 at 01:05 PM
That is what the double
type is for.
Although it will use more memory, and also it will keep on having small imprecisions, although much smaller ones. (For example, 0.800000000000000000000000000023).
Also, check this link: link text
What are you trying to accomplish? If you're trying to compare a floating point result to something, it's rare that you can do it by comparing for equality. Either use @Azrapse's suggestion about using $$anonymous$$athf.Approximately, or use your own to deal with small (nearly always meaningless) differences, e.g. ins$$anonymous$$d of comparing for equality with 0, do something like this...
float epsilon = 0.0001f;
if (value <= epsilon)
{
// consider the value to be 0 and do something
}
Also, here is a great discussion on this issue and the very valid reasons why it is this way: http://floating-point-gui.de/
Your answer
Follow this Question
Related Questions
float precision, again 0 Answers
How Precise is Random.Range? 1 Answer
Question on scale of imported 3D models 1 Answer
Vector3 constructor precision 1 Answer
Convert double to float for Vector3 1 Answer