Using the */ operator
Archiver
Posts: 46,084
Sitting around waiting for parts, so I thought I would post this for the
benefit
of some of the new kids on the block.
The code below shows how to multiply a whole number by a decimal:
'{$STAMP BS2}
'{$PBASIC 2.5}
'Operator is */
a var word
a = 56
'We want to multiply a * 1.47
'1.47 is the multiplier so let's convert that to HEX
'1 in HEX = $01
'.47 in HEX = .47 * 255 = 119.85/ We drop the decimal .85, round off to 120
'120 in HEX = $78
'Now our mulitplier in HEX = $0178
a = a*100 'Muliplying a * 100, then dividing a by 100 gives us 2
decimals
'for greater accuracy. Be sure that a*100 does not exceed
65535.
a = a*/$0178
DEBUG "a = ", DEC a/100, ".", DEC2 a, cr
'a = a *1.47 = 82.32
'a*/$0178 = 82.25
'That's pretty close!
Hope this helps someone.
[noparse][[/noparse]Non-text portions of this message have been removed]
benefit
of some of the new kids on the block.
The code below shows how to multiply a whole number by a decimal:
'{$STAMP BS2}
'{$PBASIC 2.5}
'Operator is */
a var word
a = 56
'We want to multiply a * 1.47
'1.47 is the multiplier so let's convert that to HEX
'1 in HEX = $01
'.47 in HEX = .47 * 255 = 119.85/ We drop the decimal .85, round off to 120
'120 in HEX = $78
'Now our mulitplier in HEX = $0178
a = a*100 'Muliplying a * 100, then dividing a by 100 gives us 2
decimals
'for greater accuracy. Be sure that a*100 does not exceed
65535.
a = a*/$0178
DEBUG "a = ", DEC a/100, ".", DEC2 a, cr
'a = a *1.47 = 82.32
'a*/$0178 = 82.25
'That's pretty close!
Hope this helps someone.
[noparse][[/noparse]Non-text portions of this message have been removed]
Comments
benefit
of some of the new kids on the block.
The code below shows how to multiply a whole number by a decimal:
'{$STAMP BS2}
'{$PBASIC 2.5}
'Operator is */
a var word
a = 56
'We want to multiply a * 1.47
'1.47 is the multiplier so let's convert that to HEX
'1 in HEX = $01
'.47 in HEX = .47 * 255 = 119.85/ We drop the decimal .85, round off to 120
'120 in HEX = $78
'Now our mulitplier in HEX = $0178
a = a*100 'Muliplying a * 100, then dividing a by 100 gives us 2
decimals
'for greater accuracy. Be sure that a*100 does not exceed
65535.
a = a*/$0178
DEBUG "a = ", DEC a/100, ".", DEC2 a, cr
'a = a *1.47 = 82.32
'a*/$0178 = 82.25
'That's pretty close!
'End of program
Hope this helps someone.
Sid Weaver
512K plug-in EEPROM.......
http://www.visualmuses.com/chipcircuit/index.html
[noparse][[/noparse]Non-text portions of this message have been removed]