Shop OBEX P1 Docs P2 Docs Learn Events
Math Problems — Parallax Forums

Math Problems

ThricThric Posts: 109
edited 2010-08-03 22:20 in Propeller 1
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.

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

  • Mike GreenMike Green Posts: 23,101
    edited 2010-08-03 16:26
    PI is a floating point constant while everything else you're doing is integer. The integer value of PI is a large number. Download the Floating Point library from the Object Exchange and read the documentation included in the .ZIP archive. You can also take a look at the Wikipedia article on IEEE Floating Point for a description of the format of floating point values.

    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
  • Fred HawkinsFred Hawkins Posts: 997
    edited 2010-08-03 16:38
    Or alternatively, for pi use 355/113 (Thank you Leo Brodie, "Starting Forth")*. Often you can factor this into your code to minimize the division.

    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
  • ThricThric Posts: 109
    edited 2010-08-03 20:19
    So something like this works?(I wanted to convert degrees to radians):

    math.radians(h)  'this instead of temp:=h*pi/180
    

    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?
  • Fred HawkinsFred Hawkins Posts: 997
    edited 2010-08-03 21:38
    brute force method, which I think works.

    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.
  • Cluso99Cluso99 Posts: 18,069
    edited 2010-08-03 22:12
    Your problem is with the temp:=... statement. Try result:=..... or you need to make your pub return temp.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    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
  • John AbshierJohn Abshier Posts: 1,116
    edited 2010-08-03 22:20
    >math.radians(h) probably will not work because h is probably an integer. I cannot tell for sure from your code fragement.

    h:=pst.decin ' read in as integer
    h:=math.radians(math.ffloat(h)) ' convert to float and then convert to radians

    John Abshier
Sign In or Register to comment.