Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
dok-net opened this issue Jun 11, 2021 · 0 comments · May be fixed by #8116
Open

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

dok-net opened this issue Jun 11, 2021 · 0 comments · May be fixed by #8116

Comments

@dok-net
Copy link
Contributor

dok-net commented Jun 11, 2021

With regard to arduino/ArduinoCore-avr#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<uint32_t*>(&one));
	Serial.print("minusone = "); Serial.println(*reinterpret_cast<uint32_t*>(&minusone));
	Serial.print("zero = "); Serial.println(*reinterpret_cast<uint32_t*>(&zero));
	Serial.print("minuszero = "); Serial.println(*reinterpret_cast<uint32_t*>(&minuszero));

	float res;
	res = abstest(one);
	Serial.print("abs(one) = "); Serial.println(*reinterpret_cast<uint32_t*>(&res));
	res = abstest(minusone);
	Serial.print("abs(minusone) = "); Serial.println(*reinterpret_cast<uint32_t*>(&res));
	res = abstest(zero);
	Serial.print("abs(zero) = "); Serial.println(*reinterpret_cast<uint32_t*>(&res));
	res = abstest(minuszero);
	Serial.print("abs(minuszero) = "); Serial.println(*reinterpret_cast<uint32_t*>(&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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant