rounding numbers
DestroyerX
Posts: 34
Is there a command in PBASIC 2.5 that will round a given number to an integer?
Thanks,
Jason
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Unreal tournament 2004 rox
Thanks,
Jason
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Unreal tournament 2004 rox
Comments
Thanks again,
Jason
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Unreal tournament 2004 rox
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
--==<{Chris}>==--
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Unreal tournament 2004 rox
Dave
I was asking about the rounding to integers before, because the duty for PWM supposedly can't be an integer.
Thanks,
Jason
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Unreal tournament 2004 rox
25*54=1350
1350/41=32 NOT (~32.93)
It is 25 * (51/41)
I meant to originally say 51 instead of 41 and also have (51/41) in parenthesis.
-jason
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Unreal tournament 2004 rox
Post Edited (DestroyerX) : 2/21/2005 9:39:03 PM GMT
http://www.pond.ie/pdf/BStamp.pdf
read pages 231 (PDF doc page 39) to 234 (PDF doc page 42)
sorry Parallax people, I searched BS2 manual in google and that was the 1st link·to come up. Its not from Parallax 's website but its the same manual.
Remember: "google is your friend" [noparse]:)[/noparse]
·· You can do that math in the BS2, but the result will be an integer.· It will be 25, I believe.· So you don't have to worry about the decimal places...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
--==<{Chris}>==--
25 * (51/41)
would be 25*51=1275
1275/41=31
"The BS2 solves math problems in the order they are written-from left to right. The results of each operation is fed into the next operation" Pg233
I dont think parantneses make a difference.
Thanks for all of your help everyone.
Jason
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Unreal tournament 2004 rox
25 * (51/41)
25 * (1)
=25
x VAR Byte
y VAR Word
y=x*51/41
if (x*51)//41<20 THEN NOROUNDUP
y=y+1
NOROUNDUP:
'next actions go here
Hope that helps,
Dave
Why bother "testing" and then still doing two divides (the modulo op usually does
one)? Well, it may be more "readable" but try just adding half of your denominator
to your numerator and then just divide. The answer will be rounded up if
necessary. For example:
To see what this is doing just expand the two terms:
(x*51/41) + (20/41)
The second term just adds that "rounding-up 1/2" (well, next best thing since the
denominator is odd) to the original division. If said division would have wound up
a candidate for rounding up, then the extra 20/41 would increment the integer
division's answer for rounding.
Let's try another example:
It's a math hack but it beats doing two divisions and testing and what-not which
would slow down the program.
-Rusty-
P.S. Sorry folks, I'm not always good at explaining mathmatical trickery.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
-Rusty-
--
Rusty Haddock = KD4WLZ = rusty@fe2o3.lonestar.org
**Out yonder in the Van Alstyne (TX) Metropolitan Area**
Microsoft is to software what McDonalds is to gourmet cooking
Dave