esp8266 / esp8266/Arduino

abs() macro in Arduino.h inverts signedness of (positive) 0.0F

Open
#8,115 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
16.7k
Forks
13.1k
PR merge metrics
No merged PRs in 30d

Description

With regard to https://github.com/arduino/ArduinoCore-avr/pull/406,
I found out that the C macro defined in our `Arduino.h`, flips the signedness of 0.0F to -0.0F. I think it would be more natural, if one has to make a choice, to let the error (if it is considered as such) be such that abs() returns (negative) -0.0F for the input (negative) -0.0F but not flips the signedness of the positive 0.0F.

MCVE, abstest is an identical copy of the `abs()` macro in `Arduino.h`:
```
float one = 1.0F;
float minusone = -1.0F;
float zero = 0.0F;
float minuszero = -0.0F;

#define abstest(x) ({ __typeof__(x) _x = (x); _x > 0 ? _x : -_x; })

void setup()
{
Serial.begin(74880);
delay(500);
Serial.println("abstest");
Serial.print("one = "); Serial.println(*reinterpret_cast(&one));
Serial.print("minusone = "); Serial.println(*reinterpret_cast(&minusone));
Serial.print("zero = "); Serial.println(*reinterpret_cast(&zero));
Serial.print("minuszero = "); Serial.println(*reinterpret_cast(&minuszero));

float res;
res = abstest(one);
Serial.print("abs(one) = "); Serial.println(*reinterpret_cast(&res));
res = abstest(minusone);
Serial.print("abs(minusone) = "); Serial.println(*reinterpret_cast(&res));
res = abstest(zero);
Serial.print("abs(zero) = "); Serial.println(*reinterpret_cast(&res));
res = abstest(minuszero);
Serial.print("abs(minuszero) = "); Serial.println(*reinterpret_cast(&res));
}

void loop()
{
}
```

Output:
```
abstest
one = 1065353216
minusone = 3212836864
zero = 0
minuszero = 2147483648
abs(one) = 1065353216
abs(minusone) = 1065353216
abs(zero) = 2147483648
abs(minuszero) = 0
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.