Math
10gigbill
Posts: 79
in Propeller 1
I want to type in a number zero to 6000 and multiply it by .682 and make the result
an integer zero to 4095 to send to a 12 bit D/A. I can't get the math to work???
thank you for your support...
an integer zero to 4095 to send to a 12 bit D/A. I can't get the math to work???
thank you for your support...
{{ test program }} Con _Clkmode = Xtal1 + pll16x _Xinfreq = 5_000_000 multi = 0.682 Var LONG outval 'end count long inval 'start count LONG oneval Obj pst: "Parallax Serial Terminal" Pub main pst.start(115_200) 'check the baud rate pst.str(string("hello",$0D)) repeat pst.rxflush pst.str(string("Gimmie a number -- ",$0D)) inval:=pst.Decin pst.str(string("got it... your answer is...",$0D)) outval := inval * multi pst.Dec (outval)
Comments
It would help if you took one step back from how you calculated .682, which can be expressed as a numerator / denominator pair.
Multiply first, then divide.
outval := inval * 4095 / 6000
Thanks Whicker...
Bill.
Or multiply by 1000 you "multi = 0.682" => 682 so you avoid the fractions
And result "outval := (inval * multi) /1000
This assumes "value" is scaled by the value of "denominator".