PDA

View Full Version : rounding numbers



DestroyerX
02-22-2005, 12:58 AM
Is there a command in PBASIC 2.5 that will round a given number to an integer?

Thanks,

Jason

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Unreal tournament 2004 rox

Boris
02-22-2005, 01:14 AM
I think BS only work with integers. How did you get a floating point value?

Robert Schwartz
02-22-2005, 01:23 AM
PBASIC doesn't do floating point arithmatic, all numbers are truncated. Also, all numbers are unsigned, so you can't have negative numbes, only whole numbers.

DestroyerX
02-22-2005, 02:02 AM
If I multiply two numbers and get a decimal, then use that number in say the duty for PWM, will the decimal be rounded or will the PWM just refuse to take something that is not a whole number between 0 and 255?

Thanks again,

Jason

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Unreal tournament 2004 rox

Robert Schwartz
02-22-2005, 02:11 AM
The decimal will be truncated. 1.2 becomes 1, 1.999999 becomes 1. The PWM will take the truncated number.

Chris Savage
02-22-2005, 02:16 AM
When you multiply two numbers on the BASIC Stamp, you get an integer, no matter what.ˇ The only way to work with floating point math is to use a floating-point co-processor, such as the uFPU.ˇ Even then, the PWM command only accepts an integerˇvalue of 0-255 for it's 2 arguments.




▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
--==<{Chris}>==--

DestroyerX
02-22-2005, 02:29 AM
I'm multiplying decimals and fractions. How would those values affect the BS2?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Unreal tournament 2004 rox

achilles03
02-22-2005, 03:01 AM
Back to the original question: how are you getting decimals and fractions in the BS2's programming to begin with? If possible, cut and paste some code and tell us where those numbers are coming from.

Dave

DestroyerX
02-22-2005, 03:53 AM
I'm taking a value of an input pulse that is between 0 and 205 and multiplying it by 54/41 to get a value between 0 and 255 that can be used for the duty in PWM. For example, if the input pulse is 25, then the new value is 1350/41 (or ~32.93) after multiplying by 54/41.

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

Boris
02-22-2005, 04:23 AM
in BS2:
25*54=1350
1350/41=32 NOT (~32.93)

DestroyerX
02-22-2005, 04:26 AM
I'm sorry, I wrote it incorrectly.

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

Boris
02-22-2005, 05:13 AM
ok, instead of retyping the manual here, why dont you just read it?
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" :)

Chris Savage
02-22-2005, 05:13 AM
Jason,

ˇˇ 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}>==--

Boris
02-22-2005, 05:23 AM
the result of this:
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.

DestroyerX
02-22-2005, 05:39 AM
Oh, okay, thanks! That's valuable information. I think I figured out a way to avoid using demicals in my project and still have similar functionality.

Thanks for all of your help everyone.

Jason

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Unreal tournament 2004 rox

Robert Schwartz
02-22-2005, 05:43 AM
The bs2 does use parenthases. Bs1 does not. So:
25 * (51/41)
25 * (1)
=25

achilles03
02-22-2005, 10:26 AM
Also, if you want to give it round up capabilities, then just determine if the remainder (see the // function) of whatever you're dividing by is greater than half. For instance, if you're doing x*51/41, then you can give it rounding capabilities by doing something like the following:

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

Fe2o3Fish
02-22-2005, 04:09 PM
Dave (achilles03),
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:



' 3*51/41 ==> 3.73... (round up)
x = 3
y = (x * 51 + 20) / 41 '(153+20)/41 = 4



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:



' 2*51/41 ==> 2.4878... (don't round up)
x = 2
y = (x * 51 + 20) / 41 ' (102+20)/41 = 2



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

achilles03
02-22-2005, 09:43 PM
Sure, that'd work also. Same concept, different means.

Dave