Shop OBEX P1 Docs P2 Docs Learn Events
comparison between floats — Parallax Forums

comparison between floats

gio_romegio_rome Posts: 48
edited 2014-02-11 18:51 in Propeller 1
sorry for the inconvenience....

I'm stuck now in this comparison between numbers.

R<R_rif

where R is a LONG number that comes to be like this:

A:=adc.in(o) (something like 2000, when I dec-print it to lcd or to file)
B:=adc.in(1) (ditto)
R:=A/B or f.fdiv(a,b) or f.fdiv(f.ffloat(a),f.ffloat(b))

they're all LONG numbers

and R is something like 103453 when I DEC-print it or something like 0.93 when I
BS2.LCD_STR(FP.FloatToFormat(R,5,3))-print it

THAT'S MORE LIKE IT! since R_riff would be something like 0.90...a CON of mine.

but the comparison just doesn't work. Or it has a _different_ behaviour when I compare it with "1" or with "1.0". Crazy. Well it's not crazy, just stressful.

Suggestion? :-(

I have to compare 0.93 with a number that comes from a division of numbers coming from the ADC. They are all trasformed and treated so that they make actual physical sense to me, and I expect the IF too make sense too.


I know it's a matter of number representation, I just don't know how

tnx in advance

Comments

  • JonnyMacJonnyMac Posts: 9,107
    edited 2014-02-11 08:44
    You could do something like this:
    ch0 := adc.in(0)
      ch1 := adc.in(1)
    
      check := 100 * ch1 / ch2
    
      if (check => 93)
        ' do something about it
    
  • kwinnkwinn Posts: 8,697
    edited 2014-02-11 18:44
    gio_rome wrote: »
    sorry for the inconvenience....

    I'm stuck now in this comparison between numbers.

    R<R_rif

    where R is a LONG number that comes to be like this:

    A:=adc.in(o) (something like 2000, when I dec-print it to lcd or to file)
    B:=adc.in(1) (ditto)
    R:=A/B or f.fdiv(a,b) or f.fdiv(f.ffloat(a),f.ffloat(b))

    they're all LONG numbers

    and R is something like 103453 when I DEC-print it or something like 0.93 when I
    BS2.LCD_STR(FP.FloatToFormat(R,5,3))-print it

    THAT'S MORE LIKE IT! since R_riff would be something like 0.90...a CON of mine.

    but the comparison just doesn't work. Or it has a _different_ behaviour when I compare it with "1" or with "1.0". Crazy. Well it's not crazy, just stressful.

    Suggestion? :-(

    I have to compare 0.93 with a number that comes from a division of numbers coming from the ADC. They are all trasformed and treated so that they make actual physical sense to me, and I expect the IF too make sense too.


    I know it's a matter of number representation, I just don't know how

    tnx in advance

    When R is calculated by A/B they are both long integers, and based on A being around 2000 and you expecting an answer of about 0.93 then B would have to be about 2150. Doing the calculation in integer math would result in an answer of 0 and a remainder of 2000. Not what you want.

    The solution is either to convert A and B to floating point and do the calculations in floating point, or do as JonnyMac suggested and multiply ch1 by 100 (or 1000) before dividing by ch2 so you don't loose precision when doing the integer divide.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-02-11 18:51
    You need to use the "FCmp" method to compare floating point numbers.
    PUB FCmp(a, b){{
      Floating point comparison.
      Parameters:
        a        32-bit floating point value
        b        32-bit floating point value
      Returns:   32-bit integer value
                 -1 if a < b
                  0 if a == b
                  1 if a > b
    }}
    

    Set a temporary variable to the return value and then use case or if, elseif to branch your program.

    I personally only like to use floating point when absolutely necessary. As Jon a Kwinn suggested, it's pretty easy to scale your integers by a fixed value before dividing so you don't need to resort to floating point math.
Sign In or Register to comment.