I just noticed this difference between vectors and numbers when multiplying them with a fraction of 1. I guess it has something to do with the way they’re rounded? I use a vector for xy velocity and a single number for zoom speed and I want to slow both down to 0 over time, however the zoom number doesn’t settle down, while the velocity vector does (as in the example below).
I guess this relates to another question, how can I round a number to any number of decimals? Often I would like to round something to only two decimal places.
function setup()
num = 0
vec = vec2(0,0)
parameter.watch("num")
parameter.watch("vec")
end
function draw()
num = num * .9 -- fluctuates
vec = vec * .9 -- settles
end
function touched(t)
num = 10
vec = vec2(10,10)
end
Well, to slowly dampen a number you could just set it to 0 after it gets small enough, as it would make no difference having a speed of 10^-10. I don’t know about your other question.
@Kirl An easier way to round a number to any number of decimal places is to use string.format. This example rounds to 2 decimal places. Just change the 2 in %.2f to the desired number of places. Rounding is automatic.
function setup()
a=123.4567890
r=string.format("%.2f",a)
print(r)
end
The numbers aren’t settling faster: the printout is settling faster, because of how vectors are printed compared with how numbers are printed.
if you add
diff = num - vec.x
to both your setup and your draw, and you watch “diff” you’ll see that diff is always zero. The effect is all in the printing. Another way to see that would be to set
@RonJeffries The number represented by the variable a is still the same because it wasn’t changed in any way. The number represented by the variable r is 123.46 . If you run the statement print(123.46 - r), the result is 0. If the variable a needs to be changed, then use a=string.format("%.2f",a) .
@dave1707 I bet that the pure mathematical approach to rounding numbers that @Ignatz posted is faster if the output you need is a float (as @Kirl originally asked), rather than round-tripping to a string than back to a float.
@yojimbo2000 It’s about 8 times faster if that’s all you’re doing. But when run in normal code it won’t make that much difference. It’s just that I would use string.format because I would probably be doing other formatting of the number too.
Thanks for the great alternative aproach dave! =)
From looking at it, I guessed this would simply cut off the remaining numbers as it looks like a string operation, but acording to your example it seems it does round the number somehow.
From looking at it, I guessed this would simply cut off the remaining numbers as it looks like a string operation, but acording to your example it seems it does round the number somehow.
It uses Lua’s implicit type conversions to convert from float to string and then back to float again. If you are wanting to round a number for the purpose of printing to the screen, use this approach. However, if you are wanting to round a number for some other reason then it is much more efficient to use math.floor as Ignatz suggested.