For my own personal game development, I made a math algorithm that rounds vector lookvector XYZ values to either -1, 0, or 1… (Open / Close Door Logic)
Here is my current code:
(ignore the Y value because it’s going to be an ignored variable)
I might be misreading your post, but if you want to round to the nearest of those three numbers you can just use math.floor and clamp the value from that.
math.clamp(math.floor(x + 0.5), -1, 1)
This will round to the nearest number, and if it’s above 1 will round down to 1, while if it’s below -1 it will round up to -1. The 0.5 is because math.floor always rounds down, and to make 0.5-0.99999 round up you can just add 0.5 to make that range 1-1.499999 (which will go down to 1).
If I’m not mistaken, math.sign(x)*math.ciel(math.abs(x)) for each component should do it. That’ll round the result to 1 or 0 depending upon what it is, and then multiply it by the sign of the original number (which will be -1, 0, or -1).
His solution is great and more readable, so if you want readability then you should definitely use his method. If both of ours work the same for your purposes, mine seems to be slightly more efficient. After running both of our methods, I got 1,000,000 repeats of his taking 1.036175 seconds while mine took 0.883054 seconds.