Shop OBEX P1 Docs P2 Docs Learn Events
Simple float question — Parallax Forums

Simple float question

turbosupraturbosupra Posts: 1,088
edited 2010-10-30 20:56 in Propeller 1
I'm trying to get a simple floatmath calculation working that I can baseline off of and build on ... what am I doing wrong here?

CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000

VAR
long numerator
long denominator
long answer

OBJ
debug : "FullDuplexSerial"
f : "FloatMath"

PUB main | freq
debug.Start(31,30,0,115200) 'start cog with serial driver
waitcnt((clkfreq * 3) + cnt) ' makes life easier when starting to debug with a 2 second pause to get the serial viewer active and receiving

debug.Tx(13)
Debug.Tx(13)
Debug.Tx(13)
Debug.Str(string(" */\*/\*/\*/\*/\*/\*/\* START */\*/\*/\*/\*/\*/\*/\* About to enter main pub")) ' for debug purposes
Debug.Tx(13)
Debug.Tx(13)
Debug.Tx(13)
numerator := f.ffloat(22)
denominator := f.ffloat(7)

repeat
f.ffloat(answer)
answer := f.fdiv(numerator,denominator)
debug.str(string("The answer equals "))
debug.dec(f.ffloat(answer))
debug.tx(13)
waitcnt((clkfreq) + cnt)

Thanks for reading! The attachment has the spin file and the necessary math/debug spin files.

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2010-10-29 21:29
    Try using
    debug.dec(f.[COLOR="Red"]fround[/COLOR](answer))
    
    instead. ffloat converts int to float. As answer is a float you want to convert it to an int (fround) to be displayed by debug.dec().
  • turbosupraturbosupra Posts: 1,088
    edited 2010-10-29 21:41
    Hi,

    Thanks!

    I would like it displayed as a decimal though, is that possible? Are you saying debug.dec is not able to display a decimal?
  • kuronekokuroneko Posts: 3,623
    edited 2010-10-29 21:52
    turbosupra wrote: »
    I would like it displayed as a decimal though, is that possible? Are you saying debug.dec is not able to display a decimal?

    debug.dec does display a decimal number (an int in fact). If you want this in the form of 3.14 then have a look at the FloatString object (available in the prop tool library). Something along the lines of
    debug.str(fstring.FloatToString(answer))
    
    should do the trick.
  • turbosupraturbosupra Posts: 1,088
    edited 2010-10-29 22:03
    Thank you, that worked!

    Now if I can just get the rest of the calcs to work with this I will be set :)

    kuroneko wrote: »
    debug.dec does display a decimal number (an int in fact). If you want this in the form of 3.14 then have a look at the FloatString object (available in the prop tool library). Something along the lines of
    debug.str(fstring.FloatToString(answer))
    
    should do the trick.
  • turbosupraturbosupra Posts: 1,088
    edited 2010-10-29 22:38
    One more question if I could

    numerator/denominator = 16.666667 (numerator is 1000, denominator is 60)
    but
    (((clkfreq/2)/66656) /36) should also = 1000, but instead equals 1.345247e-42, see the quoted lines below, what would cause the difference?

    On serial out I see
    The answer equals 16.66667
    1.345247e-42
    
    
    
    CON
      _clkmode = xtal1 + pll16x
      _xinfreq = 5_000_000
    
    
      
    VAR
      long numerator
      long denominator
      long answer
      long answertwo
      long halfperiod
     
    
    OBJ
      debug : "FullDuplexSerial"
      f   : "FloatMath"
      fstring : "FloatString"
      
    
    PUB main  | freq
      debug.Start(31,30,0,115200) 'start cog with serial driver
      waitcnt((clkfreq * 3) + cnt)          ' makes life easier when starting to debug with a 2 second pause to get the serial viewer active and receiving
    
      debug.Tx(13)
      Debug.Tx(13)     
      Debug.Tx(13)
      Debug.Str(string(" */\*/\*/\*/\*/\*/\*/\* START */\*/\*/\*/\*/\*/\*/\* About to enter main pub"))          ' for debug purposes
      Debug.Tx(13)
      Debug.Tx(13)     
      Debug.Tx(13)
      numerator := 1000
      denominator := 60
      halfperiod := 66656
    
      
      
      repeat
        'answer := f.ffloat(answer)
        answer := f.fdiv(numerator,denominator)
        debug.str(string("The answer equals "))
        debug.str(fstring.FloatToString(answer))
        'debug.dec(f.fround(answer))
        debug.tx(13)
    
        answertwo := ((((clkfreq / 2) / halfperiod)/  36) * 60) 'cklfreq/2/halfperiod/36 = 16.666667 .... 16.666667 * 60 = 1000
        debug.str(fstring.FloatToString(answertwo))
        debug.tx(13)
        waitcnt((clkfreq) + cnt)
    
    
  • kuronekokuroneko Posts: 3,623
    edited 2010-10-29 22:51
    turbosupra wrote: »
    halfperiod := 66656
    
        answertwo := ((((clkfreq / 2) / halfperiod)/  36) * 60)
        debug.str(fstring.FloatToString(answertwo))
    
    1. answertwo is an integer, so use debug.dec()
    2. as you are using integer arithmetic you won't arrive at 1000
    • 80_000_000/2 = 40_000_000
    • 40_000_000/halfperiod = 600.096015
    • 600/36 = 16.666667
    • 16*60 = 960
  • Heater.Heater. Posts: 21,230
    edited 2010-10-29 22:52
    turbosupra,

    Your calculation:
    answertwo := ((((clkfreq / 2) / halsperiod)/ 36) *60)
    

    is being done as normal signed integer. So you have an integer result of 1000 which you should print as
    debug.dec(answertwo)
    
  • turbosupraturbosupra Posts: 1,088
    edited 2010-10-30 20:56
    Thank you both,

    I found that sometimes I have to use these objects

    object fullduplexserial = debug
    object floatstring = fstring
    object floatmath = f

    and then f.div(variable1, variable2)
    or f.div(variable1, variable2.0) as it wanted a .0 to properly display and calculate

    and then debug.str(fstring.floattostring(variablename))
    or debug.dec(variablename)

    There are also numbers too big, the float object cannot accept 40,000,000 but can accept 40,000 ? So some scaling might be necessary.
Sign In or Register to comment.