comparison between floats
gio_rome
Posts: 48
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
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
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.
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.