File: Float_traits.h

package info (click to toggle)
cgal 6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 144,912 kB
  • sloc: cpp: 810,858; ansic: 208,477; sh: 493; python: 411; makefile: 286; javascript: 174
file content (168 lines) | stat: -rw-r--r-- 4,478 bytes parent folder | download
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
// Copyright (c) 2006-2009 Max-Planck-Institute Saarbruecken (Germany).
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org)
//
// $URL: https://github.com/CGAL/cgal/blob/v6.1/Algebraic_kernel_d/include/CGAL/Algebraic_kernel_d/Float_traits.h $
// $Id: include/CGAL/Algebraic_kernel_d/Float_traits.h b26b07a1242 $
// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial
//
//
// Author(s)     : Michael Hemmer <hemmer@mpi-inf.mpg.de>
//
// ============================================================================

// TODO: Some comments are original EXACUS comments and aren't adapted. So
//         they may be wrong now.

// TODO: should exponent type be long or Integer ?

#ifndef CGAL_ALGEBRAIC_KERNEL_D_FLOAT_TRAITS_H
#define CGAL_ALGEBRAIC_KERNEL_D_FLOAT_TRAITS_H

#include <CGAL/basic.h>

#if CGAL_USE_LEDA
#include <CGAL/leda_bigfloat.h>
#endif

#if CGAL_USE_CORE
#include <CGAL/CORE_BigFloat.h>
#endif

#if CGAL_USE_MPFR
#include <CGAL/Gmpfr.h>
#endif

#include <CGAL/ipower.h>


namespace CGAL {

namespace internal {

// Don't define default, results in more convenient compiler messages
template< class Type > class Float_traits;
// {
// public:
//   typedef Null_functor    Get_mantissa;
//   typedef Null_functor    Get_exponent;
//   typedef Null_functor    Mul_by_pow_of_2;
// };

#ifdef CGAL_USE_LEDA

// Specialization for leda_bigfloat
template<>
class Float_traits< leda_bigfloat > {
public:
  struct Get_mantissa
    : public CGAL::cpp98::unary_function< leda_bigfloat, leda_integer > {
    leda_integer operator()( const leda_bigfloat& x ) const {
      //std::cout << x.get_significant() << std::endl;
      return x.get_significant();
    }
  };

  struct Get_exponent
    : public CGAL::cpp98::unary_function< leda_bigfloat, long > {
    long operator()( const leda_bigfloat& x ) const {
      return x.get_exponent().to_long();
    }
  };

  struct Mul_by_pow_of_2
    : public CGAL::cpp98::binary_function< leda_bigfloat, long, leda_bigfloat> {
    leda_bigfloat operator()( const leda_bigfloat& a, long e ) const {
      return leda_bigfloat(a.get_significant(), a.get_exponent()+e);
    }
  };
};

#endif

#ifdef CGAL_USE_CORE

// Specialization for CORE::BigFloat
template<>
class Float_traits< CORE::BigFloat > {
public:

  struct Get_mantissa
    : public CGAL::cpp98::unary_function< CORE::BigFloat, CORE::BigInt > {
    CORE::BigInt operator()( const CORE::BigFloat& x ) const {
      return x.m();
    }
  };

  struct Get_exponent
    : public CGAL::cpp98::unary_function< CORE::BigFloat, long > {
    long operator()( const CORE::BigFloat& x ) const {
      return CORE::CHUNK_BIT*x.exp(); // The basis is 2^CORE::CHUNK_BIT
    }
  };

  struct Mul_by_pow_of_2
    : public CGAL::cpp98::binary_function
    < CORE::BigFloat, long , CORE::BigFloat> {
    CORE::BigFloat operator()( const CORE::BigFloat& a, long e ) const {
      return a*CORE::BigFloat::exp2(e);
    }
  };

};
#endif


#if CGAL_USE_MPFR
template<> class Float_traits< Gmpfr > {

  struct Get_mantissa_exponent
    : public CGAL::cpp98::unary_function< Gmpfr, std::pair<Gmpz,long> > {

    std::pair<Gmpz,long> operator()( const Gmpfr& x ) const {
      return x.to_integer_exp();
    }
  };
public:
  struct Get_mantissa
    : public CGAL::cpp98::unary_function< Gmpfr, Gmpz > {
    Gmpz operator()( const Gmpfr& x ) const {
      return Get_mantissa_exponent()(x).first;
    }
  };

  struct Get_exponent
    : public CGAL::cpp98::unary_function< Gmpfr, long > {
    long operator()( const Gmpfr& x ) const {
      return Get_mantissa_exponent()(x).second;
    }
  };

struct Mul_by_pow_of_2
  : public CGAL::cpp98::binary_function< Gmpfr, Gmpz, Gmpfr> {
  Gmpfr operator()( const Gmpfr& a, long e ) const {
    Gmpfr result(0,a.get_precision()); // just to get the prec of a
    if (e >= 0 ){
      mpfr_mul_2si (result.fr(), a.fr(), e, mpfr_get_default_rounding_mode());
      //std::cout << "INPUT   : "<< a <<"+" << e << std::endl;
      //std::cout << "result: "<< result << std::endl;
      //std::cout << "TRUTH   : "<< a * CGAL::ipower(Gmpfr(2),e) << std::endl;
      CGAL_postcondition(a * CGAL::ipower(Gmpfr(2),e) == result);
    }
    else{
      mpfr_div_2si (result.fr(), a.fr(), -e, mpfr_get_default_rounding_mode());
      CGAL_postcondition(a / CGAL::ipower(Gmpfr(2),-e) == result);
    }
    return result;
  }
};
};
#endif
} //namespace internal



} //namespace CGAL

#endif // CGAL_ALGEBRAIC_KERNEL_D_FLOAT_TRAITS_H