Files
core_math/tjp/core/math/Real.cpp
Timothy Prepscius 0807c0286a flatten 20260225
2026-02-25 12:36:47 -05:00

87 lines
1.6 KiB
C++
Executable File

//
// Math.cpp
// amoeba
//
// Created by Timothy Prepscius on 12/30/16.
// Copyright © 2016 Timothy Prepscius. All rights reserved.
//
#include "Math.hpp"
#include "Real.hpp"
#include "Real.inl"
#include <cmath>
namespace tjp {
namespace core {
namespace math {
// http://www.codecodex.com/wiki/Calculate_an_integer_square_root
template<typename T>
T IntSqrt(T v)
{
T remainder = v;
if (remainder < 0) { return 0; }
T one = 1;
T place = one <<(sizeof(T)*8 -2);
while (place > remainder) { place /= 4; }
T root = 0;
while (place)
{
if (remainder >= root + place)
{
remainder -= root + place;
root += place*2;
}
root /= 2;
place /= 4;
}
return root;
}
// float
template<>
u128 sqrt(const u128 &v)
{
return IntSqrt<u128>(v);
}
template<>
s128 sqrt(const s128 &v)
{
return IntSqrt<s128>(v);
}
template<>
const double tjp::core::math::constants<double>::PI = atan<double>(1)*4.0;
template<>
const float tjp::core::math::constants<float>::PI = atan<float>(1)*4.0;
template<>
const double tjp::core::math::constants<double>::TWO_PI = atan<double>(1)*8.0;
template<>
const float tjp::core::math::constants<float>::TWO_PI = atan<float>(1)*8.0;
template<>
const float tjp::core::math::constants<float>::DEGREES_TO_RADIANS = 4.0 / 180.0 * atan<float>(1);
template<>
const float tjp::core::math::constants<float>::RADIANS_TO_DEGREES = 180.0 / 4.0 / atan<float>(1);
template<>
const double tjp::core::math::constants<double>::DEGREES_TO_RADIANS = 4.0 / 180.0 * atan<double>(1);
template<>
const double tjp::core::math::constants<double>::RADIANS_TO_DEGREES = 180.0 / 4.0 / atan<double>(1);
} // namespace
} // namespace
} // namespace