- Home /
The question is answered, right answer was accepted
Are float values reliable?
Hey :) I want to use the coordinates of an object to read values in an array. Like:
var vPosition : Vector3 = transform.position;
var value = myArray[vPosition.x, vPosition.y, vPosition.z];
the positions are set by script and always able to be a int. I noticed that the float values sometimes goes like 0.99999999 or stuff. So do I have to set up an int Array or can I use the positions of the objects?
right, that's normal on computers
check out $$anonymous$$athf.Approximately
so I have to use RoundToInt()... but than an extra int array for positions would work faster, right?
you could use RoundToInt, or you could use an extra array
there is no performance concern either way
i personally would go with an additional "conceptual" array, yeah - for sure
if you essentially want them to be on a grid, use a "conceptual" integer array, express that in a data structure, or whatever - for sure
If you are talking about having two arrays operate in parallel, I would advise against.
If ints suit your need, use it. If not, it is very normal to use floats and deal with their inaccuracies. Just do not rely on testing if float == float. Either use $$anonymous$$athf.Approximately() like @Fattie suggested or ask if float1 - float2 <= 0.01 or whatever is considered equal to you.
Follow this Question
Related Questions
Display a float number when button is clicked 4 Answers
int conversion gives bad results ! 3 Answers
Convert positive float to negative? 5 Answers
How do i divide int by float? 1 Answer
Int vs. Float operations 4 Answers