Shop OBEX P1 Docs P2 Docs Learn Events
Error in Float32.spin — Parallax Forums

Error in Float32.spin

John AbshierJohn Abshier Posts: 1,116
edited 2006-10-05 14:10 in Propeller 1
I cannot get the Degrees and Radians functions to work.· In the code below they print as zero instead of 45 and 0.785.
CON
  _CLKMODE = XTAL1 + PLL16X
  _XINFREQ = 5_000_000
  c = 180.0 / pi
  d = pi / 180.0
VAR
  
OBJ
    Tv : "TV_Text"
    F : "Float32Full"
    FTS : "FloatString"
PUB Main    | x, y, a, b
  Tv.start(12)                                          ' should check fail
  F.start
  Tv.out($00)                 ' clear
  Tv.out($01)                 ' home
  TV.str(FTS.FloatToString(F.Degrees(0.785))) ' prints 0 not 45
  Tv.out($09)
  Tv.str(FTS.FloatToString(F.Radians(45.0)))  ' prints 0 not 0.785
  Tv.out($09)
  Tv.str(FTS.FloatToString(F.FMul(c, 0.785))) ' prints 44.97719
  Tv.out($09)
  Tv.str(FTS.FloatToString(F.FMul(d, 45.0)))  ' prints 0.785398

Comments

  • John AbshierJohn Abshier Posts: 1,116
    edited 2006-09-27 01:15
    Here is the fix
    PUB Radians(a) | b, c
      b := a
      c := 0.017453293         ' change
      'sendCmd(FMulCmd + @a) 
      command := FMulCmd + @b
      repeat while command
      return cmdReturn  
    PUB Degrees(a) | b, c
      b := a
      c := 57.2957795          ' change
      'sendCmd(FMulCmd + @a) 
      command := FMulCmd + @b
      repeat while command
      return cmdReturn  
     
    
  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-09-27 02:00
    Thanks for figuring out a fix John, I was just too busy today to take a look at it.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • TyreBiterTyreBiter Posts: 40
    edited 2006-09-28 11:30
    The same question was asked in thread "Float32.radians seems to fail"
    nice to see an answer no matter where it is poster. smile.gif
  • camtcamt Posts: 45
    edited 2006-10-05 14:10
    Thanks John for posting a fix. I've corrected the problem in both Float32 and Float32Full and sent an update to the object exchange and included the updated library here.

    For those interested, this bug was a trap that I'd documented in the Tips and Traps on page 10 of the floating point document - which I then proceeded to fall into myself!

    Spin recognizes floating point constants, but all expressions are evaluated as integer expressions unless you use the constant directive.
    c := pi / 180.0 ' is incorrect
    c := constant(pi / 180.0) ' yields the correct result

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cam Thompson
    Micromega Corporation
Sign In or Register to comment.