- Home /
why is my float slightly off, and how do I see it?
So I am trying to get rid of a 0.0000001 incorrectness on my float from the expression
cldsDown[i] = Mathf.Round(HitDown.distance * 1000) * 0.001f; // ray dist rounded
it isn't always off, but annoying when it is. I tried to check to see if it is less then or equal to itself times a thousand rounded,
if (cldsDown[i] * 1000 > Mathf.Round(cldsDown[i] * 1000)
{
cldsDown[i] -= 0.0000001
}
But this is true even if my print I do of the two numbers are exactly the same! does print() not show precise Precision?
Summery of question
How to make float precise
how to see what my floats are exactly.
ps. I tried to google this, but have no clue what to call it.
"How to make float precise". You can't. By their very nature, floating point numbers are inherently imprecise. But it doesn't matter - you never need to, and never should try to, compare equality of two floating point numbers.
But if I am using a raycast to find a distance to a wall, and then move my character that distance, but then it says there is a 0.000000001 gap, but then won't move my character that distance to close the gap to say that it is actually colliding against a wall. So there ends up being a gap that never gets closed, so my character never collides with it!
There's no reason for this. A value being off by .00000001 will make no difference whatsoever in any sane situation. If your design is somehow dependent on infinitesimal differences like this, you really need to change your design.
Answer by Jessespike · Jul 14, 2016 at 11:45 PM
That's just the way floats work, as explained by tanoshimi. If you need more precision, you can use a double, it still won't be exact though. Use Mathf.Approximately if you want to compare floating points.
But these are binary values, that represent a specific decimal value, I understand division and a few other mathematical functions will always be off because we are working on a base 2, just like you can't divide 1 perfectly bye 3, but it still can be saved as 0.33333332 or 0.33333334 which then can be viewed and adjusted by hand. What I am saying is even though the math might not turn out correct one still can view it perfectly and edit it perfectly right? So if floats only go down to like 7 decimal places why can't I see all 7 decimal places, and then edit the very last one, and compare two together??
Edit Take this for an example. I just multyplied my float buy 100k, and then cast it to int. As a float printed it said it was 121.00000001 as and int it was 12,100,000,000,003, so clearly the unity is out putting it to print rounded up.
I think the print or ToString function only shows up to 7 decimal places by default, even though floats can contain more than 7 decimal places, you usually don't need to know more than that. The string format can be overridden to show more digits if you want.
Floats aren't simple binary value as you may think, quoting wiki: "floating point is the formulaic representation that approximates a real number so as to support a trade-off between range and precision. A number is, in general, represented approximately to a fixed number of significant digits (the significand) and scaled using an exponent in some fixed base".
No, you can't edit it precisely because that's not how floats are stored. They can store a very large range of values, but at the cost of precision. Why does it matter that your character is 0.000000001 units away from the wall?
Well how ever this might be annoying, I still think you for your answer :).
Answer by rmassanet · Jul 15, 2016 at 09:43 AM
As stated by other answers:
1) You can't make floats more precise.
However: 2) You can print floats with more digits using the string.Format
function, which lets you control the number of decimal places, 0 padding, and things like that.
Regarding your problem of the 0.000000001
gap causing your character not colliding with a wall, why is that important? Your character is 0.000000001
away from the wall, so what? Whatever logic you are doing when the character collides with the wall, you could trigger it when you are forcing the character to move to the wall, right?
I hope this helps.
Your answer
Follow this Question
Related Questions
Is there a setting to change integer rounding to 'Swedish Rounding'? 2 Answers
Rounding Off A Float? 2 Answers
Rounding to the nearest int as a Result of a Function? 1 Answer
Double Round Problems 1 Answer