Shop OBEX P1 Docs P2 Docs Learn Events
Divide problem — Parallax Forums

Divide problem

marclamarcla Posts: 19
edited 2008-03-19 19:04 in Propeller 1
Hi!
When i divide 7845 with 36 i got 217 instead of 217,9166667 why??
I do like this:
VAR
long Time

Pub start
Time := 7845/36



and then i display time

Comments

  • Martin HebelMartin Hebel Posts: 1,239
    edited 2008-03-19 16:09
    Spin is an integer math interpreter (much like C and other languages), if you need floating point, you need to use the floating point object - FloatMath.spin.

    -Martin

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    SelmaWare Solutions - StampPlot GUI for controllers, XBee and Propeller Application Boards

    Southern Illinois University Carbondale, Electronic Systems Technologies

    American Technical Educator's Assoc. Conference·- April, Biloxi, MS. -- PROPELLER WORKSHOP!
  • marclamarcla Posts: 19
    edited 2008-03-19 16:14
    Can some one give me an exampel
  • Martin HebelMartin Hebel Posts: 1,239
    edited 2008-03-19 16:25
    Untested, but compiles....

    Obj
      FP : "FloatMath"
    
    
    Var
      Long val
    
    Pub main       
      Val := fp.fdiv(7856,36)
    
    
    
  • Mike GreenMike Green Posts: 23,101
    edited 2008-03-19 16:34
    Download the floating point library object from the Object Exchange. It includes a manual with examples.
  • Beanie2kBeanie2k Posts: 83
    edited 2008-03-19 16:56
    Martin Hebel said...
    Untested, but compiles....

    Obj
      FP : "FloatMath"
    
    
    Var
      Long val
    
    Pub main       
      Val := fp.fdiv(7856,36)
    
    
    


    Wouldn't you want to make "val" a double or float (whichever fp.div returns)? Also some compilers require a decimal point for floating constants (e.g. 7856.0,36.0) or else they interpret the numbers as integers. Is spin this way as well or is it smart enough to know you mean floating point?
  • hippyhippy Posts: 1,981
    edited 2008-03-19 17:04
    There isn't a double or float in Spin, but as the result is 32-bit a long can hold the returned value.

    Spin isn't smart enough to know anything, it doesn't care what's in a long variable or what that may mean to the programmer, just that it is a 32-bit value. It's up to the programmer to later use that variable in a context which makes sense.
  • Tracy AllenTracy Allen Posts: 6,660
    edited 2008-03-19 18:31
    Marcia,

    You might also do the problem in integer math.
    VAR
    long Time
    
    Pub start
    Time := 784500018/36   ' numerator times 10000, plus 18 for roundoff when divided by 36.
    ' display(Time/10000)  ' 217
    ' display(",")     ' radix
    ' display(Time//10000, fourDigits)  ' 217,9167
    




    or, an alternative method that can kick out as many digits as you want...

    VAR
    long Time
    byte digit
    
    Pub start
    Time := 7845/36   ' 217
    '   display(Time)   ' integer part
    '   display(",")    ' radix
    repeat 23     ' this many to the right of the decimal point
      Time := (Time // 36) * 10
      digit := Time / 36
      ' display(digit, oneDigit)
    digit := ((Time // 36) * 10 + 18)/36  ' round off last digit
    '   display(digit, oneDigit)
    ' 217,916666666666666666666667   24 decimal places.
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Martin HebelMartin Hebel Posts: 1,239
    edited 2008-03-19 19:04
    Beanie2k said...
    Also some compilers require a decimal point for floating constants (e.g. 7856.0,36.0) or else they interpret the numbers as integers. Is spin this way as well or is it smart enough to know you mean floating point?

    God catch, yes, the 36.0 and such is needed. since I answered initially, I felt obligated to follow-up, but didn't have time for a fully checked test. Sorry for the confusion.

    -Martin
Sign In or Register to comment.