File indexing completed on 2024-05-12 05:55:16
0001 // HMath: C++ high precision math routines 0002 // Copyright (C) 2004 Ariya Hidayat <ariya.hidayat@gmail.com> 0003 // Copyright (C) 2007-2008, 2014, 2016 @heldercorreia 0004 // Copyright (C) 2008 Wolf Lammen 0005 // 0006 // This program is free software; you can redistribute it and/or 0007 // modify it under the terms of the GNU General Public License 0008 // as published by the Free Software Foundation; either version 2 0009 // of the License, or (at your option) any later version. 0010 // 0011 // This program is distributed in the hope that it will be useful, 0012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0014 // GNU General Public License for more details. 0015 // 0016 // You should have received a copy of the GNU General Public License 0017 // along with this program; see the file COPYING. If not, write to 0018 // the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0019 // Boston, MA 02110-1301, USA. 0020 0021 #ifndef MATH_HMATH_H 0022 #define MATH_HMATH_H 0023 0024 #include "errors.h" 0025 0026 #include <QJsonObject> 0027 #include <QString> 0028 0029 #include <ostream> 0030 0031 class HMath; 0032 class HNumberPrivate; 0033 class CNumber; 0034 class CMath; 0035 class Rational; 0036 0037 class HNumber { 0038 friend class HMath; 0039 friend class CNumber; 0040 friend class CMath; 0041 friend HNumber operator-(const HNumber&); 0042 friend HNumber operator-(const HNumber&, const HNumber&); 0043 friend bool operator>(const HNumber&, const HNumber&); 0044 friend bool operator<(const HNumber&, const HNumber&); 0045 friend bool operator>=(const HNumber&, const HNumber&); 0046 friend bool operator<=(const HNumber&, const HNumber&); 0047 friend bool operator==(const HNumber&, const HNumber&); 0048 friend bool operator!=(const HNumber&, const HNumber&); 0049 0050 public: 0051 HNumber(); 0052 HNumber(const HNumber&); 0053 HNumber(int); 0054 HNumber(const char*); 0055 HNumber(const QJsonObject&); 0056 ~HNumber(); 0057 0058 bool isNan() const; 0059 bool isZero() const; 0060 bool isNearZero() const; 0061 bool isPositive() const; 0062 bool isNegative() const; 0063 bool isInteger() const; 0064 0065 void serialize(QJsonObject&) const; 0066 static HNumber deSerialize(const QJsonObject&); 0067 0068 int toInt() const; 0069 Error error() const; 0070 0071 HNumber& operator=(const HNumber&); 0072 HNumber operator+(const HNumber&) const; 0073 HNumber& operator+=(const HNumber&); 0074 HNumber& operator-=(const HNumber&); 0075 HNumber operator*(const HNumber&) const; 0076 HNumber& operator*=(const HNumber&); 0077 HNumber operator/(const HNumber&) const; 0078 HNumber& operator/=(const HNumber&); 0079 HNumber operator%(const HNumber&) const; 0080 HNumber operator&(const HNumber&) const; 0081 HNumber& operator&=(const HNumber&); 0082 HNumber operator|(const HNumber&) const; 0083 HNumber& operator|=(const HNumber&); 0084 HNumber operator^(const HNumber&) const; 0085 HNumber& operator^=(const HNumber&); 0086 HNumber operator~() const; 0087 HNumber operator>>(const HNumber&) const; 0088 HNumber operator<<(const HNumber&) const; 0089 0090 private: 0091 HNumberPrivate* d; 0092 0093 int compare(const HNumber&) const; 0094 0095 public: 0096 struct Format { 0097 enum class Base {Null, Binary, Decimal, Octal, Hexadecimal}; 0098 enum class RadixChar {Null, Point, Comma}; 0099 enum class Mode {Null, General, Fixed, Scientific, Engineering}; 0100 0101 Base base; 0102 RadixChar radixChar; 0103 Mode mode; 0104 int precision; // -1 means 'auto' 0105 static const int PrecisionNull = -1000; 0106 0107 Format(); 0108 Format(const Format&); 0109 Format operator+(const Format&) const; 0110 0111 static Format Binary(); 0112 static Format Octal(); 0113 static Format Decimal(); 0114 static Format Hexadecimal(); 0115 0116 static Format Precision(int); 0117 0118 static Format Point(); 0119 static Format Comma(); 0120 0121 static Format General(); 0122 static Format Fixed(); 0123 static Format Scientific(); 0124 static Format Engineering(); 0125 }; 0126 }; 0127 0128 class HMath { 0129 public: 0130 // FORMAT 0131 static QString format(const HNumber&, HNumber::Format = HNumber::Format()); 0132 // PARSING 0133 static HNumber parse_str(const char*, const char** out); 0134 // CONSTANTS 0135 static HNumber e(); 0136 static HNumber phi(); 0137 static HNumber pi(); 0138 static HNumber nan(Error = Success); 0139 // GENERAL MATH 0140 static HNumber rad2deg(const HNumber&); 0141 static HNumber deg2rad(const HNumber&); 0142 static HNumber rad2gon(const HNumber&); 0143 static HNumber gon2rad(const HNumber&); 0144 static HNumber max(const HNumber&, const HNumber&); 0145 static HNumber min(const HNumber&, const HNumber&); 0146 static HNumber abs(const HNumber&); 0147 static HNumber integer(const HNumber&); 0148 static HNumber frac(const HNumber&); 0149 static HNumber floor(const HNumber&); 0150 static HNumber ceil(const HNumber&); 0151 static HNumber gcd(const HNumber&, const HNumber&); 0152 static HNumber idiv(const HNumber&, const HNumber&); 0153 static HNumber round(const HNumber&, int prec = 0); 0154 static HNumber trunc(const HNumber&, int prec = 0); 0155 static HNumber sqrt(const HNumber&); 0156 static HNumber cbrt(const HNumber&); 0157 static HNumber raise(const HNumber&, int); 0158 static HNumber raise(const HNumber&, const HNumber&); 0159 static HNumber sgn(const HNumber&); 0160 // EXPONENTIAL FUNCTION AND RELATED 0161 static HNumber exp(const HNumber&); 0162 static HNumber ln(const HNumber&); 0163 static HNumber lg(const HNumber&); 0164 static HNumber lb(const HNumber&); 0165 static HNumber log(const HNumber& base, const HNumber& x); 0166 static HNumber sinh(const HNumber&); 0167 static HNumber cosh(const HNumber&); 0168 static HNumber tanh(const HNumber&); 0169 static HNumber arsinh(const HNumber&); 0170 static HNumber arcosh(const HNumber&); 0171 static HNumber artanh(const HNumber&); 0172 // TRIGONOMETRY 0173 static HNumber sin(const HNumber&); 0174 static HNumber cos(const HNumber&); 0175 static HNumber tan(const HNumber&); 0176 static HNumber cot(const HNumber&); 0177 static HNumber sec(const HNumber&); 0178 static HNumber csc(const HNumber&); 0179 static HNumber arcsin(const HNumber&); 0180 static HNumber arccos(const HNumber&); 0181 static HNumber arctan(const HNumber&); 0182 static HNumber arctan2(const HNumber&, const HNumber&); 0183 // HIGHER MATH FUNCTIONS 0184 static HNumber factorial(const HNumber&, const HNumber& base = HNumber(1)); 0185 static HNumber gamma(const HNumber&); 0186 static HNumber lnGamma(const HNumber&); 0187 static HNumber erf(const HNumber&); 0188 static HNumber erfc(const HNumber&); 0189 // PROBABILITY 0190 static HNumber nCr(const HNumber& n, const HNumber& k); 0191 static HNumber nPr(const HNumber& n, const HNumber& r); 0192 static HNumber binomialPmf(const HNumber& k, const HNumber& n, const HNumber& p); 0193 static HNumber binomialCdf(const HNumber& k, const HNumber& n, const HNumber& p); 0194 static HNumber binomialMean(const HNumber& n, const HNumber& p); 0195 static HNumber binomialVariance(const HNumber& n, const HNumber& p); 0196 static HNumber hypergeometricPmf(const HNumber& k, const HNumber& N, const HNumber& M, const HNumber& n); 0197 static HNumber hypergeometricCdf(const HNumber& k, const HNumber& N, const HNumber& M, const HNumber& n); 0198 static HNumber hypergeometricMean(const HNumber& N, const HNumber& M, const HNumber& n); 0199 static HNumber hypergeometricVariance(const HNumber& N, const HNumber& M, const HNumber& n); 0200 static HNumber poissonPmf(const HNumber& k, const HNumber& l); 0201 static HNumber poissonCdf(const HNumber& k, const HNumber& l); 0202 static HNumber poissonMean(const HNumber& l); 0203 static HNumber poissonVariance(const HNumber& l); 0204 // LOGIC 0205 static HNumber mask(const HNumber&, const HNumber& bits); 0206 static HNumber sgnext(const HNumber&, const HNumber& bits); 0207 static HNumber ashr(const HNumber&, const HNumber& bits); 0208 // IEEE-754 CONVERSION 0209 static HNumber decodeIeee754(const HNumber&, const HNumber& exp_bits, const HNumber& significand_bits); 0210 static HNumber decodeIeee754(const HNumber&, const HNumber& exp_bits, const HNumber& significand_bits, 0211 const HNumber& exp_bias); 0212 static HNumber encodeIeee754(const HNumber&, const HNumber& exp_bits, const HNumber& significand_bits); 0213 static HNumber encodeIeee754(const HNumber&, const HNumber& exp_bits, const HNumber& significand_bits, 0214 const HNumber& exp_bias); 0215 }; 0216 0217 std::ostream& operator<<(std::ostream&, const HNumber&); 0218 0219 #endif // MATH_HMATH_H