Shop OBEX P1 Docs P2 Docs Learn Events
Trouble with a Math Library — Parallax Forums

Trouble with a Math Library

heathclfheathclf Posts: 43
edited 2009-02-02 02:47 in Propeller 1
Trying to map my wall with an IR range finder and the MX2125 Accelerometer. For reasons not worth explaining, I have to turn the accelerometer on it's side, and the angle will be angle := Tan(x/y). I'm printing my result out to an LCD at the moment, hoping to put it on a usb later. Things are working fine, until I try to compute Tan(x/y)...or even Tan(0.5). If I try to use the library, my LCD is completely blank. If I comment the line out, the first two numbers (x and y pulse widths) print out just fine.

I'm using the Float32 library, which can be found here: obex.parallax.com/objects/202/

Here's my code, but it works fine as long as I comment out the Tan() line.

OBJ
  LCD   : "debug_lcd.spin"
  math  : "Float32.spin"
  accel : "MXd2125.spin"

PUB main

  accel.start(PWM_MonitorPin_x, PWM_MonitorPin_y)
  InitializeLCD

  repeat
    LCD.gotoxy(0,0)
    'LCD.str(string("yo"))
    x := accel.x - 400_000
    y := accel.y - 400_000
    angle := math.Tan(x / y)                     'This line causes nothing to be printed on my LCD at all
    LCD.decf(x, 8)
    LCD.gotoxy(0,5)
    LCD.decf(y, 8)
    LCD.gotoxy(0,1)
    'LCD.decf(angle, 4)
    waitcnt(clkfreq / 2 + cnt)



Any thoughts are much appreciated.

Thanks.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-02-02 02:34
    First of all, x and y are not floating point. You would have to convert them to floating point using the math.ffloat function, then use math.fdiv to divide them, then do the tangent. I don't think that LCD.decf knows anything about floating point so there's some more scaling and conversion needed there.

    At the heart of this is a basic fact. The Spin compiler has limited support for floating point. It can handle floating point constants and it can perform basic arithmetic on constant expressions using floating point, but it does no automatic conversions and it does no floating point arithmetic when the program is running, only during compilation. Any floating point values are treated as integers at run-time. The floating point libraries do arithmetic in floating point as function calls, but they pass around 32-bit integers which happen to contain values in floating point format.
  • heathclfheathclf Posts: 43
    edited 2009-02-02 02:47
    Thanks, Mike. Much appreciated.
Sign In or Register to comment.