Number vs vector rounding?

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

Are you pointing this out as something interesting, or because you need a solution?

I would like a solution for how to slowly dampen a number to 0, like with a vector. Also I added another question while you posted your response;

How can I round a fractional number to a desired nr of decimal places?

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 - the vector settles quicker because it’s rounded to many fewer places

To round a number to 2 places, you can use this

x = math.floor(x*100+0.5)/100

@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

disp = “(” … tostring(vec.x) … ", " … tostring(vec.y) … “)”

and watch that. Again, you’ll see that your vector isn’t really zero, it is just not displaying more than six places.

Note also that in @dave1707 's example the printout is rounded but the number is still the same as it ever was.

@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) .

sorry, my bad, misread the thing. you’re right, i’m wrong.

@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.

I will experiment with it, thanks guys!

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.

@Kirl Do a google search for string.format. It does a lot more than just round numbers.