Shop
OBEX
P1 Docs
P2 Docs
Learn
Events
Faster fractional multiply — Parallax Forums
toggle menu
Categories
Discussions
Sign In
·
Register
Sign In
·
Register
Categories
Discussions
Sign In
·
Register
×
Home
›
Propeller 1
Faster fractional multiply
Bobb Fwed
Posts:
1,119
2011-07-12 08:48
edited 2011-07-12 09:10
in
Propeller 1
I want to multiply a 16-bit number by 1.054. Is there a faster way to do it other than float math and not using any additional cogs.
Comments
ericball
Posts:
774
2011-07-12 08:55
edited 2011-07-12 08:55
Yes. Use fixed point.
Use PhiPi's solution. I always forget that SPIN uses signed values. (Plus his is probably slightly faster anyway.)
Phil Pilgrim (PhiPi)
Posts:
23,514
2011-07-12 09:04
edited 2011-07-12 09:04
Similar to Eric's:
y += y ** 231928234 'Multiply y by 1.054
This multiplies y by 0.054 * 2
32
, returning the high 32 bits, then adds the result to y (i.e. the "1" part).
-Phil
Ale
Posts:
2,363
2011-07-12 09:05
edited 2011-07-12 09:05
Well... if you use a 16.16 format, and multiply by 1.0000110111010010 then you get a passable approximation (1.0539856)
result = (x << 16) * $1_0DD2
Bobb Fwed
Posts:
1,119
2011-07-12 09:10
edited 2011-07-12 09:10
Gives me negative numbers ('cause the "fraction" is signed).
Changed it to
result := input << 2 ** $4374_BC6A
Thanks!
Sign In
or
Register
to comment.
Comments
Use PhiPi's solution. I always forget that SPIN uses signed values. (Plus his is probably slightly faster anyway.)
This multiplies y by 0.054 * 232, returning the high 32 bits, then adds the result to y (i.e. the "1" part).
-Phil
result = (x << 16) * $1_0DD2
Changed it to Thanks!