Compiler error compare two negativ values go wrong

two negativ values with the same values go wrong

The if return a true on compare it is like compare <=

The error come on Version of 1. 8.10. And 1. 8.12.

Help.

sensor*.v_temp_diff = - 0.06*

  • v_wertdb = -0.06*
    if ( sensor*.v_temp_diff < v_wertdb ) { // return true*
    sensor*.v_vollstop = 53;
    sensor.v_heater1 = 1 ;
    sensor.v_heater2 = 1 ;
    //sensor.v_air = 0 ;
    sensor.v_halten_aktiv = 320 ;
    v_st =
    _ String (i) +
    String (".") +_

    String (" sensor.v_temp_diff < “) +
    String (v_wertdb)
    _ ;_
    mySerial.println( v_st ) ;
    } <. v_wertdb ) {
    sensor.v_vollstop = 53;
    sensor.v_heater1 = 1 ;
    sensor.v_heater2 = 1 ;
    //sensor.v_air = 0 ;
    sensor.v_halten_aktiv = 320 ;
    v_st =
    _ String (i) +
    String (”.") +_

    String (" sensor.v_temp_diff <= ") +
    String (v_wertdb)
    _ ;_
    mySerial.println( v_st ) ;
    _ }*_

what type (unsigned?) are both variables defined as?

are you sure both values are -0.06000000000000000000000

No program should ever rely on comparing floats exactly, other than to zero.

Compilers are free to optimize floating point expressions in that knowledge.

I'm surprised that they're actually coming out different, seeing as you're setting them right there to a literal like that... but yeah, you can't trust floats... they are not exact...

Please edit your post to add code tags, as described in "How to use this forum".

Please post complete code.
Please use code tags.

What is this supposed to do? Does it even compile?

} <. v_wertdb    ) {

If you want to compare two floats, each with two decimals, you should multiply with 100 and cast to integer and do the comparison:

float f1 = 1.23;
float f2 = 3.21;

if ((int)(f1*100) == (int)(f2*100)) {}

Just be carefull with the selected integer type, roll over may become an issue with large numbers.

Actually, I found that it works “normally”:

float v_temp_diff = - 0.06;
float v_wertdb = -0.06;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  delay(200);
  Serial.println(v_temp_diff < v_wertdb);
  Serial.println(v_temp_diff <= v_wertdb);
}

void loop() {
}

prints:

0
1

The OP’s claim is false.

aarg:
The OP’s claim is false.

Not necessarily… The real problem may be related to the fact that “stream.print” only prints two decimals:

float f1 = 1.234;
float f2 = 1.233;

Serial.print(f1);
Serial.print(" equals ");
Serial.print(f2);
Serial.print(" = ");

if (f1 == f2) Serial.print("YES");
else Serial.print("NO");

Serial.println("!");

If either of the values being compared are not a constant (eg. a sensor reading), this may cause confusion.

the compiler has massive problems with float compare operations

float variable <= static not correct

float compare -0.06 < -0.06 is equal to -0.06 <= -0.06

float errror -0.06 < 0.0 returns false

please check float compare in compiler

please check constant 0.0 static in source translation in float variable

thanks

i doubt the GNU compiler which is used in industry (we used it at Qualcomm) has such an obvious failing.

can you post the code?

Please supply the code that you are concerned with.

Paul

Also here

sprintf doesn’t handle floats, so scaled ints displayed, but comparison is between floats

Test
    -60 >     -60  false
    -60 >=    -60  true
    -60 ==    -60  true
    -60 <=    -60  true
    -60 <     -60  false

    -60 >       0  false
    -60 >=      0  false
    -60 ==      0  false
    -60 <=      0  true
    -60 <       0  true
void test1 (
    float a,
    float b )
{
    char   s [80];

#define K   1000

    sprintf (s, " %6d >  %6d  %s", int(a*K), int(b*K), a >  b ? "true" : "false");
    Serial.println (s);

    sprintf (s, " %6d >= %6d  %s", int(a*K), int(b*K), a >= b ? "true" : "false");
    Serial.println (s);

    sprintf (s, " %6d == %6d  %s", int(a*K), int(b*K), a == b ? "true" : "false");
    Serial.println (s);

    sprintf (s, " %6d <= %6d  %s", int(a*K), int(b*K), a <= b ? "true" : "false");
    Serial.println (s);

    sprintf (s, " %6d <  %6d  %s", int(a*K), int(b*K), a < b ? "true" : "false");
    Serial.println (s);
    Serial.println ("");
}

// -----------------------------------------------------------------------------
void setup() {
    Serial.begin(9600);
    Serial.println("Test");

    test1 (-0.06, -0.06);
    test1 (-0.06,  0.00);
}


// -----------------------------------------------------------------------------
void
loop () {
}

Topics merged

Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting will result in a suspension from the forum.

In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

sircoolgandalf:
the compiler has massive problems with float compare operations

Nonsense. User error instead.

sircoolgandalf:
the compiler has massive problems with float compare operations

float variable <= static not correct

float compare -0.06 < -0.06 is equal to -0.06 <= -0.06

float errror -0.06 < 0.0 returns false

please check float compare in compiler

please check constant 0.0 static in source translation in float variable

thanks

I’m pretty sure you’ve been confused by the default print settings which show 2 decimal places only
for floats.

You values are perhaps 0.0648474 and 0.0605762, both print as 0.06 unless you specify more places,
so you think they are the same and they are not.

And as I said code should never rely on floating point equality other than for zero in some circumstances.
That’s because of rounding errors in the basic operations and inexact representation. For instance
0.1 * 10.0 doesn’t have to equal 1.0

https://floating-point-gui.de/errors/comparison/