/source/Real.cpp
https://github.com/denprog/BigNumbersParser · C++ · 2046 lines · 1029 code · 355 blank · 662 comment · 290 complexity · 9518f738564d58f077aadea9439d99c2 MD5 · raw file
- #include "stdafx.h"
- #include "Real.h"
- namespace BigNumbersParser
- {
- Real pi(const int precision);
- /**
- * Default constructor.
- */
- Real::Real()
- {
- mpfr_init2(number, mpfr_get_default_prec());
- mpfr_set_si(number, 0, GMP_RNDN);
- #ifdef TRACE_OUTPUT
- UpdateNumberStr();
- #endif
- }
- /**
- * Constructor.
- * @param precision The precision.
- */
- Real::Real(int precision)
- {
- mpfr_init2(number, max((int)mpfr_get_default_prec(), precision));
- //mpfr_init2(number, precision);
- mpfr_set_si(number, 0, GMP_RNDN);
- //addPrecision = 0;
- #ifdef TRACE_OUTPUT
- UpdateNumberStr();
- #endif
- }
- /**
- * Constructor from char*.
- * @param precision The precision.
- * @param num The number.
- */
- Real::Real(int precision, const char* num)
- {
- mpfr_init2(number, max((int)mpfr_get_default_prec(), precision));
- //mpfr_init2(number, strlen(num) + 1);
- mpfr_set_str(number, num, DEFAULT_BASE, MPFR_RNDZ);
- stringNumber = num;
- //mpfr_prec_round(number, precision / 2, GMP_RNDN);
- //addPrecision = (int)strchr(num, '.');
- //if (!addPrecision)
- // addPrecision = strlen(num);
- //else
- // addPrecision -= (int)num;
- #ifdef TRACE_OUTPUT
- UpdateNumberStr();
- #endif
- }
- /**
- * Constructor from int.
- * @param precision The precision.
- * @param num The number.
- */
- Real::Real(int precision, int num)
- {
- mpfr_init2(number, max((int)mpfr_get_default_prec(), precision));
- //mpfr_init2(number, precision);
- mpfr_set_si(number, num, GMP_RNDN);
- //addPrecision = 1;
- #ifdef TRACE_OUTPUT
- UpdateNumberStr();
- #endif
- }
- /**
- * Constructor from float.
- * @param precision The precision.
- * @param num The number.
- */
- Real::Real(int precision, float num)
- {
- mpfr_init2(number, max((int)mpfr_get_default_prec(), precision));
- //mpfr_init2(number, precision);
- mpfr_set_d(number, num, GMP_RNDN);
- //addPrecision = 1;
- #ifdef TRACE_OUTPUT
- UpdateNumberStr();
- #endif
- }
- /**
- * Constructor from string.
- * @param num The number.
- */
- Real::Real(const string& num)
- {
- stringNumber = num;
- mpfr_init2(number, max((int)mpfr_get_default_prec(), MathHelper::ToBitPrecision(num.length() * 2)) + 1);
- mpfr_set_str(number, num.c_str(), DEFAULT_BASE, MPFR_RNDA);
- SetPrecision(GetPrecision() + GetExp());
- #ifdef TRACE_OUTPUT
- UpdateNumberStr();
- #endif
- }
- /**
- * Copy constructor.
- * @param source The source number.
- */
- Real::Real(const Real& source)
- {
- mpfr_init2(number, source.GetBitPrecision());
- mpfr_set(number, source.number, GMP_RNDN);
- //addPrecision = source.addPrecision;
- stringNumber = source.stringNumber;
-
- #ifdef TRACE_OUTPUT
- UpdateNumberStr();
- #endif
- }
- /**
- * Destructor.
- */
- Real::~Real()
- {
- mpfr_clear(number);
- }
- /**
- * = operator.
- * @param source Source for the number.
- * @return The result number.
- */
- Real& Real::operator=(const Real& source)
- {
- if (this == &source)
- return *this;
- mpfr_clear(number);
- mpfr_init2(number, source.GetBitPrecision());
- mpfr_set(number, source.number, GMP_RNDN);
- //addPrecision = source.addPrecision;
- stringNumber = source.stringNumber;
- #ifdef TRACE_OUTPUT
- UpdateNumberStr();
- #endif
- return *this;
- }
- /**
- * = operator from int.
- * @param num The number.
- * @return The result number.
- */
- Real& Real::operator=(const int num)
- {
- mpfr_set_si(number, num, GMP_RNDN);
- //addPrecision = 1;
- #ifdef TRACE_OUTPUT
- UpdateNumberStr();
- #endif
- return *this;
- }
- /**
- * = operator from double.
- * @param num The number.
- * @return The result number.
- */
- Real& Real::operator=(const double num)
- {
- mpfr_set_d(number, num, GMP_RNDN);
- //addPrecision = 1;
- #ifdef TRACE_OUTPUT
- UpdateNumberStr();
- #endif
- return *this;
- }
- /**
- * = operator from char*.
- * @param num The number.
- * @return The result number.
- */
- Real& Real::operator=(const char* num)
- {
- mpfr_t t;
- mpfr_init2(t, GetBitPrecision());
- if (mpfr_set_str(t, num, DEFAULT_BASE, GMP_RNDN) == 0)
- {
- mpfr_set(number, t, GMP_RNDN);
- mpfr_clear(t);
- }
- else
- {
- mpfr_clear(t);
- }
- stringNumber = num;
- #ifdef TRACE_OUTPUT
- UpdateNumberStr();
- #endif
- return *this;
- }
- /**
- * = operator from std::string.
- * @param num The number.
- * @return The result number.
- */
- Real& Real::operator=(const string& num)
- {
- mpfr_t t;
- if ((int)num.length() + 1 > GetPrecision())
- {
- SetBitPrecision(MathHelper::ToBitPrecision(num.length() * 2));
- mpfr_init2(t, GetBitPrecision());
- }
- else
- mpfr_init2(t, GetBitPrecision());
- if (mpfr_set_str(t, num.c_str(), DEFAULT_BASE, GMP_RNDN) == 0)
- {
- mpfr_set(number, t, GMP_RNDN);
- mpfr_clear(t);
- }
- else
- {
- mpfr_clear(t);
- }
- stringNumber = num;
- #ifdef TRACE_OUTPUT
- UpdateNumberStr();
- #endif
- return *this;
- }
- /**
- * + operator.
- * @return The result number.
- */
- Real Real::operator+()
- {
- return *this;
- }
- /**
- * - operator.
- * @return The result number.
- */
- Real Real::operator-()
- {
- Real res(GetBitPrecision());
- mpfr_neg(res.number, number, GMP_RNDN);
- return res;
- }
- /**
- * ++ operator.
- * @return The result number.
- */
- Real Real::operator++()
- {
- *this += 1;
- return *this;
- }
- /**
- * -- operator.
- * @return The result number.
- */
- Real Real::operator--()
- {
- *this -= 1;
- return *this;
- }
- /**
- * Addition operator.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num1 The first value.
- * @param num2 A value to add to it.
- * @return The result of the operation.
- */
- Real operator+(const Real& num1, const Real& num2)
- {
- Real res(max(num1.GetBitPrecision() + 2, num2.GetBitPrecision()) + 2);
- while (mpfr_add(res.number, num1.number, num2.number, DEFAULT_RND) != 0)
- {
- if (res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION >= MPFR_PREC_MAX)
- throw MathException(Overflow);
- res.SetBitPrecision(res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION);
- }
- return res;
- }
- /**
- * Addition operator.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num1 The first value.
- * @param num2 A value to add to it.
- * @return The result of the operation.
- */
- Real operator+(const Real& num1, const int num2)
- {
- Real res(num1.GetBitPrecision());
- while (mpfr_add_si(res.number, num1.number, num2, DEFAULT_RND) != 0)
- {
- if (res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION >= MPFR_PREC_MAX)
- throw MathException(Overflow);
- res.SetBitPrecision(res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION);
- }
- return res;
- }
- /**
- * Addition operator.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num1 The first value.
- * @param num2 A value to add to it.
- * @return The result of the operation.
- */
- Real operator+(const int num1, const Real& num2)
- {
- Real res(num2.GetBitPrecision());
- while (mpfr_add_si(res.number, num2.number, num1, DEFAULT_RND) != 0)
- {
- if (res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION >= MPFR_PREC_MAX)
- throw MathException(Overflow);
- res.SetBitPrecision(res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION);
- }
- return res;
- }
- /**
- * Addition operator.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num1 The first value.
- * @param num2 A value to add to it.
- * @return The result of the operation.
- */
- Real operator+(const Real& num1, const float num2)
- {
- Real res(num1.GetBitPrecision());
- while (mpfr_add_d(res.number, num1.number, num2, DEFAULT_RND) != 0)
- {
- if (res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION >= MPFR_PREC_MAX)
- throw MathException(Overflow);
- res.SetBitPrecision(res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION);
- }
- return res;
- }
- /**
- * Addition operator.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num1 The first value.
- * @param num2 A value to add to it.
- * @return The result of the operation.
- */
- Real operator+(const float num1, const Real& num2)
- {
- Real res(num2.GetBitPrecision());
- while (mpfr_add_d(res.number, num2.number, num1, DEFAULT_RND) != 0)
- {
- if (res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION >= MPFR_PREC_MAX)
- throw MathException(Overflow);
- res.SetBitPrecision(res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION);
- }
- return res;
- }
- /**
- * Subtraction operator.
- * @param num1 The first value.
- * @param num2 A value to subtract from it.
- * @return The result of the operation.
- */
- Real operator-(const Real& num1, const Real& num2)
- {
- Real res(max(num1.GetBitPrecision(), num2.GetBitPrecision()) + 1);
- mpfr_sub(res.number, num1.number, num2.number, GMP_RNDN);
- #ifdef TRACE_OUTPUT
- res.UpdateNumberStr();
- #endif
- return res;
- }
- /**
- * Subtraction operator.
- * @param num1 The first value.
- * @param num2 A value to subtract from it.
- * @return The result of the operation.
- */
- Real operator-(const Real& num1, const int num2)
- {
- Real res(num1.GetBitPrecision());
- mpfr_sub_si(res.number, num1.number, num2, DEFAULT_RND);
- return res;
- }
- /**
- * Subtraction operator.
- * @param num1 The first value.
- * @param num2 A value to subtract from it.
- * @return The result of the operation.
- */
- Real operator-(const int num1, const Real& num2)
- {
- Real res(num2.GetBitPrecision());
- mpfr_si_sub(res.number, num1, num2.number, DEFAULT_RND);
- return res;
- }
- /**
- * Subtraction operator.
- * @param num1 The first value.
- * @param num2 A value to subtract from it.
- * @return The result of the operation.
- */
- Real operator-(const Real& num1, const float num2)
- {
- Real res(num1.GetBitPrecision());
- mpfr_sub_d(res.number, num1.number, num2, DEFAULT_RND);
- return res;
- }
- /**
- * Subtraction operator.
- * @param num1 The first value.
- * @param num2 A value to subtract from it.
- * @return The result of the operation.
- */
- Real operator-(const float num1, const Real& num2)
- {
- Real res(num2.GetBitPrecision());
- mpfr_d_sub(res.number, num1, num2.number, DEFAULT_RND);
- return res;
- }
- /**
- * Multiplication operator.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num1 The first value to multiply.
- * @param num2 The second value to multiply.
- * @return The result of the operation.
- */
- Real operator*(const Real& num1, const Real& num2)
- {
- Real res(max(num1.GetBitPrecision() * 2, num2.GetBitPrecision()) * 2);
- while (mpfr_mul(res.number, num1.number, num2.number, DEFAULT_RND) != 0)
- {
- if (res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION >= MPFR_PREC_MAX)
- throw MathException(Overflow);
- res.SetBitPrecision(res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION);
- }
- return res;
- }
- /**
- * Multiplication operator.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num1 The first value to multiply.
- * @param num2 The second value to multiply.
- * @return The result of the operation.
- */
- Real operator*(const Real& num1, const int num2)
- {
- Real res(num1.GetBitPrecision());
- while (mpfr_mul_si(res.number, num1.number, num2, DEFAULT_RND) != 0)
- {
- if (res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION >= MPFR_PREC_MAX)
- throw MathException(Overflow);
- res.SetBitPrecision(res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION);
- }
- return res;
- }
- /**
- * Multiplication operator.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num1 The first value to multiply.
- * @param num2 The second value to multiply.
- * @return The result of the operation.
- */
- Real operator*(const int num1, const Real& num2)
- {
- Real res(num2.GetBitPrecision());
- while (mpfr_mul_si(res.number, num2.number, num1, DEFAULT_RND) != 0)
- {
- if (res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION >= MPFR_PREC_MAX)
- throw MathException(Overflow);
- res.SetBitPrecision(res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION);
- }
- return res;
- }
- /**
- * Multiplication operator.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num1 The first value to multiply.
- * @param num2 The second value to multiply.
- * @return The result of the operation.
- */
- Real operator*(const Real& num1, const float num2)
- {
- Real res(num1.GetBitPrecision());
- while (mpfr_mul_d(res.number, num1.number, num2, DEFAULT_RND) != 0)
- {
- if (res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION >= MPFR_PREC_MAX)
- throw MathException(Overflow);
- res.SetBitPrecision(res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION);
- }
- return res;
- }
- /**
- * Multiplication operator.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num1 The first value to multiply.
- * @param num2 The second value to multiply.
- * @return The result of the operation.
- */
- Real operator*(const float num1, const Real& num2)
- {
- Real res(num2.GetBitPrecision());
- while (mpfr_mul_d(res.number, num2.number, num1, DEFAULT_RND) != 0)
- {
- if (res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION >= MPFR_PREC_MAX)
- throw MathException(Overflow);
- res.SetBitPrecision(res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION);
- }
- return res;
- }
- /**
- * Division operator.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num1 The numerator.
- * @param num2 The denominator.
- * @return The result of the operation.
- */
- Real operator/(const Real& num1, const Real& num2)
- {
- Real res(max(num1.GetBitPrecision() + 2, num2.GetBitPrecision() + 2));
- mpfr_div(res.number, num1.number, num2.number, GMP_RNDN);
- if (res.IsInfinity() || res.IsNaN())
- throw MathException(DivisionByZero);
- #ifdef TRACE_OUTPUT
- res.UpdateNumberStr();
- #endif
- return res;
- }
- /**
- * Division operator.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num1 The numerator.
- * @param num2 The denominator.
- * @return The result of the operation.
- */
- Real operator/(const Real& num1, const int num2)
- {
- Real res(num1.GetBitPrecision());
- mpfr_div_si(res.number, num1.number, num2, DEFAULT_RND);
- if (res.IsInfinity() || res.IsNaN())
- throw MathException(Overflow);
- return res;
- }
- /**
- * Division operator.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num1 The numerator.
- * @param num2 The denominator.
- * @return The result of the operation.
- */
- Real operator/(const int num1, const Real& num2)
- {
- Real res(num2.GetBitPrecision());
- mpfr_si_div(res.number, num1, num2.number, DEFAULT_RND);
- if (res.IsInfinity() || res.IsNaN())
- throw MathException(Overflow);
- return res;
- }
- /**
- * Division operator.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num1 The numerator.
- * @param num2 The denominator.
- * @return The result of the operation.
- */
- Real operator/(const Real& num1, const float num2)
- {
- Real res(num1.GetBitPrecision());
- mpfr_div_d(res.number, num1.number, num2, DEFAULT_RND);
- if (res.IsInfinity() || res.IsNaN())
- throw MathException(Overflow);
- return res;
- }
- /**
- * Division operator.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num1 The numerator.
- * @param num2 The denominator.
- * @return The result of the operation.
- */
- Real operator/(const float num1, const Real& num2)
- {
- Real res(num2.GetBitPrecision());
- mpfr_d_div(res.number, num1, num2.number, DEFAULT_RND);
- if (res.IsInfinity() || res.IsNaN())
- throw MathException(Overflow);
- return res;
- }
-
- /**
- * += operator.
- * @param num A value to add to it.
- */
- void Real::operator+=(const Real& num)
- {
- *this = *this + num;
- }
- /**
- * += operator.
- * @param num A value to add to it.
- */
- void Real::operator+=(const int num)
- {
- *this = *this + num;
- }
- /**
- * += operator.
- * @param num A value to add to it.
- */
- void Real::operator+=(const float num)
- {
- *this = *this + num;
- }
- /**
- * -= operator.
- * @param num A value to subtract from it.
- */
- void Real::operator-=(const Real& num)
- {
- *this = *this - num;
- }
- /**
- * -= operator.
- * @param num A value to subtract from it.
- */
- void Real::operator-=(const int num)
- {
- *this = *this - num;
- }
- /**
- * -= operator.
- * @param num A value to subtract from it.
- */
- void Real::operator-=(const float num)
- {
- *this = *this - num;
- }
- /**
- * *= operator.
- * @param num A value to multiply.
- */
- void Real::operator*=(const Real& num)
- {
- *this = *this * num;
- }
- /**
- * *= operator.
- * @param num A value to multiply.
- */
- void Real::operator*=(const int num)
- {
- *this = *this * num;
- }
- /**
- * *= operator.
- * @param num A value to multiply.
- */
- void Real::operator*=(const float num)
- {
- *this = *this * num;
- }
- /**
- * /= operator.
- * @param num The denominator.
- */
- void Real::operator/=(const Real& num)
- {
- *this = *this / num;
- }
- /**
- * /= operator.
- * @param num The denominator.
- */
- void Real::operator/=(const int num)
- {
- *this = *this / num;
- }
- /**
- * /= operator.
- * @param num The denominator.
- */
- void Real::operator/=(const float num)
- {
- *this = *this / num;
- }
- /**
- * Gets the int.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @return number of type int.
- */
- Real::operator int() const
- {
- if (IsInteger())
- return mpfr_get_si(number, DEFAULT_RND);
- else
- throw MathException(ConversionDoesNotFit);
- }
- /**
- * Equality operator.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num1 The first instance to compare.
- * @param num2 The second instance to compare.
- * @return true if the parameters are considered equivalent.
- */
- bool operator==(const Real& num1, const Real& num2)
- {
- if (num1.IsNaN() || num2.IsNaN())
- throw MathException(Overflow);
- return mpfr_equal_p(num1.number, num2.number) != 0;
- }
- /**
- * Equality operator.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num1 The first instance to compare.
- * @param num2 The second instance to compare.
- * @return true if the parameters are considered equivalent.
- */
- bool operator==(const Real& num1, const int num2)
- {
- if (num1.IsNaN())
- throw MathException(Overflow);
- return mpfr_cmp_si(num1.number, num2) == 0;
- }
- /**
- * Equality operator.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num1 The first instance to compare.
- * @param num2 The second instance to compare.
- * @return true if the parameters are considered equivalent.
- */
- bool operator==(const int num1, const Real& num2)
- {
- if (num2.IsNaN())
- throw MathException(Overflow);
- return mpfr_cmp_si(num2.number, num1) == 0;
- }
- /**
- * Equality operator.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num1 The first instance to compare.
- * @param num2 The second instance to compare.
- * @return true if the parameters are considered equivalent.
- */
- bool operator==(const Real& num1, const float num2)
- {
- if (num1.IsNaN())
- throw MathException(Overflow);
- return mpfr_cmp_d(num1.number, num2) == 0;
- }
- /**
- * Equality operator.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num1 The first instance to compare.
- * @param num2 The second instance to compare.
- * @return true if the parameters are considered equivalent.
- */
- bool operator==(const float num1, const Real& num2)
- {
- if (num2.IsNaN())
- throw MathException(Overflow);
- return mpfr_cmp_d(num2.number, num1) == 0;
- }
- /**
- * Equality operator.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num1 The first instance to compare.
- * @param num2 The second instance to compare.
- * @return true if the parameters are considered equivalent.
- */
- bool operator==(const Real& num1, const double num2)
- {
- if (num1.IsNaN())
- throw MathException(Overflow);
- return mpfr_cmp_d(num1.number, num2) == 0;
- }
- /**
- * Equality operator.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num1 The first instance to compare.
- * @param num2 The second instance to compare.
- * @return true if the parameters are considered equivalent.
- */
- bool operator==(const double num1, const Real& num2)
- {
- if (num2.IsNaN())
- throw MathException(Overflow);
- return mpfr_cmp_d(num2.number, num1) == 0;
- }
- /**
- * Equality operator.
- * @param num1 The first instance to compare.
- * @param num2 The second instance to compare.
- * @return true if the parameters are considered equivalent.
- */
- bool operator==(const Real& num1, const char* num2)
- {
- return num1 == Real(num1.GetBitPrecision(), num2);
- }
- /**
- * Equality operator.
- * @param num1 The first instance to compare.
- * @param num2 The second instance to compare.
- * @return true if the parameters are considered equivalent.
- */
- bool operator==(const char* num1, const Real& num2)
- {
- return Real(num2.GetBitPrecision(), num1) == num2;
- }
- /**
- * Not equality operator.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num1 The first instance to compare.
- * @param num2 The second instance to compare.
- * @return true if the parameters are not considered equivalent.
- */
- bool operator!=(const Real& num1, const Real& num2)
- {
- if (num1.IsNaN() || num2.IsNaN())
- throw MathException(Overflow);
- return mpfr_equal_p(num1.number, num2.number) == 0;
- }
- /**
- * Not equality operator.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num1 The first instance to compare.
- * @param num2 The second instance to compare.
- * @return true if the parameters are not considered equivalent.
- */
- bool operator!=(const Real& num1, const int num2)
- {
- if (num1.IsNaN())
- throw MathException(Overflow);
- return mpfr_cmp_si(num1.number, num2) != 0;
- }
- /**
- * Not equality operator.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num1 The first instance to compare.
- * @param num2 The second instance to compare.
- * @return true if the parameters are not considered equivalent.
- */
- bool operator!=(const int num1, const Real& num2)
- {
- if (num2.IsNaN())
- throw MathException(Overflow);
- return mpfr_cmp_si(num2.number, num1) != 0;
- }
- /**
- * Not equality operator.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num1 The first instance to compare.
- * @param num2 The second instance to compare.
- * @return true if the parameters are not considered equivalent.
- */
- bool operator!=(const Real& num1, const float num2)
- {
- if (num1.IsNaN())
- throw MathException(Overflow);
- return mpfr_cmp_d(num1.number, num2) != 0;
- }
- /**
- * Not equality operator.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num1 The first instance to compare.
- * @param num2 The second instance to compare.
- * @return true if the parameters are not considered equivalent.
- */
- bool operator!=(const float num1, const Real& num2)
- {
- if (num2.IsNaN())
- throw MathException(Overflow);
- return mpfr_cmp_d(num2.number, num1) != 0;
- }
- /**
- * Greater-than comparison operator.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num1 The first instance to compare.
- * @param num2 The second instance to compare.
- * @return true if the first parameter is greater than to the second.
- */
- bool operator>(const Real& num1, const Real& num2)
- {
- if (num1.IsNaN() || num2.IsNaN())
- throw MathException(Overflow);
- return mpfr_greater_p(num1.number, num2.number) != 0;
- }
- /**
- * Greater-than comparison operator.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num1 The first instance to compare.
- * @param num2 The second instance to compare.
- * @return true if the first parameter is greater than to the second.
- */
- bool operator>(const Real& num1, const int num2)
- {
- if (num1.IsNaN())
- throw MathException(Overflow);
- return mpfr_cmp_si(num1.number, num2) > 0;
- }
- /**
- * Greater-than comparison operator.
- * @param num1 The first instance to compare.
- * @param num2 The second instance to compare.
- * @return true if the first parameter is greater than to the second.
- */
- bool operator>(const int num1, const Real& num2)
- {
- Real _num1(num2.GetBitPrecision(), num1);
- return _num1 > num2;
- }
- /**
- * Greater-than comparison operator.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num1 The first instance to compare.
- * @param num2 The second instance to compare.
- * @return true if the first parameter is greater than to the second.
- */
- bool operator>(const Real& num1, const float num2)
- {
- if (num1.IsNaN())
- throw MathException(Overflow);
- return mpfr_cmp_d(num1.number, num2) > 0;
- }
- /**
- * Greater-than comparison operator.
- * @param num1 The first instance to compare.
- * @param num2 The second instance to compare.
- * @return true if the first parameter is greater than to the second.
- */
- bool operator>(const float num1, const Real& num2)
- {
- Real _num1(num2.GetBitPrecision(), num1);
- return _num1 > num2;
- }
- /**
- * Greater-than-or-equal comparison operator.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num1 The first instance to compare.
- * @param num2 The second instance to compare.
- * @return true if the first parameter is greater than or equal to the second.
- */
- bool operator>=(const Real& num1, const Real& num2)
- {
- if (num1.IsNaN() || num2.IsNaN())
- throw MathException(Overflow);
- return mpfr_greaterequal_p(num1.number, num2.number) != 0;
- }
- /**
- * Greater-than-or-equal comparison operator.
- * @param num1 The first instance to compare.
- * @param num2 The second instance to compare.
- * @return true if the first parameter is greater than or equal to the second.
- */
- bool operator>=(const Real& num1, const int num2)
- {
- return (num1 > num2) || (num1 == num2);
- }
- /**
- * Greater-than-or-equal comparison operator.
- * @param num1 The first instance to compare.
- * @param num2 The second instance to compare.
- * @return true if the first parameter is greater than or equal to the second.
- */
- bool operator>=(const int num1, const Real& num2)
- {
- return (num1 > num2) || (num1 == num2);
- }
- /**
- * Greater-than-or-equal comparison operator.
- * @param num1 The first instance to compare.
- * @param num2 The second instance to compare.
- * @return true if the first parameter is greater than or equal to the second.
- */
- bool operator>=(const Real& num1, const float num2)
- {
- return (num1 > num2) || (num1 == num2);
- }
- /**
- * Greater-than-or-equal comparison operator.
- * @param num1 The first instance to compare.
- * @param num2 The second instance to compare.
- * @return true if the first parameter is greater than or equal to the second.
- */
- bool operator>=(const float num1, const Real& num2)
- {
- return (num1 > num2) || (num1 == num2);
- }
- /**
- * Less-than comparison operator.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num1 The first instance to compare.
- * @param num2 The second instance to compare.
- * @return true if the first parameter is less than the second.
- */
- bool operator<(const Real& num1, const Real& num2)
- {
- if (num1.IsNaN() || num2.IsNaN())
- throw MathException(Overflow);
- return mpfr_less_p(num1.number, num2.number) != 0;
- }
- /**
- * Less-than comparison operator.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num1 The first instance to compare.
- * @param num2 The second instance to compare.
- * @return true if the first parameter is less than the second.
- */
- bool operator<(const Real& num1, const int num2)
- {
- if (num1.IsNaN())
- throw MathException(Overflow);
- return mpfr_cmp_si(num1.number, num2) < 0;
- }
- /**
- * Less-than comparison operator.
- * @param num1 The first instance to compare.
- * @param num2 The second instance to compare.
- * @return true if the first parameter is less than the second.
- */
- bool operator<(const int num1, const Real& num2)
- {
- Real _num1(num2.GetBitPrecision(), num1);
- return _num1 < num2;
- }
- /**
- * Less-than comparison operator.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num1 The first instance to compare.
- * @param num2 The second instance to compare.
- * @return true if the first parameter is less than the second.
- */
- bool operator<(const Real& num1, const float num2)
- {
- if (num1.IsNaN())
- throw MathException(Overflow);
- return mpfr_cmp_d(num1.number, num2) < 0;
- }
- /**
- * Less-than comparison operator.
- * @param num1 The first instance to compare.
- * @param num2 The second instance to compare.
- * @return true if the first parameter is less than the second.
- */
- bool operator<(const float num1, const Real& num2)
- {
- Real _num1(num2.GetBitPrecision(), num1);
- return _num1 < num2;
- }
- /**
- * Less-than-or-equal comparison operator.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num1 The first instance to compare.
- * @param num2 The second instance to compare.
- * @return true if the first parameter is less than or equal to the second.
- */
- bool operator<=(const Real& num1, const Real& num2)
- {
- if (num1.IsNaN() || num2.IsNaN())
- throw MathException(Overflow);
- return mpfr_lessequal_p(num1.number, num2.number) != 0;
- }
- /**
- * Less-than-or-equal comparison operator.
- * @param num1 The first instance to compare.
- * @param num2 The second instance to compare.
- * @return true if the first parameter is less than or equal to the second.
- */
- bool operator<=(const Real& num1, const int num2)
- {
- return (num1 < num2) || (num1 == num2);
- }
- /**
- * Less-than-or-equal comparison operator.
- * @param num1 The first instance to compare.
- * @param num2 The second instance to compare.
- * @return true if the first parameter is less than or equal to the second.
- */
- bool operator<=(const int num1, const Real& num2)
- {
- return (num1 < num2) || (num1 == num2);
- }
- /**
- * Less-than-or-equal comparison operator.
- * @param num1 The first instance to compare.
- * @param num2 The second instance to compare.
- * @return true if the first parameter is less than or equal to the second.
- */
- bool operator<=(const Real& num1, const float num2)
- {
- return (num1 < num2) || (num1 == num2);
- }
- /**
- * Less-than-or-equal comparison operator.
- * @param num1 The first instance to compare.
- * @param num2 The second instance to compare.
- * @return true if the first parameter is less than or equal to the second.
- */
- bool operator<=(const float num1, const Real& num2)
- {
- return (num1 < num2) || (num1 == num2);
- }
- /**
- * Exponent function.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num The argument.
- * @return The number raised to the exponent.
- */
- Real exp(const Real& num)
- {
- Real res(num.GetBitPrecision());
- while (mpfr_exp(res.number, num.number, DEFAULT_RND) < 0)
- {
- if (res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION >= MPFR_PREC_MAX)
- throw MathException(Overflow);
- res.SetBitPrecision(res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION);
- }
- //mpfr_exp(res.number, num.number, DEFAULT_RND);
- return res;
- }
- /**
- * Natural logarithm function.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num The argument.
- * @return The natural logarithm of the number.
- */
- Real ln(const Real& num)
- {
- Real res(num.GetBitPrecision());
- while (mpfr_log(res.number, num.number, DEFAULT_RND) < 0)
- {
- if (res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION >= MPFR_PREC_MAX)
- throw MathException(Overflow);
- res.SetBitPrecision(res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION);
- }
- //mpfr_log(res.number, num.number, DEFAULT_RND);
- return res;
- }
- /**
- * Base 10 logarithm function.
- * @param num The argument.
- * @return Base 10 logarithm of the number.
- */
- Real lg(const Real& num)
- {
- Real res = ln(num) / ln(Real(num.GetBitPrecision(), 10));
- return res;
- }
- /**
- * Logarithm with base function.
- * @param num1 The base.
- * @param num2 The argument.
- * @return Logarithm with base of the number.
- */
- Real log(const Real& num1, const Real& num2)
- {
- Real res = ln(num2) / ln(num1);
- return res;
- }
- /**
- * Sine function.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num The argument.
- * @param angleMeasure The angle measure.
- * @return The sinus of the number.
- */
- Real sin(const Real& num, AngleMeasure angleMeasure)
- {
- Real res(num.GetBitPrecision());
- if (angleMeasure != RADIAN)
- {
- Real _num(num.GetBitPrecision());
- if (angleMeasure == DEGREE)
- _num = num.DegreeToRadian();
- else if (angleMeasure == GRAD)
- _num = num.GradToRadian();
- while (mpfr_sin(res.number, _num.number, DEFAULT_RND) < 0)
- {
- if (res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION >= MPFR_PREC_MAX)
- throw MathException(Overflow);
- res.SetBitPrecision(res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION);
- }
- return res;
- }
- Real misc = MathHelper::GetMisc<Real>(num);
- Real _pi = pi(num.GetBitPrecision());
- Real pi2 = pi(num.GetBitPrecision()) * 2;
- Real _num = num;
- Real mul = round(_num / pi2);
- _num -= mul * pi2;
- _num = abs(_num);
- if (_num <= misc)
- {
- res = 0;
- }
- else if (abs(_num - _pi) <= misc)
- {
- res = 0;
- }
- else
- {
- while (mpfr_sin(res.number, num.number, DEFAULT_RND) < 0)
- {
- if (res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION >= MPFR_PREC_MAX)
- throw MathException(Overflow);
- res.SetBitPrecision(res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION);
- }
- }
- return res;
- }
- /**
- * Cosine function.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num The argument.
- * @param angleMeasure The angle measure.
- * @return The cosine of the number.
- */
- Real cos(const Real& num, AngleMeasure angleMeasure)
- {
- Real res(num.GetBitPrecision());
- if (angleMeasure != RADIAN)
- {
- Real _num(num.GetBitPrecision());
- if (angleMeasure == DEGREE)
- _num = num.DegreeToRadian();
- else if (angleMeasure == GRAD)
- _num = num.GradToRadian();
- while (mpfr_cos(res.number, _num.number, DEFAULT_RND) < 0)
- {
- if (res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION >= MPFR_PREC_MAX)
- throw MathException(Overflow);
- res.SetBitPrecision(res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION);
- }
- return res;
- }
- Real _pi = pi(num.GetBitPrecision());
- Real pi2 = pi(num.GetBitPrecision()) * 2;
- Real mul = round(num / pi2);
- Real _num = num;
- _num -= mul * pi2;
- _num = abs(_num);
- if (_num == 0)
- {
- res = 1;
- }
- else if (_num == _pi)
- {
- res = -1;
- }
- else if (_num == _pi / 2)
- {
- res = 0;
- }
- else if (_num == 3 * _pi / 2)
- {
- res = 0;
- }
- else
- {
- while (mpfr_cos(res.number, num.number, DEFAULT_RND) < 0)
- {
- if (res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION >= MPFR_PREC_MAX)
- throw MathException(Overflow);
- res.SetBitPrecision(res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION);
- }
- }
- return res;
- }
- /**
- * Tangent function.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num The argument.
- * @param angleMeasure The angle measure.
- * @return The tangent of the number.
- */
- Real tg(const Real& num, AngleMeasure angleMeasure)
- {
- Real res(num.GetBitPrecision());
- if (angleMeasure != RADIAN)
- {
- Real _num(num.GetBitPrecision());
- if (angleMeasure == DEGREE)
- _num = num.DegreeToRadian();
- else if (angleMeasure == GRAD)
- _num = num.GradToRadian();
- while (mpfr_tan(res.number, _num.number, DEFAULT_RND) < 0)
- {
- if (res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION >= MPFR_PREC_MAX)
- throw MathException(Overflow);
- res.SetBitPrecision(res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION);
- }
- return res;
- }
- while (mpfr_tan(res.number, num.number, DEFAULT_RND) < 0)
- {
- if (res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION >= MPFR_PREC_MAX)
- throw MathException(Overflow);
- res.SetBitPrecision(res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION);
- }
- if (res.IsInfinity() || res.IsNaN())
- throw MathException(Overflow);
- return res;
- }
- /**
- * Cotangent function.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num The argument.
- * @param angleMeasure The angle measure.
- * @return The cotangent of the number.
- */
- Real ctg(const Real& num, AngleMeasure angleMeasure)
- {
- Real res(num.GetBitPrecision());
- if (angleMeasure != RADIAN)
- {
- Real _num(num.GetBitPrecision());
- if (angleMeasure == DEGREE)
- _num = num.DegreeToRadian();
- else if (angleMeasure == GRAD)
- _num = num.GradToRadian();
- while (mpfr_cot(res.number, _num.number, DEFAULT_RND) < 0)
- {
- if (res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION >= MPFR_PREC_MAX)
- throw MathException(Overflow);
- res.SetBitPrecision(res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION);
- }
- return res;
- }
- while (mpfr_cot(res.number, num.number, DEFAULT_RND) < 0)
- {
- if (res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION >= MPFR_PREC_MAX)
- throw MathException(Overflow);
- res.SetBitPrecision(res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION);
- }
- if (res.IsInfinity() || res.IsNaN())
- throw MathException(Overflow);
- return res;
- }
- /**
- * Secant function.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num The argument.
- * @param angleMeasure The angle measure.
- * @return The secant of the number.
- */
- Real sec(const Real& num, AngleMeasure angleMeasure)
- {
- Real res(num.GetBitPrecision());
- if (angleMeasure != RADIAN)
- {
- Real _num(num.GetBitPrecision());
- if (angleMeasure == DEGREE)
- _num = num.DegreeToRadian();
- else if (angleMeasure == GRAD)
- _num = num.GradToRadian();
- while (mpfr_sec(res.number, _num.number, DEFAULT_RND) < 0)
- {
- if (res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION >= MPFR_PREC_MAX)
- throw MathException(Overflow);
- res.SetBitPrecision(res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION);
- }
- return res;
- }
- while (mpfr_sec(res.number, num.number, DEFAULT_RND) < 0)
- {
- if (res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION >= MPFR_PREC_MAX)
- throw MathException(Overflow);
- res.SetBitPrecision(res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION);
- }
- return res;
- }
- /**
- * Cosecant function.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num The argument.
- * @param angleMeasure The angle measure.
- * @return The cosecant of the number.
- */
- Real cosec(const Real& num, AngleMeasure angleMeasure)
- {
- Real res(num.GetBitPrecision());
- if (angleMeasure != RADIAN)
- {
- Real _num(num.GetBitPrecision());
- if (angleMeasure == DEGREE)
- _num = num.DegreeToRadian();
- else if (angleMeasure == GRAD)
- _num = num.GradToRadian();
- while (mpfr_csc(res.number, _num.number, DEFAULT_RND) < 0)
- {
- if (res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION >= MPFR_PREC_MAX)
- throw MathException(Overflow);
- res.SetBitPrecision(res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION);
- }
- return res;
- }
- while (mpfr_csc(res.number, num.number, DEFAULT_RND) < 0)
- {
- if (res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION >= MPFR_PREC_MAX)
- throw MathException(Overflow);
- res.SetBitPrecision(res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION);
- }
- return res;
- }
- /**
- * Arcsine function.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num The argument.
- * @param angleMeasure The angle measure.
- * @return The arcsine of the number.
- */
- Real arcsin(const Real& num, AngleMeasure angleMeasure)
- {
- Real res(num.GetBitPrecision());
- if (abs(num) > 1)
- throw MathException(Overflow);
- while (mpfr_asin(res.number, num.number, DEFAULT_RND) < 0)
- {
- if (res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION >= MPFR_PREC_MAX)
- throw MathException(Overflow);
- res.SetBitPrecision(res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION);
- }
- if (angleMeasure == DEGREE)
- res = res.RadianToDegree();
- else if (angleMeasure == GRAD)
- res = res.RadianToGrad();
- return res;
- }
- /**
- * Arccosine function.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num The argument.
- * @param angleMeasure The angle measure.
- * @return The arccosine of the number.
- */
- Real arccos(const Real& num, AngleMeasure angleMeasure)
- {
- Real res(num.GetBitPrecision());
- if (abs(num) > 1)
- throw MathException(Overflow);
- while (mpfr_acos(res.number, num.number, DEFAULT_RND) < 0)
- {
- if (res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION >= MPFR_PREC_MAX)
- throw MathException(Overflow);
- res.SetBitPrecision(res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION);
- }
- if (angleMeasure == DEGREE)
- res = res.RadianToDegree();
- else if (angleMeasure == GRAD)
- res = res.RadianToGrad();
- return res;
- }
- /**
- * Arctangent function.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num The argument.
- * @param angleMeasure The angle measure.
- * @return The arctangent of the number.
- */
- Real arctg(const Real& num, AngleMeasure angleMeasure)
- {
- Real res(num.GetBitPrecision());
- while (mpfr_atan(res.number, num.number, DEFAULT_RND) < 0)
- {
- if (res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION >= MPFR_PREC_MAX)
- throw MathException(Overflow);
- res.SetBitPrecision(res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION);
- }
- if (angleMeasure == DEGREE)
- res = res.RadianToDegree();
- else if (angleMeasure == GRAD)
- res = res.RadianToGrad();
- return res;
- }
- /**
- * Arccotangent function.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num The argument.
- * @param angleMeasure The angle measure.
- * @return The arccotangent of the number.
- */
- Real arcctg(const Real& num, AngleMeasure angleMeasure)
- {
- Real _pi = pi(num.GetBitPrecision());
- if (angleMeasure == DEGREE)
- _pi = _pi.RadianToDegree();
- else if (angleMeasure == GRAD)
- _pi = _pi.RadianToGrad();
- Real res(_pi / 2 - arctg(num, angleMeasure));
- return res;
- }
- /**
- * Arcsecant function.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num The argument.
- * @param angleMeasure The angle measure.
- * @return The arcsecant of the number.
- */
- Real arcsec(const Real& num, AngleMeasure angleMeasure)
- {
- Real res(arccos(1 / num, angleMeasure));
- return res;
- }
- /**
- * Acrcosecant function.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num The argument.
- * @param angleMeasure The angle measure.
- * @return The arccosecant of the number.
- */
- Real arccosec(const Real& num, AngleMeasure angleMeasure)
- {
- Real res(arcsin(1 / num, angleMeasure));
- return res;
- }
- /**
- * Hyperbolic sine function.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num The argument.
- * @param angleMeasure The angle measure.
- * @return The hyperbolic sine of the number.
- */
- Real sh(const Real& num, AngleMeasure angleMeasure)
- {
- Real res(num.GetBitPrecision());
- if (angleMeasure != RADIAN)
- {
- Real _num(num.GetBitPrecision());
- if (angleMeasure == DEGREE)
- _num = num.DegreeToRadian();
- else if (angleMeasure == GRAD)
- _num = num.GradToRadian();
- while (mpfr_sinh(res.number, _num.number, DEFAULT_RND) < 0)
- {
- if (res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION >= MPFR_PREC_MAX)
- throw MathException(Overflow);
- res.SetBitPrecision(res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION);
- }
- //mpfr_sinh(res.number, _num.number, DEFAULT_RND);
- return res;
- }
- while (mpfr_sinh(res.number, num.number, DEFAULT_RND) < 0)
- {
- if (res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION >= MPFR_PREC_MAX)
- throw MathException(Overflow);
- res.SetBitPrecision(res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION);
- }
- //mpfr_sinh(res.number, num.number, DEFAULT_RND);
- if (res.IsInfinity() || res.IsNaN())
- throw MathException(Overflow);
- return res;
- }
- /**
- * Hyperbolic cosine function.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num The argument.
- * @param angleMeasure The angle measure.
- * @return The hyperbolic cosine of the number.
- */
- Real ch(const Real& num, AngleMeasure angleMeasure)
- {
- Real res(num.GetBitPrecision());
- if (angleMeasure != RADIAN)
- {
- Real _num(num.GetBitPrecision());
- if (angleMeasure == DEGREE)
- _num = num.DegreeToRadian();
- else if (angleMeasure == GRAD)
- _num = num.GradToRadian();
- while (mpfr_cosh(res.number, _num.number, DEFAULT_RND) < 0)
- {
- if (res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION >= MPFR_PREC_MAX)
- throw MathException(Overflow);
- res.SetBitPrecision(res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION);
- }
- //mpfr_cosh(res.number, _num.number, DEFAULT_RND);
- return res;
- }
- while (mpfr_cosh(res.number, num.number, DEFAULT_RND) < 0)
- {
- if (res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION >= MPFR_PREC_MAX)
- throw MathException(Overflow);
- res.SetBitPrecision(res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION);
- }
- //mpfr_cosh(res.number, num.number, DEFAULT_RND);
- if (res.IsInfinity() || res.IsNaN())
- throw MathException(Overflow);
- return res;
- }
- /**
- * Hyperbolic tangent function.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num The argument.
- * @param angleMeasure The angle measure.
- * @return The hyperbolic tangent of the number.
- */
- Real th(const Real& num, AngleMeasure angleMeasure)
- {
- Real res(num.GetBitPrecision());
- if (angleMeasure != RADIAN)
- {
- Real _num(num.GetBitPrecision());
- if (angleMeasure == DEGREE)
- _num = num.DegreeToRadian();
- else if (angleMeasure == GRAD)
- _num = num.GradToRadian();
- while (mpfr_tanh(res.number, _num.number, DEFAULT_RND) < 0)
- {
- if (res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION >= MPFR_PREC_MAX)
- throw MathException(Overflow);
- res.SetBitPrecision(res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION);
- }
- //mpfr_tanh(res.number, _num.number, DEFAULT_RND);
- return res;
- }
- while (mpfr_tanh(res.number, num.number, DEFAULT_RND) < 0)
- {
- if (res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION >= MPFR_PREC_MAX)
- throw MathException(Overflow);
- res.SetBitPrecision(res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION);
- }
- if (res.IsInfinity() || res.IsNaN())
- throw MathException(Overflow);
- //mpfr_tanh(res.number, num.number, DEFAULT_RND);
- return res;
- }
- /**
- * Hyperbolic cotangent function.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num The argument.
- * @param angleMeasure The angle measure.
- * @return The hyperbolic cotangent of the number.
- */
- Real cth(const Real& num, AngleMeasure angleMeasure)
- {
- Real res(num.GetBitPrecision());
- if (angleMeasure != RADIAN)
- {
- Real _num(num.GetBitPrecision());
- if (angleMeasure == DEGREE)
- _num = num.DegreeToRadian();
- else if (angleMeasure == GRAD)
- _num = num.GradToRadian();
- while (mpfr_coth(res.number, _num.number, DEFAULT_RND) < 0)
- {
- if (res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION >= MPFR_PREC_MAX)
- throw MathException(Overflow);
- res.SetBitPrecision(res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION);
- }
- //mpfr_coth(res.number, _num.number, DEFAULT_RND);
- return res;
- }
- while (mpfr_coth(res.number, num.number, DEFAULT_RND) < 0)
- {
- if (res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION >= MPFR_PREC_MAX)
- throw MathException(Overflow);
- res.SetBitPrecision(res.GetBitPrecision() + DEFAULT_INCREASE_PRECISION);
- }
- if (res.IsInfinity() || res.IsNaN())
- throw MathException(Overflow);
- //mpfr_coth(res.number, num.number, DEFAULT_RND);
- return res;
- }
- /**
- * Hyperbolic secant function.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num The argument.
- * @param angleMeasure The angle measure.
- * @return The hyperbolic secant of the number.
- */
- Real sch(const Real& num, AngleMeasure angleMeasure)
- {
- Real res(num.GetBitPrecision());
- if (angleMeasure != RADIAN)
- {
- Real _num(num.GetBitPrecision());
- if (angleMeasure == DEGREE)
- _num = num.DegreeToRadian();
- else if (angleMeasure == GRAD)
- _num = num.GradToRadian();
- mpfr_sech(res.number, _num.number, DEFAULT_RND);
- return res;
- }
- mpfr_sech(res.number, num.number, DEFAULT_RND);
- return res;
- }
- /**
- * Hyperbolic cosecant function.
- * @exception MathException Thrown when the mathematics error condition occurs.
- * @param num The argument.
- * @param angleMeasure The angle measure.
- * @return The hyperbolic cosecant of the number.
- */
- Real csch(const Real& num, AngleMeasure angleMeasure)
- {
- Real res(num.GetBitPrecision());
- if (angleMeasure != RADIAN)
- {
- Real _num(num.GetBitPrecision());
- if (angleMeasure == DEGREE)
- _num = num.DegreeToRadian();
- else if (angleMeasure == GRAD)
- _num = num.GradToRadian();
- mpfr_csch(res.number, _num.number, DEFAULT_RND);
- return res;
- }
- mpfr_csch(res