Math Problems
Thric
Posts: 109
Although math is one of my more favorable subjects getting it functioning on the propeller for me is proving a challange.
I've created a program in which I send a decimal value from my pc to my prop via the Parallax Serial Terminal which then does some math work and spits out the result. What I'm inputing and what I'm getting out is not what I want and I can't figure out why its not working. Any help is appreciated.
When I put in 80 I get out·2128083 when I want 1.396222...
I've created a program in which I send a decimal value from my pc to my prop via the Parallax Serial Terminal which then does some math work and spits out the result. What I'm inputing and what I'm getting out is not what I want and I can't figure out why its not working. Any help is appreciated.
con _clkmode = XTAL1 + PLL16X _Xinfreq = 5_000_000 '80 MHz Var long temp
obj pst: "Parallax Serial Terminal"
Pub main |h pst.startrxtx(2,3,0,9800) pst.clear pst.str(string("Please Enter Longitude",13)) h:=pst.decin pst.dec(h) pst.newline convert(h) pst.dec(temp)
Pub convert(h) temp:=h*pi/180
When I put in 80 I get out·2128083 when I want 1.396222...
Comments
The Spin compiler, although it will handle floating point constants and constant expressions properly, doesn't do what you'd expect with floating point expressions involving variables. It takes the internal equivalent of the floating point value and treats it as a 32-bit integer for arithmetic purposes.
Post Edited (Mike Green) : 8/3/2010 4:31:02 PM GMT
so temp:=h*355/20340
further·temp:=h*71/4068
* google search value = starting forth pi approximation
Post Edited (Fred Hawkins) : 8/3/2010 5:05:18 PM GMT
HOwever what displays on my terminal is 1 but i'm expecting around 1.396. Is there something special that I have to do to convert the variable to show the decimals?
multiple your answer by 1000. Thus 1*1000 = 1000 (Your answer is an integer, right?)
then recalculate h*pi*1000/180 then subtract the first number (1000), giving your decimal value.
then display your answer 1, output a decimal point, then output the decimal value.
Obviously this is just a displayed value, not something you can poke into an equation.
But try this thought, using your example h, and my third equation for temp, and a fudge factor of 1000.
80*71000/4068 = 1396. (ignoring the fractional .263520, which is dropped by the integer math) So you could display 1396 and just mentally add a decimal point.
Or continue to calculate (you're going to be using those radians for something, right?), knowing that you are working with radians multipled by 1000. Eventually, your last step could be to take out this 1000 factor.
1000 is just to shift the fractional values to the left of the decimal point. You could use 100 for 2 digits or just 10 for 1 digit.
The advantage is that you're working in integer numbers, quick and pretty painless.
355/113 is pi accurate to 7 places. It's doubtful that you need any greater accuracy. 22/7 is accurate to 3 places, btw.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Links to other interesting threads:
· Home of the MultiBladeProps: TriBlade,·RamBlade,·SixBlade, website
· Single Board Computer:·3 Propeller ICs·and a·TriBladeProp board (ZiCog Z80 Emulator)
· Prop Tools under Development or Completed (Index)
· Emulators: CPUs Z80 etc; Micros Altair etc;· Terminals·VT100 etc; (Index) ZiCog (Z80) , MoCog (6809)·
· Prop OS: SphinxOS·, PropDos , PropCmd··· Search the Propeller forums·(uses advanced Google search)
My cruising website is: ·www.bluemagic.biz·· MultiBlade Props: www.cluso.bluemagic.biz
h:=pst.decin ' read in as integer
h:=math.radians(math.ffloat(h)) ' convert to float and then convert to radians
John Abshier