diff --git a/src/real.cpp b/src/real.cpp index e4fa64f1..77b5a37f 100644 --- a/src/real.cpp +++ b/src/real.cpp @@ -129,6 +129,9 @@ real real::operator +(real const &x) const int bigoff = (e1 - e2) / (sizeof(uint16_t) * 8); int off = e1 - e2 - bigoff * (sizeof(uint16_t) * 8); + if (bigoff > BIGITS) + return *this; + ret.m_signexp = m_signexp; uint32_t carry = 0; @@ -188,6 +191,9 @@ real real::operator -(real const &x) const int bigoff = (e1 - e2) / (sizeof(uint16_t) * 8); int off = e1 - e2 - bigoff * (sizeof(uint16_t) * 8); + if (bigoff > BIGITS) + return *this; + ret.m_signexp = m_signexp; int32_t carry = 0; @@ -501,6 +507,13 @@ real sqrt(real const &x) return ret * x; } +real fabs(real const &x) +{ + real ret = x; + ret.m_signexp &= 0x7fffffffu; + return ret; +} + real exp(real const &x) { int square = 0; @@ -529,6 +542,40 @@ real exp(real const &x) return ret; } +real sin(real const &x) +{ + real ret = 0.0, fact = 1.0, xn = x, x2 = x * x; + + for (int i = 1; ; i += 2) + { + real newret = ret + xn / fact; + if (ret == newret) + break; + ret = newret; + xn *= x2; + fact *= (real)(-(i + 1) * (i + 2)); + } + + return ret; +} + +real cos(real const &x) +{ + real ret = 0.0, fact = 1.0, xn = 1.0, x2 = x * x; + + for (int i = 1; ; i += 2) + { + real newret = ret + xn / fact; + if (ret == newret) + break; + ret = newret; + xn *= x2; + fact *= (real)(-i * (i + 1)); + } + + return ret; +} + void real::print(int ndigits) const { real const r1 = 1, r10 = 10; diff --git a/src/real.h b/src/real.h index 07ed8245..28b9533e 100644 --- a/src/real.h +++ b/src/real.h @@ -53,10 +53,15 @@ public: bool operator <=(real const &x) const; bool operator >=(real const &x) const; + friend real fabs(real const &x); + friend real fres(real const &x); friend real sqrt(real const &x); friend real exp(real const &x); + friend real sin(real const &x); + friend real cos(real const &x); + void print(int ndigits = 150) const; private: