Shop OBEX P1 Docs P2 Docs Learn Events
floating point not behaving...??? — Parallax Forums

floating point not behaving...???

adriadri Posts: 34
edited 2009-06-24 22:35 in Propeller 1
Hi

Newbie question again sorry.

I'm trying to do some floating point maths

I have a value from an ADC and I need to divide it by 8.192.

This integer maths works in a private method...

PRI ADCToValue(Sensor)
Sensor := Sensor - 2441
Sensor := Sensor / 8
Sensor := Sensor + 25
Result := Sensor

But it's not accurate enough.

I need to use 8.192 instead of 8 in the second line.

If anyone can suggest where I'm going wrong it'd be very welcome.

Many thanks

Adri

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-06-24 21:25
    If an integer result is satisfactory, you can use fixed scaling to get what you want like:

    Result := ((Sensor - 2441) * 1000) / 8192 + 25

    If you truly need floating point, you can download the FloatMath object from the Propeller Object Exchange and use those functions to provide floating point operations. There is no way to do floating point operations using the arithmetic operators (except for constant expressions).
  • adriadri Posts: 34
    edited 2009-06-24 22:35
    Many thanks Mike.

    Looks to be doing what I'm wanting.

    Cheers

    Adri
Sign In or Register to comment.