Shop OBEX P1 Docs P2 Docs Learn Events
Trouble building a calculator with the FloatMath and FloatString objects — Parallax Forums

Trouble building a calculator with the FloatMath and FloatString objects

mneuertmneuert Posts: 9
edited 2013-03-10 16:27 in Propeller 1
Hello,

Thanks in advance to anyone willing to help me out. My issue seems fairly trivial, but I appreciate all help nonetheless.

I've been playing around with the "Parallax Serial Terminal.spin" object and am trying to make a simple calculator. Here is my code so far:
CON
  _xinfreq=5_000_000
  _clkmode=xtal1+pll16x


OBJ
  pst : "Parallax Serial Terminal"
  f : "FloatMath"
  fs : "FloatString"


PUB Calculator | A,B,operation
  pst.start(115200)


  pst.str(fs.FloatToString(f.FAdd(5.0,5.2)))
  pst.NewLine
  pst.str(fs.FloatToString(f.FSub(2.0,3.5)))
  pst.NewLine
  pst.str(fs.FloatToString(f.FMul(3.0,4.0)))
  pst.NewLine
  pst.str(fs.FloatToString(f.FDiv(5.5,11.0)))
  pst.NewLine
  pst.NewLine
  
  repeat
    pst.str(string("Enter the first number "))
    'A:=pst.GetFloat
    A:=pst.DecIn
    pst.str(fs.FloatToString(A))
    pst.NewLine
    pst.NewLine


    pst.str(string("Enter the second number "))
    'B:=pst.GetFloat
    B:=pst.DecIn
    pst.str(fs.FloatToString(A))
    pst.NewLine
    pst.NewLine
    
    pst.str(string("Select an operation (+ - * /) "))
    'operation:=pst.rx
    operation:=pst.CharIn
    'pst.NewLines(2)
    pst.NewLine
    pst.str(string("The solution is "))
    case operation
      "+" : pst.str(fs.FloatToString(f.FAdd(A,B)))
      "-" : pst.str(fs.FloatToString(f.FSub(A,B)))
      "*" : pst.str(fs.FloatToString(f.FMul(A,B)))
      "/" : pst.str(fs.FloatToString(f.FDiv(A,B)))
      other : pst.str(string("ERROR!"))
    'pst.NewLines(3)
    pst.NewLine
    pst.NewLine
    pst.NewLine

When I run my code, I get the following output:

Capture.PNG


The sample calculations from the first part of my code seem to work out just fine (that's the 10.2, -1.5, etc., from the first part of the output), but when I try to take input from they keyboard (that's 8.0 and 4.5), the output is not at all what I intend; somehow, both 8.0 and 4.5 are being read as 1.121039e-43, which don't even seem to be added together properly (1.121039e-43 + 1.121039e-43 != 1.751623e-43).

Could someone please point out why my code isn't adding 8.0 and 4.5? Am I using the correct methods from FloatMath and FloatString?

One other thing is that if the numbers that I use in the fist part of my code are not floats, then the first calculations don't work either. In other words, if I try and write
pst.str(fs.FloatToString(f.FMul(3,4.0)))
at the beginning instead of using 3.0 and 4.0, the output becomes 1.681558e-44 instead of 12. Is this happening for the same reason that my calculator isn't working?

Thanks again for all your help - it's very much appreciated!

-MN
652 x 365 - 12K

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2013-03-09 21:32
    Problem is that you read the terminal input as decimal and treat that bit pattern as float, NG. Grab a newer version of FloatString from the OBEX and then try this:
    CON
      _xinfreq=5_000_000
      _clkmode=xtal1+pll16x
    
    
    OBJ
      pst : "Parallax Serial Terminal"
        f : "FloatMath"
       fs : "FloatString"
    
    VAR
      byte  one[20]
      byte  two[20]
      
    PUB Calculator | A,B,operation
    
      pst.start(115200)
      waitcnt(clkfreq*3 + cnt)
      
      repeat
        pst.str(string("Enter the first number "))
        [COLOR="#FFA500"]pst.StrIn(@one{0})
        A := fs.StringToFloat(@one{0})[/COLOR]
    
        pst.str(@one{0})
        pst.NewLine
        pst.NewLine
    
    
        pst.str(string("Enter the second number "))
        [COLOR="#FFA500"]pst.StrIn(@two{0})
        B := fs.StringToFloat(@two{0})[/COLOR]
    
        pst.str(@two{0})
        pst.NewLine
        pst.NewLine
        
        pst.str(string("Select an operation (+ - * /) "))
        operation:=pst.CharIn
        pst.NewLine
        pst.str(string("The solution is "))
        case operation
          "+" : pst.str(fs.FloatToString(f.FAdd(A,B)))
          "-" : pst.str(fs.FloatToString(f.FSub(A,B)))
          "*" : pst.str(fs.FloatToString(f.FMul(A,B)))
          "/" : pst.str(fs.FloatToString(f.FDiv(A,B)))
          other : pst.str(string("ERROR!"))
        'pst.NewLines(3)
        pst.NewLine
        pst.NewLine
        pst.NewLine
    
    DAT
    
  • mneuertmneuert Posts: 9
    edited 2013-03-10 11:15
    Thanks so much!

    One quick question - is there a reason you added the comments {0} after the pointer to one and two? I assume those are comments...

    -MN
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-03-10 11:25
    If you're interested in seeing another attempt at a floating point calculator, there's one here.

    There was a recent discussion about converting a float back to an integer while preserving precision of the fractional portion by using "pseudo real" numbers.
  • kuronekokuroneko Posts: 3,623
    edited 2013-03-10 16:27
    mneuert wrote: »
    One quick question - is there a reason you added the comments {0} after the pointer to one and two?
    Yes they are comments. And it's just personal preference. @one is the same as @one[0] but uses less memory. Because it's an array I still want an indication of said fact so I put the comment in there.
Sign In or Register to comment.