Browse Source

sanitized square roots

devel
Ludovic 'Archivist' Lagouardette 4 years ago
parent
commit
e47731f1e5
1 changed files with 7 additions and 0 deletions
  1. +7
    -0
      include/gp/math.hpp

+ 7
- 0
include/gp/math.hpp View File

@ -13,9 +13,14 @@ namespace gp {
T lerp(T input, T low, T high) { T lerp(T input, T low, T high) {
return low + (high - low) * input; return low + (high - low) * input;
} }
template<typename T>
T lextrap(T input, T low, T high) {
return (input - low) / (high - low);
}
template<typename T, size_t fixed_passes = 16> template<typename T, size_t fixed_passes = 16>
T fixed_sqrt(T value) { T fixed_sqrt(T value) {
gp_config::assertion(value >= 0, "trying to compute square root of negative number");
if(value == 0) return 0; if(value == 0) return 0;
T ret = value / 2; T ret = value / 2;
T tmp; T tmp;
@ -30,6 +35,7 @@ namespace gp {
template<typename T, size_t cap_passes = 16> template<typename T, size_t cap_passes = 16>
T epsilon_sqrt(T value) { T epsilon_sqrt(T value) {
gp_config::assertion(value >= 0, "trying to compute square root of negative number");
if(value == 0) return 0; if(value == 0) return 0;
T ret = value / 2; T ret = value / 2;
T tmp; T tmp;
@ -52,6 +58,7 @@ namespace gp {
template<typename T, size_t cap_passes = 16> template<typename T, size_t cap_passes = 16>
T stable_sqrt(T value) { T stable_sqrt(T value) {
gp_config::assertion(value >= 0, "trying to compute square root of negative number");
if(value == 0) return 0; if(value == 0) return 0;
T ret = value / 2; T ret = value / 2;
T tmp; T tmp;

Loading…
Cancel
Save