Fixed bx::sign function to return tri-state result.

This commit is contained in:
Бранимир Караџић
2022-08-20 11:35:27 -07:00
parent a8b2d9ee4e
commit 7929f098f5
3 changed files with 13 additions and 2 deletions

View File

@@ -135,7 +135,7 @@ namespace bx
inline BX_CONSTEXPR_FUNC float sign(float _a)
{
return _a < 0.0f ? -1.0f : 1.0f;
return (0.0f < _a) - (0.0f > _a);
}
inline BX_CONSTEXPR_FUNC float abs(float _a)

View File

@@ -205,7 +205,11 @@ namespace bx
///
BX_CONSTEXPR_FUNC float invLerp(float _a, float _b, float _value);
/// Returns the sign of _a.
/// Extracts the sign of value `_a`.
///
/// @param[in] _a Value.
///
/// @returns -1 if `_a` less than zero, 0 if `_a` is equal to 0, or +1 if `_a` is greater than zero.
///
BX_CONSTEXPR_FUNC float sign(float _a);

View File

@@ -210,6 +210,13 @@ TEST_CASE("libm", "")
REQUIRE(bx::isEqual(bx::atan2(0.0f, 0.0f), ::atan2f(0.0f, 0.0f), 0.00001f) );
}
TEST_CASE("sign", "")
{
REQUIRE(-1 == bx::sign(-0.1389f) );
REQUIRE( 0 == bx::sign( 0.0000f) );
REQUIRE( 1 == bx::sign( 0.1389f) );
}
TEST_CASE("ToBits", "")
{
REQUIRE(UINT32_C(0x12345678) == bx::floatToBits( bx::bitsToFloat( UINT32_C(0x12345678) ) ) );