/
BigFloat.h
637 lines (580 loc) · 17.4 KB
/
BigFloat.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
/****************************************************************************
* Core Library Version 1.7, August 2004
* Copyright (c) 1995-2004 Exact Computation Project
* All rights reserved.
*
* This file is part of CGAL (www.cgal.org);
* You can redistribute it and/or modify it under the terms of the GNU
* Lesser General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* Licensees holding a valid commercial license may use this file in
* accordance with the commercial license agreement provided with the
* software.
*
* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
* WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
*
* File: BigFloat.h
* Synopsis:
* An implementation of BigFloat numbers with error bounds.
*
* Written by
* Chee Yap <yap@cs.nyu.edu>
* Chen Li <chenli@cs.nyu.edu>
* Zilin Du <zilin@cs.nyu.edu>
*
* WWW URL: http://cs.nyu.edu/exact/
* Email: exact@cs.nyu.edu
*
* $URL$
* $Id$
***************************************************************************/
#ifndef _CORE_BIGFLOAT_H_
#define _CORE_BIGFLOAT_H_
#include <CGAL/CORE/BigFloatRep.h>
#include <CGAL/assertions.h>
namespace CORE {
class Expr;
/// \class BigFloat BigFloat.h
/// \brief BigFloat is a class of Float-Point number with error bounds.
typedef RCImpl<BigFloatRep> RCBigFloat;
class CGAL_CORE_EXPORT BigFloat : public RCBigFloat {
public:
/// \name Constructors and Destructor
//@{
/// default constructor
BigFloat() : RCBigFloat(new BigFloatRep()) {}
/// constructor for <tt>short</tt>
BigFloat(short i) : RCBigFloat(new BigFloatRep(i)) {}
/// constructor for <tt>float</tt>
BigFloat(float i) : RCBigFloat(new BigFloatRep(i)) {}
/// constructor for <tt>int</tt>
BigFloat(int i) : RCBigFloat(new BigFloatRep(i)) {}
/// constructor for <tt>unsigned int</tt>
BigFloat(unsigned int i) : RCBigFloat(new BigFloatRep(i)) {}
/// constructor for <tt>long</tt>
BigFloat(long l) : RCBigFloat(new BigFloatRep(l)) {}
/// constructor for <tt>double</tt>
BigFloat(double d) : RCBigFloat(new BigFloatRep(d)) {}
/// constructor for <tt>const char* </tt>(default base = 10)
BigFloat(const char* s) : RCBigFloat(new BigFloatRep(s)) {}
/// constructor for <tt>std::string</tt>(default base = 10)
BigFloat(const std::string& s) : RCBigFloat(new BigFloatRep(s)) {}
/// constructor for <tt>int</tt> and <tt>long</tt>
// This is a hack because in Sturm, we need to approximate any
// coefficient type NT to a BigFloat, and it would complain if we
// do not have this method explicitly:
BigFloat(int& i, const extLong& /*r*/, const extLong& /*a*/)
: RCBigFloat(new BigFloatRep(i)) {}
BigFloat(long& x, const extLong& /*r*/, const extLong& /*a*/)
: RCBigFloat(new BigFloatRep(x)) {}
/// constructor from <tt>BigInt</tt>, error and exponent values
BigFloat(const BigInt& I, unsigned long er, long ex)
: RCBigFloat(new BigFloatRep(I, er, ex)) {}
/// constructor from <tt>BigInt</tt>, exponent values
BigFloat(const BigInt& I, long ex)
: RCBigFloat(new BigFloatRep(I, ex)) {}
BigFloat(const BigInt& I)
: RCBigFloat(new BigFloatRep(I)) {}
/// constructor for <tt>BigRat</tt>
BigFloat(const BigRat& R, const extLong& r = get_static_defRelPrec(),
const extLong& a = get_static_defAbsPrec())
: RCBigFloat(new BigFloatRep()) {
rep->approx(R, r, a);
}
// REMARK: it is somewhat against our principles to have BigFloat
// know about Expr, but BigFloat has a special role in our system!
// ===============================
/// constructor for <tt>Expr</tt>
explicit BigFloat(const Expr& E, const extLong& r = get_static_defRelPrec(),
const extLong& a = get_static_defAbsPrec());
//Dummy
explicit BigFloat(const BigFloat& E, const extLong& ,
const extLong&): RCBigFloat(E) {
rep->incRef();
}
/// constructor for <tt>BigFloatRep</tt>
explicit BigFloat(BigFloatRep* r) : RCBigFloat(new BigFloatRep()) {
rep = r;
}
// constructor for exp2
explicit BigFloat(BigFloatRep* r, bool) : RCBigFloat(r) {
}
//@}
/// \name Copy-Assignment-Destructor
//@{
/// copy constructor
BigFloat(const BigFloat& rhs) : RCBigFloat(rhs) {
rep->incRef();
}
/// assignment operator
BigFloat& operator=(const BigFloat& rhs) {
if (this != &rhs) {
rep->decRef();
rep = rhs.rep;
rep->incRef();
}
return *this;
}
/// destructor
~BigFloat() {
rep->decRef();
}
//@}
/// \name Compound Assignment Operators
//@{
/// operator+=
BigFloat& operator+= (const BigFloat& x) {
BigFloat z;
z.rep->add(*rep, *x.rep);
*this = z;
return *this;
}
/// operator-=
BigFloat& operator-= (const BigFloat& x) {
BigFloat z;
z.rep->sub(*rep, *x.rep);
*this = z;
return *this;
}
/// operator*=
BigFloat& operator*= (const BigFloat& x) {
BigFloat z;
z.rep->mul(*rep, *x.rep);
*this = z;
return *this;
}
/// operator/=
BigFloat& operator/= (const BigFloat& x) {
BigFloat z;
z.rep->div(*rep, *x.rep, get_static_defBFdivRelPrec());
*this = z;
return *this;
}
//@}
/// \name Unary Minus Operator
//@{
/// unary plus
BigFloat operator+() const {
return BigFloat(*this);
}
/// unary minus
BigFloat operator-() const {
return BigFloat(-rep->m, rep->err, rep->exp);
}
//@}
/// \name String Conversion Functions
//@{
/// set value from <tt>const char*</tt> (base = 10)
void fromString(const char* s, extLong p = getBigFloatInputDigits()) {
rep->fromString(s, p);
}
/// convert to <tt>std::string</tt> (base = 10)
std::string toString(long prec=get_static_defBigFloatOutputDigits(), bool sci=false) const {
return rep->toString(prec, sci);
}
std::string str() const {
return toString();
}
//@}
/// \name Conversion Functions
//@{
/// return int value
int intValue() const {
return static_cast<int>(rep->toLong());
}
/// return long value
long longValue() const {
long l = rep->toLong();
if ((l == LONG_MAX) || (l == LONG_MIN))
return l; // return the overflown value.
if ((sign() < 0) && (cmp(BigFloat(l)) != 0)) {
// a negative value not exactly rounded.
l--; // rounded to floor.
}
return l;
}
/// return float value
float floatValue() const {
return static_cast<float>(rep->toDouble());
}
/// return double value
double doubleValue() const {
return rep->toDouble();
}
/// return BigInt value
BigInt BigIntValue() const {
return rep->toBigInt();
}
/// return BigRat value
BigRat BigRatValue() const {
return rep->BigRatize();
}
//@}
/// \name Helper Functions
//@{
/// Has Exact Division
static bool hasExactDivision() {
return false;
}
//CONSTANTS
/// return BigFloat(0)
static const BigFloat& getZero();
/// return BigFloat(1)
static const BigFloat& getOne();
/// sign function
/** \note This is only the sign of the mantissa, it can be taken to be
the sign of the BigFloat only if !(isZeroIn()). */
int sign() const {
CGAL_assertion((err() == 0 && m() == 0) || !(isZeroIn()));
return rep->signM();
}
/// check whether contains zero
/** \return true if contains zero, otherwise false */
bool isZeroIn() const {
return rep->isZeroIn();
}
/// absolute value function
BigFloat abs() const {
return (sign()>0) ? +(*this) : -(*this);
}
/// comparison function
int cmp(const BigFloat& x) const {
return rep->compareMExp(*x.rep);
}
/// get mantissa
const BigInt& m() const {
return rep->m;
}
/// get error bits
unsigned long err() const {
return rep->err;
}
/// get exponent
long exp() const {
return rep->exp;
}
/// check whether err == 0
/** \return true if err == 0, otherwise false */
bool isExact() const {
return rep->err == 0;
}
/// set err to 0
/** \return an exact BigFloat, see Tutorial for why this is useful! */
BigFloat& makeExact() {
makeCopy();
rep->err =0;
return *this;
}
/// set err to 0, but first add err to the mantissa (m)
/** \return the ceiling exact BigFloat, variant of makeExact */
BigFloat& makeCeilExact() {
makeCopy();
rep->m += rep->err;
rep->err =0;
return *this;
}
/// set err to 0, but subtract err from the mantissa (m)
/** \return the floor exact BigFloat, variant of makeExact */
BigFloat& makeFloorExact() {
makeCopy();
rep->m -= rep->err;
rep->err =0;
return *this;
}
/// set err to 1
/** \return an inexact BigFloat, see Tutorial for why this is useful! */
BigFloat& makeInexact() {
makeCopy();
rep->err =1;
return *this;
}
/// return lower bound of Most Significant Bit
extLong lMSB() const {
return rep->lMSB();
}
/// return upper bound of Most Significant Bit
extLong uMSB() const {
return rep->uMSB();
}
/// return Most Significant Bit
extLong MSB() const {
return rep->MSB();
}
/// floor of Lg(err)
extLong flrLgErr() const {
return rep->flrLgErr();
}
/// ceil of Lg(err)
extLong clLgErr() const {
return rep->clLgErr();
}
/// division with relative precsion <tt>r</tt>
BigFloat div(const BigFloat& x, const extLong& r) const {
BigFloat y;
y.rep->div(*rep, *x.rep, r);
return y;
}
/// exact division by 2
BigFloat div2() const {
BigFloat y;
y.rep->div2(*rep);
return y;
}
/// squareroot
BigFloat sqrt(const extLong& a) const {
BigFloat x;
x.rep->sqrt(*rep, a);
return x;
}
/// squareroot with initial approximation <tt>init</tt>
BigFloat sqrt(const extLong& a, const BigFloat& init) const {
BigFloat x;
x.rep->sqrt(*rep, a, init);
return x;
}
//@}
/// \name Utility Functions
//@{
/// approximate BigInt number
void approx(const BigInt& I, const extLong& r, const extLong& a) {
makeCopy();
rep->trunc(I, r, a);
}
/// approximate BigFloat number
void approx(const BigFloat& B, const extLong& r, const extLong& a) {
makeCopy();
rep->approx(*B.rep, r, a);
}
/// approximate BigRat number
void approx(const BigRat& R, const extLong& r, const extLong& a) {
makeCopy();
rep->approx(R, r, a);
}
/// dump internal data
void dump() const {
rep->dump();
}
//@}
/// returns a BigFloat of value \f$ 2^e \f$
static BigFloat exp2(int e) {
return BigFloat(BigFloatRep::exp2(e),true);
}
}; // class BigFloat
//@} // For compatibility with BigInt
/// \name File I/O Functions
//@{
/// read from file
void readFromFile(BigFloat& bf, std::istream& in, long maxLength = 0);
/// write to file
void writeToFile(const BigFloat& bf, std::ostream& in,
int base=10, int charsPerLine=80);
/// IO stream operator<<
inline std::ostream& operator<< (std::ostream& o, const BigFloat& x) {
x.getRep().operator<<(o);
return o;
}
/// IO stream operator>>
inline std::istream& operator>> (std::istream& i, BigFloat& x) {
x.makeCopy();
x.getRep().operator>>(i);
return i;
}
//@}
/// operator+
inline BigFloat operator+ (const BigFloat& x, const BigFloat& y) {
BigFloat z;
z.getRep().add(x.getRep(), y.getRep());
return z;
}
/// operator-
inline BigFloat operator- (const BigFloat& x, const BigFloat& y) {
BigFloat z;
z.getRep().sub(x.getRep(), y.getRep());
return z;
}
/// operator*
inline BigFloat operator* (const BigFloat& x, const BigFloat& y) {
BigFloat z;
z.getRep().mul(x.getRep(), y.getRep());
return z;
}
/// operator/
inline BigFloat operator/ (const BigFloat& x, const BigFloat& y) {
BigFloat z;
z.getRep().div(x.getRep(),y.getRep(),get_static_defBFdivRelPrec());
return z;
}
/// operator==
inline bool operator== (const BigFloat& x, const BigFloat& y) {
return x.cmp(y) == 0;
}
/// operator!=
inline bool operator!= (const BigFloat& x, const BigFloat& y) {
return x.cmp(y) != 0;
}
/// operator>=
inline bool operator>= (const BigFloat& x, const BigFloat& y) {
return x.cmp(y) >= 0;
}
/// operator>
inline bool operator> (const BigFloat& x, const BigFloat& y) {
return x.cmp(y) > 0;
}
/// operator<=
inline bool operator<= (const BigFloat& x, const BigFloat& y) {
return x.cmp(y) <= 0;
}
/// operator<
inline bool operator< (const BigFloat& x, const BigFloat& y) {
return x.cmp(y) < 0;
}
/// sign
inline int sign(const BigFloat& x) {
return x.sign();
}
/// div2
inline BigFloat div2(const BigFloat& x){
return x.div2();
}
/// abs
inline BigFloat abs(const BigFloat& x) {
return x.abs();
}
/// cmp
inline int cmp(const BigFloat& x, const BigFloat& y) {
return x.cmp(y);
}
/// pow
CGAL_CORE_EXPORT BigFloat pow(const BigFloat&, unsigned long);
/// power
inline BigFloat power(const BigFloat& x, unsigned long p) {
return pow(x, p);
}
/// root(x,k,prec,xx) returns the k-th root of x to precision prec.
/// The argument x is an initial approximation.
BigFloat root(const BigFloat&, unsigned long k, const extLong&, const BigFloat&);
inline BigFloat root(const BigFloat& x, unsigned long k) {
return root(x, k, get_static_defBFsqrtAbsPrec(), x);
}
/// sqrt to defAbsPrec:
inline BigFloat sqrt(const BigFloat& x) {
return x.sqrt(get_static_defBFsqrtAbsPrec());
}
/// convert an BigFloat Interval to a BigFloat with error bits
inline BigFloat centerize(const BigFloat& a, const BigFloat& b) {
BigFloat z;
z.getRep().centerize(a.getRep(), b.getRep());
return z;
}
/// minStar(m,n) returns the min-star of m and n
inline long minStar(long m, long n) {
if (m*n <= 0) return 0;
if (m>0)
return core_min(m, n);
else
return core_max(m, n);
}
/// \name Functions for Compatibility with BigInt (needed by Poly, Curves)
//@{
/// isDivisible(a,b) = "is a divisible by b"
/** Assuming that a and b are in coanonized forms.
Defined to be true if mantissa(b) | mantissa(a) &&
exp(b) = min*(exp(b), exp(a)).
* This concepts assume a and b are exact BigFloats.
*/
inline bool isDivisible(const BigFloat& a, const BigFloat& b) {
// assert: a and b are exact BigFloats.
if (sign(a.m()) == 0) return true;
if (sign(b.m()) == 0) return false;
unsigned long bin_a = getBinExpo(a.m());
unsigned long bin_b = getBinExpo(b.m());
BigInt m_a = a.m() >> bin_a;
BigInt m_b = b.m() >> bin_b;
long e_a = bin_a + BigFloatRep::bits(a.exp());
long e_b = bin_b + BigFloatRep::bits(b.exp());
long dx = minStar(e_a, e_b);
return isDivisible(m_a, m_b) && (dx == e_b);
}
inline bool isDivisible(double x, double y) {
//Are these exact?
return isDivisible(BigFloat(x), BigFloat(y));
}
/// div_exact(x,y) returns the BigFloat quotient of x divided by y
/** This is defined only if isDivisible(x,y).
*/
// Chee (8/1/2004) The definition of div_exact(x,y)
// ensure that Polynomials<NT> works with NT=BigFloat and NT=double.
//Bug: We should first normalize the mantissas of the Bigfloats and
//then do the BigInt division. For e.g. 1 can be written as 2^{14}*2{-14}.
//Now if we divide 2 by one using this representation of one and without
// normalizing it then we get zero.
inline BigFloat div_exact(const BigFloat& x, const BigFloat& y) {
BigInt z;
CGAL_assertion (isDivisible(x,y));
unsigned long bin_x = getBinExpo(x.m());
unsigned long bin_y = getBinExpo(y.m());
BigInt m_x = x.m() >> bin_x;
BigInt m_y = y.m() >> bin_y;
long e_x = bin_x + BigFloatRep::bits(x.exp());
long e_y = bin_y + BigFloatRep::bits(y.exp());
//Since y divides x, e_y = minstar(e_x, e_y)
z = div_exact(m_x, m_y);
// mpz_divexact(z.get_mp(), x.m().get_mp(), y.m().get_mp()); THIS WAS THE BUG
// assert: x.exp() - y.exp() does not under- or over-flow.
return BigFloat(z, e_x - e_y);
}
inline BigFloat div_exact(double x, double y) {
return div_exact(BigFloat(x), BigFloat(y));
}
// Remark: there is another notion of "exact division" for BigFloats,
// and that is to make the division return an "exact" BigFloat
// i.e., err()=0.
/// gcd(a,b) = BigFloat(gcd(a.mantissa,b.matissa), min(a.exp(), b.exp()) )
inline BigFloat gcd(const BigFloat& a, const BigFloat& b) {
if (sign(a.m()) == 0) return core_abs(b);
if (sign(b.m()) == 0) return core_abs(a);
BigInt r;
long dx;
unsigned long bin_a = getBinExpo(a.m());
unsigned long bin_b = getBinExpo(b.m());
/* THE FOLLOWING IS ALTERNATIVE CODE, for GCD using base B=2^{14}:
*std::cout << "bin_a=" << bin_a << ",bin_b=" << bin_b << std::endl;
std::cout << "a.exp()=" << a.exp() << ",b.exp()=" << b.exp() << std::endl;
long chunk_a = BigFloatRep::chunkFloor(bin_a);
long chunk_b = BigFloatRep::chunkFloor(bin_b);
BigInt m_a = BigFloatRep::chunkShift(a.m(), chunk_a);
BigInt m_b = BigFloatRep::chunkShift(b.m(), chunk_b);
r = gcd(m_a, m_b);
dx = minStar(chunk_a + a.exp(), chunk_b + b.exp());
*/
BigInt m_a = a.m() >> bin_a;
BigInt m_b = b.m() >> bin_b;
r = gcd(m_a, m_b);
dx = minStar(bin_a + BigFloatRep::bits(a.exp()),
bin_b + BigFloatRep::bits(b.exp()));
long chunks = BigFloatRep::chunkFloor(dx);
r <<= (dx - BigFloatRep::bits(chunks));
dx = chunks;
return BigFloat(r, 0, dx);
}
// Not needed for now:
/// div_rem
// inline void div_rem(BigFloat& q, BigFloat& r,
// const BigFloat& a, const BigFloat& b) {
//q.makeCopy();
//r.makeCopy();
//mpz_tdiv_qr(q.get_mp(), r.get_mp(), a.get_mp(), b.get_mp());
//}//
// constructor BigRat from BigFloat
inline BigRat::BigRat(const BigFloat& f) : RCBigRat(new BigRatRep()){
*this = f.BigRatValue();
}
} //namespace CORE
#ifdef CGAL_HEADER_ONLY
#include <CGAL/CORE/BigFloat_impl.h>
#include <CGAL/CORE/CoreIO_impl.h>
#endif // CGAL_HEADER_ONLY
#endif // _CORE_BIGFLOAT_H_