So I am trying to get rid of a 0.0000001 incorrectness on my float from the expression
cldsDown _= 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 * 1000 > Mathf.Round(cldsDown * 1000)
{
cldsDown -= 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
1.) How to make float precise
2.) how to see what my floats are exactly.
ps. I tried to google this, but have no clue what to call it.
2 Answers
2
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.
As stated by other answers:
- 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.
"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.
– tanoshimiThere'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.
– Eric5h5