Why does float division not work?
If i do something like this
double xd = 1.5f / .3f;
it shows xd as not equaling 5, and instead equaling something like 4.999999
Why is this?
Answer by JJMGsoftworks · Jan 20, 2017 at 04:33 PM
Doubles and floats are not always stored as expected in memory. They are also not always precise for equations. Take a look at this answer on a stack overflow Post Here Hellium Has Commented this link above already. I believe Decimal is a more accurate type to go for but it depends on what you need. Check this out if you need More Help. If you have trouble in the future MSDN or a google search will always have the information on how data types work.
Decimal gives more precision if you're calculating, say, interest rates in a business application. But it's very uncommon in games program$$anonymous$$g - ints and floats should be sufficient for pretty much everything.
Agreed I have never used Decimal in a game. I just provided an alternative if completely necessary. The main thing is the the $$anonymous$$SDN page because it covers the data types in more detail.
The realization that certain decimals that are finite in decimal but infinite in binary sealed the deal.
Answer by Amon · Jan 20, 2017 at 10:15 AM
I think it is due to "floating point precision error" in the number of digits after the decimal point. When your code is executed on different types of hardware it could yield a different result to the one desired. Some hardware can only process 6 digits after the decimal and others 4 or 7 etc.
Partly true. I recommend reading for example the link @hellium posted above, or even skim$$anonymous$$g through the wikipedia article on floats. It's not just the number of decimals, it's also how numbers of different magnitude are stored and that some numbers simply can't be exactly presented in the format floating point numbers use... Or decimal format in general...
10 / 3 = 3.33333...
No matter how many "3"s you put after the decimal point, multiplying by 3 will never give you 10 as a result.
But at no point is the code snippet I presented dealing with anything larger than one decimal point. And the answer is an integer. I should be no where near the limits of the float data structure.
Answer by elenzil · Jan 20, 2017 at 08:59 PM
lots of good discussion about the "Why" of what's going on here.
here's some additional practical advice:
unless you fully understand the article Hellium linked to (and very few programmers do), you should never use == to compare two floats for equality. they will almost always be unequal.
instead, you should test for approximate equality. you can do this by either writing your own utility function, or using Mathf.Approximately()
.
Here's an example.
void floatTest() {
float a = 1.5f;
float b = 0.3f;
float c = 5.0f;
float d = 5.00001f;
Debug.Log("(1.5f / 0.3f) == " + c + ": " + ((a / b) == c));
Debug.Log("Mathf.Approximately(1.5f / 0.3f, 5.0f): " + Mathf.Approximately(a / b, c));
Debug.Log("Mathf.Approximately(1.5f / 0.3f, 5.0001f): " + Mathf.Approximately(a / b, d));
/*
ouput:
(1.5f / 0.3f) == 5: False
Mathf.Approximately(1.5f / 0.3f, 5.0f): True
Mathf.Approximately(1.5f / 0.3f, 5.0001f): False
*/
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612105210im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
how to calculate the angle between two vectors? 6 Answers
Adjusting viewing angle without distortion. 0 Answers
Issues wrapping my brain around this math 2 Answers
I am showing double on screen and it disappear 1 Answer
How to divide big float 2 Answers