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
|
// Copyright (c) 2006-2008 Max-Planck-Institute Saarbruecken (Germany).
// 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.6-branch/Number_types/include/CGAL/CORE_BigFloat.h $
// $Id: CORE_BigFloat.h 51456 2009-08-24 17:10:04Z spion $
//
//
// Author(s) : Michael Hemmer <hemmer@mpi-inf.mpg.de>
//============================================================================
#ifndef CGAL_CORE_BIGFLOAT_H
#define CGAL_CORE_BIGFLOAT_H
#include <CGAL/basic.h>
#include <CGAL/number_type_basic.h>
#include <CGAL/CORE/BigFloat.h>
#include <CGAL/CORE_coercion_traits.h>
#include <CGAL/Interval_traits.h>
#include <CGAL/Bigfloat_interval_traits.h>
CGAL_BEGIN_NAMESPACE
// ######### Interval_traits
template<>
class Interval_traits<CORE::BigFloat>
: public internal::Interval_traits_base<CORE::BigFloat>{
public:
typedef Interval_traits<CORE::BigFloat> Self;
typedef CORE::BigFloat Interval;
typedef CORE::BigFloat Bound;
typedef CGAL::Tag_true Is_interval;
struct Lower :public std::unary_function<Interval,Bound>{
Bound operator() ( Interval x ) const {
CORE::BigFloat result = ::CORE::BigFloat(x.m()-x.err(),0,x.exp());
CGAL_postcondition(result <= x);
return result;
}
};
struct Upper :public std::unary_function<Interval,Bound>{
Bound operator() ( Interval x ) const {
CORE::BigFloat result = ::CORE::BigFloat(x.m()+x.err(),0,x.exp());
CGAL_postcondition(result >= x);
return result;
}
};
struct Width :public std::unary_function<Interval,Bound>{
Bound operator() ( Interval x ) const {
unsigned long err = 2*x.err();
return Bound(CORE::BigInt(err),0,x.exp());
}
};
struct Median :public std::unary_function<Interval,Bound>{
Bound operator() ( Interval x ) const {
return Bound(x.m(),0,x.exp());
}
};
struct Norm :public std::unary_function<Interval,Bound>{
Bound operator() ( Interval x ) const {
BOOST_USING_STD_MAX();
return max BOOST_PREVENT_MACRO_SUBSTITUTION (Upper()(x).abs(),Lower()(x).abs());
}
};
struct Zero_in :public std::unary_function<Interval,bool>{
bool operator() ( Interval x ) const {
return x.isZeroIn();
}
};
struct In :public std::binary_function<Bound,Interval,bool>{
bool operator()( Bound x, const Interval& a ) const {
CGAL_precondition(CGAL::singleton(x));
return (Lower()(a) <= x && x <= Upper()(a));
}
};
struct Equal :public std::binary_function<Interval,Interval,bool>{
bool operator()( const Interval& a, const Interval& b ) const {
return (Upper()(a) == Upper()(b) && Lower()(a) == Lower()(b));
}
};
struct Subset :public std::binary_function<Interval,Interval,bool>{
bool operator()( const Interval& a, const Interval& b ) const {
return Lower()(b) <= Lower()(a) && Upper()(a) <= Upper()(b);
}
};
struct Proper_subset :public std::binary_function<Interval,Interval,bool>{
bool operator()( const Interval& a, const Interval& b ) const {
return Subset()(a,b) && (!Equal()(a,b));
}
};
struct Intersection :public std::binary_function<Interval,Interval,Interval>{
Interval operator()( const Interval& a, const Interval& b ) const {
BOOST_USING_STD_MAX();
BOOST_USING_STD_MIN();
// std::cout <<"a= (" << a.m() << "+-" << a.err() << ")*2^" << a.exp() << std::endl;
Bound l(max BOOST_PREVENT_MACRO_SUBSTITUTION (Lower()(a),Lower()(b)));
Bound u(min BOOST_PREVENT_MACRO_SUBSTITUTION (Upper()(a),Upper()(b)));
if(u < l ) throw Exception_intersection_is_empty();
return Construct()(l,u);
}
};
struct Overlap :public std::binary_function<Interval,Interval,bool>{
bool operator() ( Interval x, Interval y ) const {
Self::Zero_in Zero_in;
bool result = Zero_in(x-y);
return result;
}
};
struct Hull :public std::binary_function<Interval,Interval,Interval>{
/* for debugging
void print_bf(CORE::BigFloat bf, std::string s) const {
std::cout << s << ".m()=" << bf.m() << ","
<< s << ".err()=" << bf.err() << ","
<< s << ".exp()=" << bf.exp() << ","
<< "td=" << bf << std::endl;
}
*/
Interval operator() ( Interval x, Interval y ) const {
BOOST_USING_STD_MAX();
BOOST_USING_STD_MIN();
#if 0
// this is not possible since CORE::centerize has a bug.
Interval result = CORE::centerize(x,y);
#else
//print_bf(x,"x");
//print_bf(y,"y");
CORE::BigFloat result;
// Unfortunately, CORE::centerize(x,y) has bugs.
if ((x.m() == y.m()) && (x.err() == y.err()) && (x.exp() == y.exp())) {
return x;
}
CORE::BigFloat lower = min BOOST_PREVENT_MACRO_SUBSTITUTION (CGAL::lower(x), CGAL::lower(y));
CORE::BigFloat upper = max BOOST_PREVENT_MACRO_SUBSTITUTION (CGAL::upper(x), CGAL::upper(y));
CORE::BigFloat mid = (lower + upper)/2;
//print_bf(lower,"lower");
//print_bf(upper,"upper");
//print_bf(mid,"mid");
// Now we have to compute the error. The problem is that .err() is just a long
CORE::BigFloat err = (upper - lower)/CORE::BigFloat(2);
//print_bf(err,"err");
//std::cout << "lower " << lower << std::endl;
//std::cout << "upper " << upper << std::endl;
//std::cout << "mid " << mid << std::endl;
//std::cout << "err I " << err << std::endl;
// shift such that err.m()+err.err() fits into long
int digits_long = std::numeric_limits<long>::digits;
if(::CORE::bitLength(err.m()+err.err()) >= digits_long){
long shift = ::CORE::bitLength(err.m()) - digits_long + 1 ;
//std::cout << "shift " << shift<< std::endl;
long new_err = ((err.m()+err.err()) >> shift).longValue()+1;
err = CORE::BigFloat(0,new_err,0) * CORE::BigFloat::exp2(err.exp()*14+shift);
}else{
err = CORE::BigFloat(0,err.m().longValue()+err.err(),err.exp());
}
//print_bf(err,"new_err");
// TODO: This is a workaround for a bug in operator+
// of CORE::Bigfloat. If the exponent difference is too big,
// this might cause problems, since the error is a long
if(mid.exp() > err.exp()) {
long mid_err = mid.err();
CORE::BigInt mid_m = mid.m();
mid_err = mid_err << (mid.exp()-err.exp())*14;
mid_m = mid_m << (mid.exp()-err.exp())*14;
mid = CORE::BigFloat(mid_m,mid_err,err.exp());
//print_bf(mid,"corr_mid");
}
//print_bf(result,"result");
result = mid + err;
#endif
CGAL_postcondition(
CGAL::lower(result)
<= min BOOST_PREVENT_MACRO_SUBSTITUTION (CGAL::lower(x), CGAL::lower(y)));
CGAL_postcondition(
CGAL::upper(result)
>= max BOOST_PREVENT_MACRO_SUBSTITUTION (CGAL::upper(x), CGAL::upper(y)));
return result ;
}
};
struct Singleton :public std::unary_function<Interval,bool> {
bool operator() ( Interval x ) const {
return (x.err() == 0);
}
};
struct Construct :public std::binary_function<Bound,Bound,Interval>{
Interval operator()( const Bound& l,const Bound& r) const {
CGAL_precondition( l < r );
return Hull()(l,r);
}
};
};
// ########### Bigfloat_interval_traits
template<typename BFI> long get_significant_bits(BFI bfi);
CORE::BigFloat
inline
round(const CORE::BigFloat& x, long rel_prec = CORE::defRelPrec.toLong() ){
CGAL_postcondition(rel_prec >= 0);
// since there is not rel prec defined if Zero_in(x)
if (x.isZeroIn()) return x;
if (CGAL::get_significant_bits(x) <= rel_prec) return x;
// if 1
// CORE::BigFloat xr;
// xr.approx(x,rel_prec,1024);
// typedef CORE::BigFloat BF;
// else
typedef CORE::BigFloat BF;
typedef CORE::BigFloat BFI;
typedef CORE::BigInt Integer;
BF xr;
CORE::BigInt m = x.m();
long err = x.err();
long exp = x.exp();
long shift = ::CORE::bitLength(m) - rel_prec - 1;
if( shift > 0 ){ Integer new_m = m >> shift ;
if(err == 0){ xr = BF(new_m,1,0)*BF::exp2(exp*14+shift);
}else{ xr = BF(new_m,2,0)*BF::exp2(exp*14+shift);
}
}else{ // noting to do
xr = x;
}
// endif
CGAL_postcondition(CGAL::get_significant_bits(xr) - rel_prec >= 0);
CGAL_postcondition(CGAL::get_significant_bits(xr) - rel_prec <= 32);
CGAL_postcondition(BF(xr.m()-xr.err(),0,xr.exp()) <= BF(x.m()-x.err(),0,x.exp()));
CGAL_postcondition(BF(xr.m()+xr.err(),0,xr.exp()) >= BF(x.m()+x.err(),0,x.exp()));
return xr;
}
template<> class Bigfloat_interval_traits<CORE::BigFloat>
:public Interval_traits<CORE::BigFloat>
{
public:
typedef CORE::BigFloat NT;
typedef CORE::BigFloat BF;
typedef Bigfloat_interval_traits<NT> Self;
// How about retuning
struct Get_significant_bits {
// type for the \c AdaptableUnaryFunction concept.
typedef NT argument_type;
// type for the \c AdaptableUnaryFunction concept.
typedef long result_type;
long operator()( NT x) const {
if(x.err() == 0 ) {
return ::CORE::bitLength(x.m());
}
else {
return ::CORE::bitLength(x.m()) - ::CORE::bitLength(x.err());
}
}
};
struct Set_precision {
// type for the \c AdaptableUnaryFunction concept.
typedef long argument_type;
// type for the \c AdaptableUnaryFunction concept.
typedef long result_type;
long operator() ( long prec ) const {
long result = ::CORE::defRelPrec.toLong();
::CORE::defRelPrec = prec;
::CORE::defBFdivRelPrec = prec;
return result;
}
};
struct Get_precision {
// type for the \c AdaptableGenerator concept.
typedef long result_type;
long operator() () const {
return ::CORE::defRelPrec.toLong();
}
};
};
//
// Algebraic structure traits
//
template <> class Algebraic_structure_traits< CORE::BigFloat >
: public Algebraic_structure_traits_base< CORE::BigFloat,
Field_with_kth_root_tag > {
public:
typedef Tag_false Is_exact;
typedef Tag_true Is_numerical_sensitive;
class Sqrt
: public std::unary_function< Type, Type > {
public:
Type operator()( const Type& x ) const {
// What I want is a sqrt computed with ::CORE::defRelPrec bits.
// And not ::CORE::defBFsqrtAbsPrec as CORE does.
CGAL_precondition(::CORE::defRelPrec.toLong() > 0);
CGAL_precondition(x > 0);
Type a = CGAL::round(x, ::CORE::defRelPrec.toLong()*2);
CGAL_postcondition(a > 0);
Type tmp1 =
CORE::BigFloat(a.m(),0,0).sqrt(::CORE::defRelPrec.toLong());
Type err =
Type(0,long(std::sqrt(double(a.err()))),0)
* CORE::BigFloat::exp2(a.exp()*7);
Type result = tmp1*CORE::BigFloat::exp2(a.exp()*7) + err;
CGAL_postcondition(result >= 0);
CGAL_postcondition(CGAL::lower(result*result) <= CGAL::lower(x));
CGAL_postcondition(CGAL::upper(result*result) >= CGAL::upper(x));
return result;
}
};
class Kth_root
: public std::binary_function<int, Type, Type> {
public:
Type operator()( int k,
const Type& x) const {
CGAL_precondition_msg( k > 0, "'k' must be positive for k-th roots");
// CORE::radical isn't implemented for negative values of x, so we
// have to handle this case separately
if( x < 0 && k%2 != 0) {
return Type(-CORE::radical( -x, k ) );
}
return Type( CORE::radical( x, k ) );
}
};
};
//
// Real embeddable traits
//
template <> class Real_embeddable_traits< CORE::BigFloat >
: public INTERN_RET::Real_embeddable_traits_base< CORE::BigFloat , CGAL::Tag_true > {
public:
class Abs
: public std::unary_function< Type, Type > {
public:
Type operator()( const Type& x ) const {
Type result;
if(x.isZeroIn()){
CORE::BigInt m;
if(x.m() < 0 ){
m = -(x.m()-x.err());
}else{
m = x.m()+x.err();
}
if(m % 2 == 1) m += 1;
Type upper(m,0,x.exp());
result = CORE::centerize(CORE::BigFloat(0),upper);
CGAL_postcondition(result.m()-result.err() <= 0);
if(result.m()-result.err() != 0){
result = this->operator()(result);
}
CGAL_postcondition(result.m()-result.err() == 0);
}else{
result = CORE::abs(x);
}
CGAL_postcondition(result.m()-result.err() >= 0);
CGAL_postcondition(Type(result.m()+result.err(),0,result.exp())
>= Type(x.m()+x.err(),0,x.exp()));
return result;
}
};
class Sgn
: public std::unary_function< Type, ::CGAL::Sign > {
public:
::CGAL::Sign operator()( const Type& x ) const {
::CGAL::Sign result = sign( x.sign());
return result;
}
};
class Compare
: public std::binary_function< Type, Type,
Comparison_result > {
public:
Comparison_result operator()( const Type& x,
const Type& y ) const {
return (Comparison_result) sign( (x-y).sign());
}
CGAL_IMPLICIT_INTEROPERABLE_BINARY_OPERATOR_WITH_RT( Type,
Comparison_result )
};
class To_double
: public std::unary_function< Type, double > {
public:
double operator()( const Type& x ) const {
// this call is required to get reasonable values for the double
// approximation
return x.doubleValue();
}
};
class To_interval
: public std::unary_function< Type, std::pair< double, double > > {
public:
std::pair<double, double> operator()( const Type& x ) const {
double lb,ub;
Type x_lower = CGAL::lower(CGAL::round(CGAL::lower(x),52));
Type x_upper = CGAL::upper(CGAL::round(CGAL::upper(x),52));
// since matissa has 52 bits only, conversion to double is exact
lb = x_lower.doubleValue();
CGAL_postcondition(lb == x_lower);
ub = x_upper.doubleValue();
CGAL_postcondition(ub == x_upper);
std::pair<double, double> result(lb,ub);
CGAL_postcondition( result.first <= CORE::Expr(CGAL::lower(x)));
CGAL_postcondition( result.second >= CORE::Expr(CGAL::upper(x)));
return result;
}
};
};
CGAL_END_NAMESPACE
//since types are included by CORE_coercion_traits.h:
#include <CGAL/CORE_Expr.h>
#include <CGAL/CORE_BigInt.h>
#include <CGAL/CORE_BigRat.h>
#include <CGAL/CORE_BigFloat.h>
#endif // CGAL_CORE_BIGFLOAT_H
|