Skip to content

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

Open
@dok-net

Description

@dok-net

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions