Add an fsgn() function.

fsgn() returns -1/0/1 depending on the parameter's sign.
This commit is contained in:
King_DuckZ 2017-02-08 16:30:48 +00:00
parent 67e8ceefa3
commit 8e1c19df47
3 changed files with 72 additions and 0 deletions

View file

@ -70,6 +70,7 @@ add_executable(${PROJECT_NAME}
src/worldsizenotifiable.cpp
src/worlditems.cpp
src/moveable.cpp
src/fsgn.cpp
)
target_include_directories(${PROJECT_NAME} SYSTEM

47
src/fsgn.cpp Normal file
View file

@ -0,0 +1,47 @@
/*
Copyright 2016, 2017 Michele "King_DuckZ" Santullo
This file is part of MyCurry.
MyCurry is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
MyCurry is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with MyCurry. If not, see <http://www.gnu.org/licenses/>.
*/
#include "fsgn.hpp"
#include <cstdint>
namespace curry {
//float fsgn (float parIn) {
// if (parIn < 0.0f) return -1.0f;
// if (parIn > 0.0f) return 1.0f;
// return 0.0f;
//}
float fsgn (float parIn) {
static_assert(sizeof(uint32_t) == sizeof(float), "Unexpected float size");
union {
uint32_t i;
float f;
} in, r;
in.f = parIn;
if ((in.i & 0x7FFFFFFF) == 0) {
return 0.0f;
}
else {
r.f = 1.0f;
r.i |= in.i & 0x80000000;
return r.f;
}
}
} //namespace curry

24
src/fsgn.hpp Normal file
View file

@ -0,0 +1,24 @@
/*
Copyright 2016, 2017 Michele "King_DuckZ" Santullo
This file is part of MyCurry.
MyCurry is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
MyCurry is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with MyCurry. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
namespace curry {
float fsgn (float parIn);
} //namespace curry