- Home /
Getting the same 'rounded' value of a Vector3?
When I write a Vector3 value to the console, the values will be rounded to the nearest decimal (e.g., (0.7, 0.1, 0.0)) but in reality, if I access just that single x value, it's actually something like 0.666879345.
Is the console just multiplying the actual vector by ten, rounding to int, then dividing by ten? Otherwise, I'm curious if there some way to access that already rounded value myself...
Answer by Eric5h5 · Feb 24, 2012 at 09:36 AM
I expect Vector3 just has a function like this:
public override string ToString () {
return "(" + x.ToString("f1") + ", " + y.ToString("f1") + ", " + z.ToString("f1") + ")";
}
That's all, no magic or mystery or already-rounded numbers, sorry. ;)
but please don't use the created string to get the rounded float values back ;)
Well, I guess technically that would work though, in a roundabout sort of way. ;)
Answer by ZweiD · Feb 24, 2012 at 09:03 AM
that is a general problem in computing (see Wikipedia on Floating Point Values). the computer cannot 'really' display a 0.7, instead he saves that the number as something like 0.666879345.
Therefore I don't think what you want is possible and a problem of the represantation of floating values in general.
you can also see this effect in the unity editor when editing the position of an object, closing unity and reopening it. often the value of the position vector of the object will have changed slightly.
if you really need to test against the first decimal place, the best thing to do would probably be to multiply by ten, round and cast the result to int (also save it as an int, otherwise you have the same problem that you had).
if you only compare these values to each other, you don't actually have a problem because they were all multiplied by 10.
Interesting link… Although I think I might have explained poorly what I was asking. I don't want the full float, I just want to use the same number being displayed in the console.
var figure = Vector3(0, 0.599999, 0);
Debug.Log(figure);
would show as 0.0, 0.6, 0.0 in the console.
But if I try…
if (figure.y == 0.6)
Debug.Log("hello!");
...It won't work. :)
I guess it's not possible… Was just curious though. Thanks.
like I (and you started) said: you can do:
if (((int) figure.y * 10) == 6) Debug.Log("i'm at 0.6");
There are no issues using .7 in floating point. If a number can't be represented exactly by floating point, it will be off by a very small amount, like .0000001. The issue here isn't floating point or inaccuracies thereof, it's just a matter of the Vector3 ToString function deliberately rounding numbers so that printing a Vector3 doesn't result in long strings of precision that you (probably) don't really care about. (In those cases where you do, it's mildly annoying, but can be bypassed by printing the individual elements. If you need to do that a lot, it's worth making a small function that prints the elements "as-is" and doesn't do any rounding.)
Answer by senad · Feb 24, 2012 at 09:28 AM
You could use an epsilon value to check if your float is inside a given range. For example:
float espilon = .05f;
if ( Math.abs(figure.y - .7f) < epsilon ) figure.y = .7f;
be aware that this will not work reliably, though it will probably cover about 99% of all cases ...
(meaning: it is ok to do it in unity for a small project but not for anything bigger or for a system that has to be highly reliable)
I do not understand, why is it not reliable? It will always cover the range of all float values in the range of ].65f, .75f[ in this case. (not including the numbers .65 and .75 themselves.
this is reliable for the simple case that is shown here, but it fails for example if you are multiplying the value beforehand. because then the discrepancy between the real number and the rounded one gets bigger.
As i understand the question he is looking for a way to round the float without converting it to int prior.
So I do not think your point is valid as this is a valid method for the rounding. Perfor$$anonymous$$g calculations with floating point numbers is a different issue. :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613064427im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Vector3.ToString 3 Answers
Rounding a position against the surface of an object. 1 Answer
How precise is unity with it´s transforms? 2 Answers