rounding numbers to the nearest tick
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
- Posts: 349
- Joined: Tue Oct 28, 2008 1:23 am
- Contact:
rounding numbers to the nearest tick
Hello: I need a fast way of rounding a given number to it's nearest "tick" value. (the tick values change).
example:
number=100.19
tick = .25
in this case, given the tick value of .25, the only valid numbers between 100 and 101 are 100, 100.25, 100.5, 100.75 and 101
and in this case, 100.19 would round to 100.25
Thanks,
Greg K
example:
number=100.19
tick = .25
in this case, given the tick value of .25, the only valid numbers between 100 and 101 are 100, 100.25, 100.5, 100.75 and 101
and in this case, 100.19 would round to 100.25
Thanks,
Greg K
-
- VIP Livecode Opensource Backer
- Posts: 3901
- Joined: Sat Mar 24, 2007 2:54 am
- Location: Palo Alto
Re: rounding numbers to the nearest tick
Hi Greg,
Great problem!
What should happen if
number=100.125
tick = .25
Simon
Great problem!
What should happen if
number=100.125
tick = .25
Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!
-
- VIP Livecode Opensource Backer
- Posts: 9669
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: rounding numbers to the nearest tick
Hi,
I would extract the decimal portion, and run a mod 25 and a div 25. So if you had XXX.66, 66 mod 25 would give you "16" and 66 div 25 would give you 2. If the mod is less than or equal to 12, you choose the div result, if greater, add 1 to the div result. Then just multiply the div result by 25 and reinsert back into the original number.
You will need to play around, I think , with values above XX.88 and below XXX.12. What I mean is,do test all over the range from 0-100.
Craig Newman
I would extract the decimal portion, and run a mod 25 and a div 25. So if you had XXX.66, 66 mod 25 would give you "16" and 66 div 25 would give you 2. If the mod is less than or equal to 12, you choose the div result, if greater, add 1 to the div result. Then just multiply the div result by 25 and reinsert back into the original number.
You will need to play around, I think , with values above XX.88 and below XXX.12. What I mean is,do test all over the range from 0-100.
Craig Newman
-
- VIP Livecode Opensource Backer
- Posts: 2262
- Joined: Thu Feb 28, 2013 11:52 pm
- Location: Göttingen, DE
Re: rounding numbers to the nearest tick
..........
Last edited by [-hh] on Wed Aug 13, 2014 12:07 pm, edited 1 time in total.
shiftLock happens
-
- VIP Livecode Opensource Backer
- Posts: 9669
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: rounding numbers to the nearest tick
Our resident mathematician is spot on, even though he is speaking in absolute terms.
That is two, Hermann.
But I would ask that Greg write his own handler to figure this out, and use your function after he has done that. Learning should be fun, but also arduous, or there is no triumph. Simon and I keep our silver platters close by.
Craig
That is two, Hermann.
But I would ask that Greg write his own handler to figure this out, and use your function after he has done that. Learning should be fun, but also arduous, or there is no triumph. Simon and I keep our silver platters close by.
Craig
-
- VIP Livecode Opensource Backer
- Posts: 2262
- Joined: Thu Feb 28, 2013 11:52 pm
- Location: Göttingen, DE
Re: rounding numbers to the nearest tick
..........
Last edited by [-hh] on Wed Aug 13, 2014 12:07 pm, edited 3 times in total.
shiftLock happens
-
- VIP Livecode Opensource Backer
- Posts: 9669
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: rounding numbers to the nearest tick
Hermann.
Can't think of anything cute right now to forward my quest to learn about the second "h".
I agree with you about learning from reading existing code. But really new users need really simple code, or they just will not see what is going on. Your stuff is quite a bit beyond that. Anyway, it is just a matter of style, and I do like yours.
What does your "roundToEquallySpacedTicks" function do? The older one, which codifies my verbose hint, finds the nearest "0.25". What does this one find? I tried it with several arguments, and several values of "n", and it gives trunc(arg) no matter what.
Craig
Can't think of anything cute right now to forward my quest to learn about the second "h".
I agree with you about learning from reading existing code. But really new users need really simple code, or they just will not see what is going on. Your stuff is quite a bit beyond that. Anyway, it is just a matter of style, and I do like yours.
What does your "roundToEquallySpacedTicks" function do? The older one, which codifies my verbose hint, finds the nearest "0.25". What does this one find? I tried it with several arguments, and several values of "n", and it gives trunc(arg) no matter what.
Craig
-
- VIP Livecode Opensource Backer
- Posts: 2262
- Joined: Thu Feb 28, 2013 11:52 pm
- Location: Göttingen, DE
Re: rounding numbers to the nearest tick
..........
Last edited by [-hh] on Wed Aug 13, 2014 12:07 pm, edited 1 time in total.
shiftLock happens
-
- VIP Livecode Opensource Backer
- Posts: 9669
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: rounding numbers to the nearest tick
"Hermann" & "." & yetAnotherNewWrinkle & "." & "H"
Ah. I see.
Craig
Ah. I see.
Craig