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.

597 lines
13 KiB

  1. /*
  2. Multi-precision real number class. C++ interface fo MPFR library.
  3. Project homepage: http://www.holoborodko.com/pavel/
  4. Contact e-mail: pavel@holoborodko.com
  5. Copyright (c) 2008-2011 Pavel Holoborodko
  6. Core Developers:
  7. Pavel Holoborodko, Dmitriy Gubanov, Konstantin Holoborodko.
  8. Contributors:
  9. Brian Gladman, Helmut Jarausch, Fokko Beekhof, Ulrich Mutze,
  10. Heinz van Saanen, Pere Constans, Peter van Hoof, Gael Guennebaud,
  11. Tsai Chia Cheng, Alexei Zubanov.
  12. ****************************************************************************
  13. This library is free software; you can redistribute it and/or
  14. modify it under the terms of the GNU Lesser General Public
  15. License as published by the Free Software Foundation; either
  16. version 2.1 of the License, or (at your option) any later version.
  17. This library is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. Lesser General Public License for more details.
  21. You should have received a copy of the GNU Lesser General Public
  22. License along with this library; if not, write to the Free Software
  23. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. ****************************************************************************
  25. ****************************************************************************
  26. Redistribution and use in source and binary forms, with or without
  27. modification, are permitted provided that the following conditions
  28. are met:
  29. 1. Redistributions of source code must retain the above copyright
  30. notice, this list of conditions and the following disclaimer.
  31. 2. Redistributions in binary form must reproduce the above copyright
  32. notice, this list of conditions and the following disclaimer in the
  33. documentation and/or other materials provided with the distribution.
  34. 3. The name of the author may be used to endorse or promote products
  35. derived from this software without specific prior written permission.
  36. THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  37. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  38. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  39. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  40. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  41. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  42. OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  44. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  45. OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. SUCH DAMAGE.
  47. */
  48. #include <cstring>
  49. #include "mpreal.h"
  50. #if defined (MPREAL_HAVE_CUSTOM_MPFR_MALLOC)
  51. #include "dlmalloc.h"
  52. #endif
  53. using std::ws;
  54. using std::cerr;
  55. using std::endl;
  56. using std::string;
  57. using std::ostream;
  58. using std::istream;
  59. namespace mpfr{
  60. mp_rnd_t mpreal::default_rnd = MPFR_RNDN; //(mpfr_get_default_rounding_mode)();
  61. mp_prec_t mpreal::default_prec = 64; //(mpfr_get_default_prec)();
  62. int mpreal::default_base = 10;
  63. int mpreal::double_bits = -1;
  64. #if defined (MPREAL_HAVE_CUSTOM_MPFR_MALLOC)
  65. bool mpreal::is_custom_malloc = false;
  66. #endif
  67. // Default constructor: creates mp number and initializes it to 0.
  68. mpreal::mpreal()
  69. {
  70. #if defined (MPREAL_HAVE_CUSTOM_MPFR_MALLOC)
  71. set_custom_malloc();
  72. #endif
  73. mpfr_init2(mp,default_prec);
  74. mpfr_set_ui(mp,0,default_rnd);
  75. MPREAL_MSVC_DEBUGVIEW_CODE;
  76. }
  77. mpreal::mpreal(const mpreal& u)
  78. {
  79. #if defined (MPREAL_HAVE_CUSTOM_MPFR_MALLOC)
  80. set_custom_malloc();
  81. #endif
  82. mpfr_init2(mp,mpfr_get_prec(u.mp));
  83. mpfr_set(mp,u.mp,default_rnd);
  84. MPREAL_MSVC_DEBUGVIEW_CODE;
  85. }
  86. mpreal::mpreal(const mpfr_t u)
  87. {
  88. #if defined (MPREAL_HAVE_CUSTOM_MPFR_MALLOC)
  89. set_custom_malloc();
  90. #endif
  91. mpfr_init2(mp,mpfr_get_prec(u));
  92. mpfr_set(mp,u,default_rnd);
  93. MPREAL_MSVC_DEBUGVIEW_CODE;
  94. }
  95. mpreal::mpreal(const mpf_t u)
  96. {
  97. #if defined (MPREAL_HAVE_CUSTOM_MPFR_MALLOC)
  98. set_custom_malloc();
  99. #endif
  100. mpfr_init2(mp,(mp_prec_t) mpf_get_prec(u)); // (gmp: mp_bitcnt_t) unsigned long -> long (mpfr: mp_prec_t)
  101. mpfr_set_f(mp,u,default_rnd);
  102. MPREAL_MSVC_DEBUGVIEW_CODE;
  103. }
  104. mpreal::mpreal(const mpz_t u, mp_prec_t prec, mp_rnd_t mode)
  105. {
  106. #if defined (MPREAL_HAVE_CUSTOM_MPFR_MALLOC)
  107. set_custom_malloc();
  108. #endif
  109. mpfr_init2(mp,prec);
  110. mpfr_set_z(mp,u,mode);
  111. MPREAL_MSVC_DEBUGVIEW_CODE;
  112. }
  113. mpreal::mpreal(const mpq_t u, mp_prec_t prec, mp_rnd_t mode)
  114. {
  115. #if defined (MPREAL_HAVE_CUSTOM_MPFR_MALLOC)
  116. set_custom_malloc();
  117. #endif
  118. mpfr_init2(mp,prec);
  119. mpfr_set_q(mp,u,mode);
  120. MPREAL_MSVC_DEBUGVIEW_CODE;
  121. }
  122. mpreal::mpreal(const double u, mp_prec_t prec, mp_rnd_t mode)
  123. {
  124. #if defined (MPREAL_HAVE_CUSTOM_MPFR_MALLOC)
  125. set_custom_malloc();
  126. #endif
  127. if(double_bits == -1 || fits_in_bits(u, double_bits))
  128. {
  129. mpfr_init2(mp,prec);
  130. mpfr_set_d(mp,u,mode);
  131. MPREAL_MSVC_DEBUGVIEW_CODE;
  132. }
  133. else
  134. throw conversion_overflow();
  135. }
  136. mpreal::mpreal(const long double u, mp_prec_t prec, mp_rnd_t mode)
  137. {
  138. #if defined (MPREAL_HAVE_CUSTOM_MPFR_MALLOC)
  139. set_custom_malloc();
  140. #endif
  141. mpfr_init2(mp,prec);
  142. mpfr_set_ld(mp,u,mode);
  143. MPREAL_MSVC_DEBUGVIEW_CODE;
  144. }
  145. mpreal::mpreal(const unsigned long int u, mp_prec_t prec, mp_rnd_t mode)
  146. {
  147. #if defined (MPREAL_HAVE_CUSTOM_MPFR_MALLOC)
  148. set_custom_malloc();
  149. #endif
  150. mpfr_init2(mp,prec);
  151. mpfr_set_ui(mp,u,mode);
  152. MPREAL_MSVC_DEBUGVIEW_CODE;
  153. }
  154. mpreal::mpreal(const unsigned int u, mp_prec_t prec, mp_rnd_t mode)
  155. {
  156. #if defined (MPREAL_HAVE_CUSTOM_MPFR_MALLOC)
  157. set_custom_malloc();
  158. #endif
  159. mpfr_init2(mp,prec);
  160. mpfr_set_ui(mp,u,mode);
  161. MPREAL_MSVC_DEBUGVIEW_CODE;
  162. }
  163. mpreal::mpreal(const long int u, mp_prec_t prec, mp_rnd_t mode)
  164. {
  165. #if defined (MPREAL_HAVE_CUSTOM_MPFR_MALLOC)
  166. set_custom_malloc();
  167. #endif
  168. mpfr_init2(mp,prec);
  169. mpfr_set_si(mp,u,mode);
  170. MPREAL_MSVC_DEBUGVIEW_CODE;
  171. }
  172. mpreal::mpreal(const int u, mp_prec_t prec, mp_rnd_t mode)
  173. {
  174. #if defined (MPREAL_HAVE_CUSTOM_MPFR_MALLOC)
  175. set_custom_malloc();
  176. #endif
  177. mpfr_init2(mp,prec);
  178. mpfr_set_si(mp,u,mode);
  179. MPREAL_MSVC_DEBUGVIEW_CODE;
  180. }
  181. #if defined (MPREAL_HAVE_INT64_SUPPORT)
  182. mpreal::mpreal(const uint64_t u, mp_prec_t prec, mp_rnd_t mode)
  183. {
  184. #if defined (MPREAL_HAVE_CUSTOM_MPFR_MALLOC)
  185. set_custom_malloc();
  186. #endif
  187. mpfr_init2(mp,prec);
  188. mpfr_set_uj(mp, u, mode);
  189. MPREAL_MSVC_DEBUGVIEW_CODE;
  190. }
  191. mpreal::mpreal(const int64_t u, mp_prec_t prec, mp_rnd_t mode)
  192. {
  193. #if defined (MPREAL_HAVE_CUSTOM_MPFR_MALLOC)
  194. set_custom_malloc();
  195. #endif
  196. mpfr_init2(mp,prec);
  197. mpfr_set_sj(mp, u, mode);
  198. MPREAL_MSVC_DEBUGVIEW_CODE;
  199. }
  200. #endif
  201. mpreal::mpreal(const char* s, mp_prec_t prec, int base, mp_rnd_t mode)
  202. {
  203. #if defined (MPREAL_HAVE_CUSTOM_MPFR_MALLOC)
  204. set_custom_malloc();
  205. #endif
  206. mpfr_init2(mp,prec);
  207. mpfr_set_str(mp, s, base, mode);
  208. MPREAL_MSVC_DEBUGVIEW_CODE;
  209. }
  210. mpreal::mpreal(const std::string& s, mp_prec_t prec, int base, mp_rnd_t mode)
  211. {
  212. #if defined (MPREAL_HAVE_CUSTOM_MPFR_MALLOC)
  213. set_custom_malloc();
  214. #endif
  215. mpfr_init2(mp,prec);
  216. mpfr_set_str(mp, s.c_str(), base, mode);
  217. MPREAL_MSVC_DEBUGVIEW_CODE;
  218. }
  219. mpreal::~mpreal()
  220. {
  221. mpfr_clear(mp);
  222. }
  223. // Operators - Assignment
  224. mpreal& mpreal::operator=(const char* s)
  225. {
  226. mpfr_t t;
  227. #if defined (MPREAL_HAVE_CUSTOM_MPFR_MALLOC)
  228. set_custom_malloc();
  229. #endif
  230. if(0==mpfr_init_set_str(t,s,default_base,default_rnd))
  231. {
  232. // We will rewrite mp anyway, so flash it and resize
  233. mpfr_set_prec(mp,mpfr_get_prec(t));
  234. mpfr_set(mp,t,mpreal::default_rnd);
  235. mpfr_clear(t);
  236. MPREAL_MSVC_DEBUGVIEW_CODE;
  237. }else{
  238. mpfr_clear(t);
  239. }
  240. return *this;
  241. }
  242. const mpreal fma (const mpreal& v1, const mpreal& v2, const mpreal& v3, mp_rnd_t rnd_mode)
  243. {
  244. mpreal a;
  245. mp_prec_t p1, p2, p3;
  246. p1 = v1.get_prec();
  247. p2 = v2.get_prec();
  248. p3 = v3.get_prec();
  249. a.set_prec(p3>p2?(p3>p1?p3:p1):(p2>p1?p2:p1));
  250. mpfr_fma(a.mp,v1.mp,v2.mp,v3.mp,rnd_mode);
  251. return a;
  252. }
  253. const mpreal fms (const mpreal& v1, const mpreal& v2, const mpreal& v3, mp_rnd_t rnd_mode)
  254. {
  255. mpreal a;
  256. mp_prec_t p1, p2, p3;
  257. p1 = v1.get_prec();
  258. p2 = v2.get_prec();
  259. p3 = v3.get_prec();
  260. a.set_prec(p3>p2?(p3>p1?p3:p1):(p2>p1?p2:p1));
  261. mpfr_fms(a.mp,v1.mp,v2.mp,v3.mp,rnd_mode);
  262. return a;
  263. }
  264. const mpreal agm (const mpreal& v1, const mpreal& v2, mp_rnd_t rnd_mode)
  265. {
  266. mpreal a;
  267. mp_prec_t p1, p2;
  268. p1 = v1.get_prec();
  269. p2 = v2.get_prec();
  270. a.set_prec(p1>p2?p1:p2);
  271. mpfr_agm(a.mp, v1.mp, v2.mp, rnd_mode);
  272. return a;
  273. }
  274. const mpreal sum (const mpreal tab[], unsigned long int n, mp_rnd_t rnd_mode)
  275. {
  276. mpreal x;
  277. mpfr_ptr* t;
  278. unsigned long int i;
  279. t = new mpfr_ptr[n];
  280. for (i=0;i<n;i++) t[i] = (mpfr_ptr)tab[i].mp;
  281. mpfr_sum(x.mp,t,n,rnd_mode);
  282. delete[] t;
  283. return x;
  284. }
  285. const mpreal remquo (long* q, const mpreal& x, const mpreal& y, mp_rnd_t rnd_mode)
  286. {
  287. mpreal a;
  288. mp_prec_t yp, xp;
  289. yp = y.get_prec();
  290. xp = x.get_prec();
  291. a.set_prec(yp>xp?yp:xp);
  292. mpfr_remquo(a.mp,q, x.mp, y.mp, rnd_mode);
  293. return a;
  294. }
  295. template <class T>
  296. std::string toString(T t, std::ios_base & (*f)(std::ios_base&))
  297. {
  298. std::ostringstream oss;
  299. oss << f << t;
  300. return oss.str();
  301. }
  302. #if (MPFR_VERSION >= MPFR_VERSION_NUM(2,4,0))
  303. std::string mpreal::toString(const std::string& format) const
  304. {
  305. char *s = NULL;
  306. string out;
  307. if( !format.empty() )
  308. {
  309. if(!(mpfr_asprintf(&s,format.c_str(),mp) < 0))
  310. {
  311. out = std::string(s);
  312. mpfr_free_str(s);
  313. }
  314. }
  315. return out;
  316. }
  317. #endif
  318. std::string mpreal::toString(int n, int b, mp_rnd_t mode) const
  319. {
  320. (void)b;
  321. (void)mode;
  322. #if (MPFR_VERSION >= MPFR_VERSION_NUM(2,4,0))
  323. // Use MPFR native function for output
  324. char format[128];
  325. int digits;
  326. digits = n > 0 ? n : bits2digits(mpfr_get_prec(mp));
  327. sprintf(format,"%%.%dRNg",digits); // Default format
  328. return toString(std::string(format));
  329. #else
  330. char *s, *ns = NULL;
  331. size_t slen, nslen;
  332. mp_exp_t exp;
  333. string out;
  334. #if defined (MPREAL_HAVE_CUSTOM_MPFR_MALLOC)
  335. set_custom_malloc();
  336. #endif
  337. if(mpfr_inf_p(mp))
  338. {
  339. if(mpfr_sgn(mp)>0) return "+Inf";
  340. else return "-Inf";
  341. }
  342. if(mpfr_zero_p(mp)) return "0";
  343. if(mpfr_nan_p(mp)) return "NaN";
  344. s = mpfr_get_str(NULL,&exp,b,0,mp,mode);
  345. ns = mpfr_get_str(NULL,&exp,b,n,mp,mode);
  346. if(s!=NULL && ns!=NULL)
  347. {
  348. slen = strlen(s);
  349. nslen = strlen(ns);
  350. if(nslen<=slen)
  351. {
  352. mpfr_free_str(s);
  353. s = ns;
  354. slen = nslen;
  355. }
  356. else {
  357. mpfr_free_str(ns);
  358. }
  359. // Make human eye-friendly formatting if possible
  360. if (exp>0 && static_cast<size_t>(exp)<slen)
  361. {
  362. if(s[0]=='-')
  363. {
  364. // Remove zeros starting from right end
  365. char* ptr = s+slen-1;
  366. while (*ptr=='0' && ptr>s+exp) ptr--;
  367. if(ptr==s+exp) out = string(s,exp+1);
  368. else out = string(s,exp+1)+'.'+string(s+exp+1,ptr-(s+exp+1)+1);
  369. //out = string(s,exp+1)+'.'+string(s+exp+1);
  370. }
  371. else
  372. {
  373. // Remove zeros starting from right end
  374. char* ptr = s+slen-1;
  375. while (*ptr=='0' && ptr>s+exp-1) ptr--;
  376. if(ptr==s+exp-1) out = string(s,exp);
  377. else out = string(s,exp)+'.'+string(s+exp,ptr-(s+exp)+1);
  378. //out = string(s,exp)+'.'+string(s+exp);
  379. }
  380. }else{ // exp<0 || exp>slen
  381. if(s[0]=='-')
  382. {
  383. // Remove zeros starting from right end
  384. char* ptr = s+slen-1;
  385. while (*ptr=='0' && ptr>s+1) ptr--;
  386. if(ptr==s+1) out = string(s,2);
  387. else out = string(s,2)+'.'+string(s+2,ptr-(s+2)+1);
  388. //out = string(s,2)+'.'+string(s+2);
  389. }
  390. else
  391. {
  392. // Remove zeros starting from right end
  393. char* ptr = s+slen-1;
  394. while (*ptr=='0' && ptr>s) ptr--;
  395. if(ptr==s) out = string(s,1);
  396. else out = string(s,1)+'.'+string(s+1,ptr-(s+1)+1);
  397. //out = string(s,1)+'.'+string(s+1);
  398. }
  399. // Make final string
  400. if(--exp)
  401. {
  402. if(exp>0) out += "e+"+mpfr::toString<mp_exp_t>(exp,std::dec);
  403. else out += "e"+mpfr::toString<mp_exp_t>(exp,std::dec);
  404. }
  405. }
  406. mpfr_free_str(s);
  407. return out;
  408. }else{
  409. return "conversion error!";
  410. }
  411. #endif
  412. }
  413. //////////////////////////////////////////////////////////////////////////
  414. // I/O
  415. ostream& operator<<(ostream& os, const mpreal& v)
  416. {
  417. return os<<v.toString(static_cast<int>(os.precision()));
  418. }
  419. istream& operator>>(istream &is, mpreal& v)
  420. {
  421. string tmp;
  422. is >> tmp;
  423. mpfr_set_str(v.mp, tmp.c_str(),mpreal::default_base,mpreal::default_rnd);
  424. return is;
  425. }
  426. #if defined (MPREAL_HAVE_CUSTOM_MPFR_MALLOC)
  427. // Optimized dynamic memory allocation/(re-)deallocation.
  428. void * mpreal::mpreal_allocate(size_t alloc_size)
  429. {
  430. return(dlmalloc(alloc_size));
  431. }
  432. void * mpreal::mpreal_reallocate(void *ptr, size_t old_size, size_t new_size)
  433. {
  434. return(dlrealloc(ptr,new_size));
  435. }
  436. void mpreal::mpreal_free(void *ptr, size_t size)
  437. {
  438. dlfree(ptr);
  439. }
  440. inline void mpreal::set_custom_malloc(void)
  441. {
  442. if(!is_custom_malloc)
  443. {
  444. mp_set_memory_functions(mpreal_allocate,mpreal_reallocate,mpreal_free);
  445. is_custom_malloc = true;
  446. }
  447. }
  448. #endif
  449. }