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
|
// Copyright (c) 2001-2004 Utrecht University (The Netherlands),
// ETH Zurich (Switzerland), Freie Universitaet Berlin (Germany),
// INRIA Sophia-Antipolis (France), Martin-Luther-University Halle-Wittenberg
// (Germany), Max-Planck-Institute Saarbruecken (Germany), RISC Linz (Austria),
// and Tel-Aviv University (Israel). 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; version 2.1 of the License.
// See the file LICENSE.LGPL distributed with CGAL.
//
// 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.
//
// $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.2-branch/Number_types/include/CGAL/MP_Float.h $
// $Id: MP_Float.h 31195 2006-05-19 09:28:42Z spion $
//
//
// Author(s) : Sylvain Pion
#ifndef CGAL_MP_FLOAT_H
#define CGAL_MP_FLOAT_H
#include <CGAL/basic.h>
#include <CGAL/Interval_nt.h>
#include <CGAL/MP_Float_fwd.h>
#include <iostream>
#include <vector>
#include <algorithm>
// MP_Float : multiprecision scaled integers.
// Some invariants on the internal representation :
// - zero is represented by an empty vector, and whatever exp.
// - no leading or trailing zero in the vector => unique
// The main algorithms are :
// - Addition/Subtraction
// - Multiplication
// - Comparison
// - to_double() / to_interval()
// - Construction from a double.
// - IOs
// TODO :
// - The exponent really overflows sometimes -> make it multiprecision.
// - Write a generic wrapper that adds an exponent to be used by MP integers.
// - Karatsuba (or other) ? Would be fun to implement at least.
// - Division, sqrt... : different options :
// - nothing
// - convert to double, take approximation, compute over double, reconstruct
// - exact division as separate function (for Bareiss...)
// - returns the quotient of the division, forgetting the rest.
CGAL_BEGIN_NAMESPACE
class MP_Float;
MP_Float operator+(const MP_Float &a, const MP_Float &b);
MP_Float operator-(const MP_Float &a, const MP_Float &b);
MP_Float operator*(const MP_Float &a, const MP_Float &b);
#ifdef CGAL_MP_FLOAT_ALLOW_INEXACT
MP_Float operator/(const MP_Float &a, const MP_Float &b);
#endif
Comparison_result
compare (const MP_Float & a, const MP_Float & b);
class MP_Float
{
public:
typedef Tag_false Has_gcd;
#ifdef CGAL_MP_FLOAT_ALLOW_INEXACT
typedef Tag_true Has_division;
typedef Tag_true Has_sqrt;
#else
typedef Tag_false Has_division;
typedef Tag_false Has_sqrt;
#endif
typedef Tag_true Has_exact_ring_operations;
typedef Tag_false Has_exact_division;
typedef Tag_false Has_exact_sqrt;
typedef short limb;
typedef int limb2;
typedef double exponent_type;
typedef std::vector<limb> V;
typedef V::const_iterator const_iterator;
typedef V::iterator iterator;
private:
void remove_leading_zeros()
{
while (!v.empty() && v.back() == 0)
v.pop_back();
}
void remove_trailing_zeros()
{
if (v.empty() || v.front() != 0)
return;
iterator i = v.begin();
for (++i; *i == 0; ++i)
;
exp += i-v.begin();
v.erase(v.begin(), i);
}
// This union is used to convert an unsigned short to a short with
// the same binary representation, without invoking implementation-defined
// behavior (standard 4.7.3).
// It is needed by PGCC, which behaves differently from the others.
union to_signed {
unsigned short us;
short s;
};
// The constructors from float/double/long_double are factorized in the
// following template :
template < typename T >
void construct_from_builtin_fp_type(T d);
public:
// Splits a limb2 into 2 limbs (high and low).
static
void split(limb2 l, limb & high, limb & low)
{
to_signed l2 = {l};
low = l2.s;
high = (l - low) >> (8*sizeof(limb));
}
// Given a limb2, returns the higher limb.
static
limb higher_limb(limb2 l)
{
limb high, low;
split(l, high, low);
return high;
}
void canonicalize()
{
remove_leading_zeros();
remove_trailing_zeros();
}
MP_Float()
: exp(0)
{
CGAL_assertion(sizeof(limb2) == 2*sizeof(limb));
CGAL_assertion(v.empty());
// Creates zero.
}
#if 0
// Causes ambiguities
MP_Float(limb i)
: v(1,i), exp(0)
{
remove_leading_zeros();
}
#endif
MP_Float(limb2 i)
: v(2), exp(0)
{
split(i, v[1], v[0]);
canonicalize();
}
MP_Float(float d);
MP_Float(double d);
MP_Float(long double d);
MP_Float operator-() const
{
return MP_Float() - *this;
}
MP_Float& operator+=(const MP_Float &a) { return *this = *this + a; }
MP_Float& operator-=(const MP_Float &a) { return *this = *this - a; }
MP_Float& operator*=(const MP_Float &a) { return *this = *this * a; }
#ifdef CGAL_MP_FLOAT_ALLOW_INEXACT
MP_Float& operator/=(const MP_Float &a) { return *this = *this / a; }
#endif
exponent_type max_exp() const
{
return v.size() + exp;
}
exponent_type min_exp() const
{
return exp;
}
limb of_exp(exponent_type i) const
{
if (i < exp || i >= max_exp())
return 0;
return v[static_cast<int>(i-exp)];
}
bool is_zero() const
{
return v.empty();
}
Sign sign() const
{
if (v.empty())
return ZERO;
if (v.back()>0)
return POSITIVE;
CGAL_assertion(v.back()<0);
return NEGATIVE;
}
void swap(MP_Float &m)
{
std::swap(v, m.v);
std::swap(exp, m.exp);
}
// Converts to a rational type (e.g. Gmpq).
template < typename T >
T to_rational() const
{
const unsigned log_limb = 8 * sizeof(MP_Float::limb);
if (is_zero())
return 0;
MP_Float::const_iterator i;
exponent_type exp = min_exp() * log_limb;
T res = 0;
for (i = v.begin(); i != v.end(); i++)
{
res += CGAL_CLIB_STD::ldexp(static_cast<double>(*i),
static_cast<int>(exp));
exp += log_limb;
}
return res;
}
V v;
exponent_type exp;
};
inline
void swap(MP_Float &m, MP_Float &n)
{ m.swap(n); }
inline
bool operator<(const MP_Float &a, const MP_Float &b)
{ return CGAL_NTS compare(a, b) == SMALLER; }
inline
bool operator>(const MP_Float &a, const MP_Float &b)
{ return b < a; }
inline
bool operator>=(const MP_Float &a, const MP_Float &b)
{ return ! (a < b); }
inline
bool operator<=(const MP_Float &a, const MP_Float &b)
{ return ! (a > b); }
inline
bool operator==(const MP_Float &a, const MP_Float &b)
{ return (a.v == b.v) && (a.v.empty() || (a.exp == b.exp)); }
inline
bool operator!=(const MP_Float &a, const MP_Float &b)
{ return ! (a == b); }
inline
Sign
sign (const MP_Float &a)
{
return a.sign();
}
MP_Float
square(const MP_Float&);
MP_Float
approximate_sqrt(const MP_Float &d);
MP_Float
approximate_division(const MP_Float &n, const MP_Float &d);
#ifdef CGAL_MP_FLOAT_ALLOW_INEXACT
inline
MP_Float
operator/(const MP_Float &a, const MP_Float &b)
{
return approximate_division(a, b);
}
inline
MP_Float
sqrt(const MP_Float &d)
{
return approximate_sqrt(d);
}
#endif
// to_double() returns, not the closest double, but a one bit error is allowed.
// We guarantee : to_double(MP_Float(double d)) == d.
double
to_double(const MP_Float &b);
std::pair<double,double>
to_interval(const MP_Float &b);
template < typename > class Quotient;
// Overloaded in order to protect against overflow.
double
to_double(const Quotient<MP_Float> &b);
std::pair<double, double>
to_interval(const Quotient<MP_Float> &b);
inline
void
simplify_quotient(MP_Float & numerator, MP_Float & denominator)
{
// Currently only simplifies the two exponents.
#if 0
// This better version causes problems as the I/O is changed for
// Quotient<MP_Float>, which then does not appear as rational 123/345,
// 1.23/3.45, this causes problems in the T2 test-suite (to be investigated).
numerator.exp -= denominator.exp
+ (MP_Float::exponent_type) denominator.v.size();
denominator.exp = - (MP_Float::exponent_type) denominator.v.size();
#else
numerator.exp -= denominator.exp;
denominator.exp = 0;
#endif
}
inline
bool
is_finite(const MP_Float &)
{
return true;
}
inline
bool
is_valid(const MP_Float &)
{
return true;
}
inline
io_Operator
io_tag(const MP_Float &)
{
return io_Operator();
}
std::ostream &
operator<< (std::ostream & os, const MP_Float &b);
// This one is for debug.
std::ostream &
print (std::ostream & os, const MP_Float &b);
std::istream &
operator>> (std::istream & is, MP_Float &b);
CGAL_END_NAMESPACE
#endif // CGAL_MP_FLOAT_H
|