Shop OBEX P1 Docs P2 Docs Learn Events
floattoformat — Parallax Forums

floattoformat

CncjerryCncjerry Posts: 64
edited 2012-08-03 13:48 in Propeller 1
Can't get floatforformat to compile. The other Float String routines compile
        
     f1:=float32.FFloat(micros)      ' micros is now floating
     f2:=float32.FFloat(1000000)
     fps:=float32.FDiv(f1,f2)       
     pst.str(fs.floattostring(fps))
     pst.Chars(pst#NL, 2)
     f2:=float32.FFloat(1)
     fps:=float32.FDiv(f2,fps)
     pst.str(fs.floattostring(fps))
     pst.Chars(pst#NL, 2)
           pst.str(fs.FloatToFormat(100.0,8,2))



I get an "expected a subroutine name" on the last line above no matter how I type it. The priors FloattoString and others compile. I checked the module and the routine is there.

Jerry

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-08-03 09:59
    IIRC floattoformat isn't in the current float to string object.

    I'm pretty sure you can get by without that method.

    If you're just using basic arithmatic you don't really need floating point numbers. You just multiple by 1000 or some other number to retain precision. You can then use a method "DecPoint" (a forum search should turn it up) to display the number with a decimal point in the desired location.

    I'm pretty sure I have a copy of the float to string object that uses floattoformat, I'll find it and post it here in a bit.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-08-03 10:10
    The program I'm currently working on uses the "DecPont" method.

    Here's the method using PST method calls:
    PUB DecPoint(value, denominator)
      if value < 0
        Pst.Char("-")
        -value
          
      if value => denominator
        result := value / denominator
        Pst.Dec(result)
        value //= denominator     
      else    
        Pst.Char("0")
      Pst.Char(".")  
      repeat while denominator > 1
        denominator /= 10
        if value => denominator
          result := value / denominator
          Pst.Dec(result)
          value //= denominator
        else
          Pst.Char("0")
     
    

    If you had a variable "x" which held the value "123456" and you wanted this value displayed as "123.456", you call the above method with;
      DecPoint(x, 1000)
    
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-08-03 10:13
    Here's the FloatToFormat method:
    PUB FloatToFormat(single, width, dp) : stringptr | n, w2
    ''Convert floating-point number to formatted string
    ''
    ''  entry:
    ''      Single = floating-point number
    ''      width = width of field
    ''      dp = number of decimal points
    ''
    ''  exit:
    ''      StringPtr = pointer to resultant z-string
    ''
    ''  asterisks are displayed for format errors 
    ''  leading blank fill is used
      ' get string pointer
      stringptr := p := @float_string
      ' width must be 1 to 9, dp must be 0 to width-1
      w2 := width  :=  width #> 1 <# 9
      dp := dp #> 0 <# (width - 2)
      if dp > 0
        w2--
      if single & $8000_0000 or positive_chr
        w2--
      ' get positive scaled integer value
      n := F.FRound(F.FMul(single & $7FFF_FFFF , F.FFloat(teni[dp])))
      if n => teni[w2]
        ' if format error, display asterisks
        repeat while width
          if --width == dp
            if decimal_chr
              byte[p++] := decimal_chr
            else
              byte[p++] := "."
          else
            byte[p++] := "*" 
        byte[p]~
      else
        ' store formatted number
        p += width
        byte[p]~
        repeat width
          byte[--p] := n // 10 + "0"
          n /= 10
          if --dp == 0
            if decimal_chr
              byte[--p] := decimal_chr
            else
              byte[--p] := "."
          if n == 0 and dp < 0
            quit
        ' store sign      
        if single & $80000000
          byte[--p] := "-"
        elseif positive_chr
          byte[--p] := positive_chr
        ' leading blank fill
        repeat while p <> stringptr
          byte[--p] := " "
    
  • CncjerryCncjerry Posts: 64
    edited 2012-08-03 10:14
    the floattostring object I have has floattoformat. I think it has something to do with the variable 'teni' not being in the var list for the object. I copied it into my code and it compiled once I added 'teni' to the var list.

    I get '****.**' for output so something is wrong.

    I tried using integer math and couldn't get it to work.

    I have a delta time number in clock ticks like 43385201. This gets divided by 80_000_000 to get seconds. 1 divided by seconds turns into feet per second and displayed to two decimal positions. I need to do all this within the rate of fire for the automatic weapon we are timing with the high end of 1,800 rounds per minute or 3 per second.

    Thanks for the help,

    Jerry
  • CncjerryCncjerry Posts: 64
    edited 2012-08-03 13:48
    I figured this one out. I guess there was an older version of floattostring in the library.
Sign In or Register to comment.