AbaKus – a complex calculator
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

360 lines
7.6KB

  1. /* HMath: C++ high precision math routines
  2. Copyright (C) 2004 Ariya Hidayat <ariya.hidayat@gmail.com>
  3. This file was copied from the SpeedCrunch program. Please visit
  4. http://speedcrunch.berlios.de/ for more information.
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, write to:
  15. The Free Software Foundation, Inc.
  16. 51 Franklin Street, Fifth Floor
  17. Boston, MA 02110-1301 USA.
  18. */
  19. #ifndef HMATH_H
  20. #define HMATH_H
  21. #include <iostream>
  22. class HMath;
  23. class HNumber
  24. {
  25. friend class HMath;
  26. public:
  27. /*!
  28. * Creates a new number.
  29. */
  30. HNumber();
  31. /*!
  32. * Destroys this number.
  33. */
  34. ~HNumber();
  35. /*!
  36. * Copies from another number.
  37. */
  38. HNumber( const HNumber& );
  39. /*!
  40. * Assigns from another number.
  41. */
  42. HNumber& operator=( const HNumber& );
  43. /*!
  44. * Creates a new number from an integer value.
  45. */
  46. HNumber( int i );
  47. /*!
  48. * Creates a new number from a string.
  49. */
  50. HNumber( const char* );
  51. /*!
  52. * Returns true if this number is Not a Number (NaN).
  53. */
  54. bool isNan() const;
  55. /*!
  56. * Returns true if this number is zero.
  57. */
  58. bool isZero() const;
  59. /*!
  60. * Returns true if this number is more than zero.
  61. */
  62. bool isPositive() const;
  63. /*!
  64. * Returns true if this number is less than zero.
  65. */
  66. bool isNegative() const;
  67. /*!
  68. * Adds another number.
  69. */
  70. HNumber operator+( const HNumber& ) const;
  71. /*!
  72. * Adds another number.
  73. */
  74. HNumber& operator+=( const HNumber& );
  75. /*!
  76. * Subtract from another number.
  77. */
  78. HNumber operator-( const HNumber& ) const;
  79. /*!
  80. * Subtract from another number.
  81. */
  82. HNumber& operator-=( const HNumber& );
  83. /*!
  84. * Multiplies with another number.
  85. */
  86. HNumber operator*( const HNumber& ) const;
  87. /*!
  88. * Multiplies with another number.
  89. */
  90. HNumber& operator*=( const HNumber& );
  91. /*!
  92. * Divides with another number.
  93. */
  94. HNumber operator/( const HNumber& ) const;
  95. /*!
  96. * Divides with another number.
  97. */
  98. HNumber& operator/=( const HNumber& );
  99. /*!
  100. * Returns true if this number is greater than n.
  101. */
  102. bool operator>( const HNumber& n ) const;
  103. /*!
  104. * Returns true if this number is less than n.
  105. */
  106. bool operator<( const HNumber& n ) const;
  107. /*!
  108. * Returns true if this number is greater than or equal to n.
  109. */
  110. bool operator>=( const HNumber& n ) const;
  111. /*!
  112. * Returns true if this number is less than or equal to n.
  113. */
  114. bool operator<=( const HNumber& n ) const;
  115. /*!
  116. * Returns true if this number is equal to n.
  117. */
  118. bool operator==( const HNumber& n ) const;
  119. /*!
  120. * Returns true if this number is not equal to n.
  121. */
  122. bool operator!=( const HNumber& n ) const;
  123. /*!
  124. * Returns a NaN (Not a Number).
  125. */
  126. static HNumber nan();
  127. private:
  128. class Private;
  129. Private* d;
  130. };
  131. class TQString;
  132. class HMath
  133. {
  134. public:
  135. /*!
  136. * Formats the given number as string, using specified decimal digits.
  137. * Note that the returned string must be freed.
  138. */
  139. static char* format( const HNumber&n, char format = 'g', int prec = -1 );
  140. /*!
  141. * Formats the given number as string, using specified decimal digits.
  142. * Note that the returned string must be freed.
  143. */
  144. static char* formatFixed( const HNumber&n, int prec = -1 );
  145. /*!
  146. * Formats the given number as string, in exponential format.
  147. * Note that the returned string must be freed.
  148. */
  149. static char* formatExp( const HNumber&n, int prec = -1 );
  150. /*!
  151. * Formats the given number as string, using specified decimal digits.
  152. * Note that the returned string must be freed.
  153. */
  154. static char* formatGeneral( const HNumber&n, int prec = -1 );
  155. static TQString formatGenString( const HNumber &n, int prec = -1 );
  156. /*!
  157. * Returns the constant pi.
  158. */
  159. static HNumber pi();
  160. /*!
  161. * Adds two numbers.
  162. */
  163. static HNumber add( const HNumber& n1, const HNumber& n2 );
  164. /*!
  165. * Subtracts two numbers.
  166. */
  167. static HNumber sub( const HNumber& n1, const HNumber& n2 );
  168. /*!
  169. * Multiplies two numbers.
  170. */
  171. static HNumber mul( const HNumber& n1, const HNumber& n2 );
  172. /*!
  173. * Divides two numbers.
  174. */
  175. static HNumber div( const HNumber& n1, const HNumber& n2 );
  176. /*!
  177. * Returns -1, 0, 1 if n1 is less than, equal to, or more than n2.
  178. */
  179. static int compare( const HNumber& n1, const HNumber& n2 );
  180. /*!
  181. * Returns the absolute value of n.
  182. */
  183. static HNumber abs( const HNumber& n );
  184. /*!
  185. * Returns the negative of n.
  186. */
  187. static HNumber negate( const HNumber& n );
  188. /*!
  189. * Returns the integer part of n.
  190. */
  191. static HNumber integer( const HNumber& n );
  192. /*!
  193. * Returns the fraction part of n.
  194. */
  195. static HNumber frac( const HNumber& n );
  196. /*!
  197. * Rounds n to the specified decimal digits.
  198. */
  199. static HNumber round( const HNumber&n, int prec = 0 );
  200. /*!
  201. * Returns the square root of n. If n is negative, returns NaN.
  202. */
  203. static HNumber sqrt( const HNumber& n );
  204. /*!
  205. * Raises n1 to an integer n.
  206. */
  207. static HNumber raise( const HNumber& n1, int n );
  208. /*!
  209. * Raises n1 to n2.
  210. */
  211. static HNumber raise( const HNumber& n1, const HNumber& n2 );
  212. /*!
  213. * Returns e raised to x.
  214. */
  215. static HNumber exp( const HNumber& x );
  216. /*!
  217. * Returns the natural logarithm of x.
  218. * If x is non positive, returns NaN.
  219. */
  220. static HNumber ln( const HNumber& x );
  221. /*!
  222. * Returns the base-10 logarithm of x.
  223. * If x is non positive, returns NaN.
  224. */
  225. static HNumber log( const HNumber& x );
  226. /*!
  227. * Returns the sine of x. Note that x must be in radians.
  228. */
  229. static HNumber sin( const HNumber& x );
  230. /*!
  231. * Returns the cosine of x. Note that x must be in radians.
  232. */
  233. static HNumber cos( const HNumber& x );
  234. /*!
  235. * Returns the tangent of x. Note that x must be in radians.
  236. */
  237. static HNumber tan( const HNumber& x );
  238. /*!
  239. * Returns the arc sine of x.
  240. */
  241. static HNumber asin( const HNumber& x );
  242. /*!
  243. * Returns the arc cosine of x.
  244. */
  245. static HNumber acos( const HNumber& x );
  246. /*!
  247. * Returns the arc tangent of x.
  248. */
  249. static HNumber atan( const HNumber& x );
  250. /*!
  251. * Returns the hyperbolic sine of x. Note that x must be in radians.
  252. */
  253. static HNumber sinh( const HNumber& x );
  254. /*!
  255. * Returns the arc hyperbolic sine of x. The result is in radians.
  256. */
  257. static HNumber asinh( const HNumber & x );
  258. /*!
  259. * Returns the hyperbolic cosine of x. Note that x must be in radians.
  260. */
  261. static HNumber cosh( const HNumber& x );
  262. /*!
  263. * Returns the arc hyperbolic cosine of x. The result is in radians.
  264. */
  265. static HNumber acosh( const HNumber & x );
  266. /*!
  267. * Returns the hyperbolic tangent of x. Note that x must be in radians.
  268. */
  269. static HNumber tanh( const HNumber& x );
  270. /*!
  271. * Returns the arc hyperbolic tangent of x. The result is in radians.
  272. */
  273. static HNumber atanh( const HNumber & x );
  274. /*!
  275. * Releases all resources. After calling this function, you can not use
  276. * any other functions as well as class HNumber.
  277. */
  278. static void finalize();
  279. };
  280. std::ostream& operator<<( std::ostream& s, HNumber );
  281. #endif // HMATH_H
  282. // vim: set et sw=2 ts=8: