Shop OBEX P1 Docs P2 Docs Learn Events
Floating Point calculation — Parallax Forums

Floating Point calculation

JkaneJkane Posts: 113
edited 2015-03-12 10:10 in Propeller 1
Hello,

I have a question on floating point,

I using floating point,

tMath : "FloatMath"
fString : "FloatString"

and performing this calculation

(0.1 * 13) / 13 which should give me 0.1 as output

but I am getting 0.07692308

Hz: 13 GPM: 0.07692308


I attached the code:

{Object_Title_and_Purpose}


CON
        _clkmode = xtal1 + pll16x                                               'Standard clock mode * crystal frequency = 80 MHz
        _xinfreq = 5_000_000

VAR
                
  long Transition_counter
  long pin_state
  long previous_pin_state
  long GPM
  long GPM_temp
 
  

OBJ
   term   : "FullDuplexSerial"
   Math : "FloatMath"
  '  fMath : "Float32full"   
  String : "FloatString"
  
PUB Main | t  

term.start(31, 30, 0, 115200)



dira[8]~  ' set to input := 0

'Hz is frequeny per second
' count the number of transisions from high->low->high

repeat
  term.tx(13) 
  term.str(string("Hz:  "))
   transition_counter := 13
  term.dec(transition_counter)
  'GPM := (0.1 * transition_counter) / 13
  fmath.FFloat(transition_counter)
  GPM_temp := fmath.FMul(0.10, transition_counter)
  GPM := fmath.FDiv(GPM_temp,13)
  term.str(string(" GPM:  ")) 
  term.str(fstring.FloatToString(GPM))
  
  GPM_temp := 0
  GPM := 0
  Transition_counter := 0           ' reset  Transision counter
  
   t := clkfreq + cnt               ' get next second
  repeat while cnt < t              ' loop until second is up
     waitcnt(clkfreq/1000 + cnt)    ' wait one millisecond
     pin_state := ina[8]         ' check high or low
    
        if pin_state == previous_pin_state
             'do nothing
        else
            previous_pin_state := pin_state   ' make new pin state
            transition_counter++              ' and count the transision
     

regards

Jeff

Comments

  • LeonLeon Posts: 7,620
    edited 2015-03-12 05:09
    Use code tags!
  • JkaneJkane Posts: 113
    edited 2015-03-12 05:24
    Ok,

    Jeff
  • BeanBean Posts: 8,129
    edited 2015-03-12 05:56
    Jeff,
    I think the line
    fmath.FFloat(transition_counter)
    

    should be
    transition_counter := fmath.FFloat(transition_counter)
    

    Bean
  • JkaneJkane Posts: 113
    edited 2015-03-12 06:08
    Bean


    that yields

    Hz: 13 GPM: 5.444519e+38

    regards

    Jeff
  • BeanBean Posts: 8,129
    edited 2015-03-12 06:14
    Okay,
    Oh, I see. I think this needs changed too.
    GPM := fmath.FDiv(GPM_temp,13)
    
    to
    GPM := fmath.FDiv(GPM_temp,FFloat(13))
    

    Basically you can't use integers ANYWHERE you are doing floating point stuff. Without using FFloat.
    I'm not sure, but you might be able to just use 13.0 too.

    Bean

    P.S. Yes, I tried it. using 13.0 will work.
  • ersmithersmith Posts: 6,053
    edited 2015-03-12 06:14
    The line
     GPM := fmath.FDiv(GPM_temp,13)
    
    is mixing floating point and integer -- the 13 should probably be 13.0. That error is partially masked by the one Bean noticed above, where transition_counter is never updated to be in floating point. The result is that you're doing all the math using the floating point number whose binary representation is 0x0000000d (13), which is a very tiny floating point number -- small enough so that you're encountering significant round off errors. The floating point representation of 13.0 is actually 0x41500000.
  • JkaneJkane Posts: 113
    edited 2015-03-12 06:43
    Yes, it worked, picky, picky, picky.

    anyway, the changes
    'GPM := (0.1 * transition_counter) / 13
      transition_counter := fmath.FFloat(transition_counter)
      GPM_temp := fmath.FMul(0.10, transition_counter)
      GPM := fmath.FDiv(GPM_temp ,13.0)
      term.str(string(" GPM: ")) 
      term.str(fstring.FloatToString(GPM))
    
    

    output is now correct

    Hz: 13 GPM: 0.1

    thanks for the help

    regards

    Jeff
  • Duane DegnDuane Degn Posts: 10,588
    edited 2015-03-12 10:10
    The object "F32" is a new and improved floating point object. Lonesock sped up the methods and was able to fit what used to take two cogs into a single cog.

    The latest version (i think it's 1.6) is in the OBEX. F32 is also included in the archive attached to my integer vs floating point trig speed tests.
Sign In or Register to comment.