carl  24.04
Computer ARithmetic Library
MpfrTest.cpp
Go to the documentation of this file.
1 /**
2  * This file is for short testing - not intended for distribution.
3  * @file FloatExample.cpp
4  * @author Stefan Schupp <stefan.schupp@cs.rwth-aachen.de>
5  *
6  * @since 2013-12-16
7  * @version 2013-12-16
8  */
9 
11 
12 #ifdef USE_MPFR_FLOAT
13 
14 void compare() {
15  mpfr_t a;
16  mpfr_init2(a,53);
17  mpfr_set_ui(a,53, mpfr_rnd_t::MPFR_RNDN);
18 
19  mpfr_t b;
20  mpfr_init2(b,14);
21 
22  mpfr_set_prec(b, mpfr_get_prec(a));
23  mpfr_set(b, a, MPFR_RNDN);
24 
25  assert(b->_mpfr_prec == a->_mpfr_prec);
26  assert(b->_mpfr_sign = a->_mpfr_sign);
27  assert(b->_mpfr_exp = a->_mpfr_exp);
28 
29  std::size_t limbs = (std::size_t)std::ceil(double(a->_mpfr_prec)/double(mp_bits_per_limb));
30  while( limbs > 0 ){
31  assert(b->_mpfr_d[limbs-1] == a->_mpfr_d[limbs-1]);
32  --limbs;
33  }
34 }
35 
36 
37 void to_int(mpz_t intRep, mpfr_t a) {
38  std::cout << "Bits per limb " << mp_bits_per_limb << std::endl;
39  std::cout << "Number limbs " << std::ceil(double(a->_mpfr_prec)/double(mp_bits_per_limb)) << std::endl;
40  std::cout << "Precision is " << a->_mpfr_prec << std::endl;
41  std::cout << "Sign is " << a->_mpfr_sign << std::endl;
42  //std::cout << "Exponent is " << carl::binary(a->_mpfr_exp) << std::endl;
43  std::cout << "Exponent is " << a->_mpfr_exp << std::endl;
44  std::cout << "Min Exponent is " << mpfr_get_emin() << std::endl;
45  //std::cout << "Min Exponent is " << carl::binary(mpfr_get_emin()) << std::endl;
46  std::cout << "Scaled exponent: " << (a->_mpfr_exp + std::abs(mpfr_get_emin())) << std::endl;
47  //std::cout << "Scaled exponent: " << carl::binary((a->_mpfr_exp + std::abs(mpfr_get_emin()))) << std::endl;
48 
49  // mpfr mantissa is stored in limbs (usually 64-bit words) - the number of those depends on the precision.
50  int limbs = std::ceil(double(a->_mpfr_prec)/double(mp_bits_per_limb));
51  /*
52  std::cout << "Mantissa is ";
53  while( limbs > 0 ){
54  std::cout << carl::binary(h->_mpfr_d[limbs-1]) << " " << std::endl;
55  --limbs;
56  }
57  std::cout << std::endl;
58  limbs = std::ceil(double(h->_mpfr_prec)/double(mp_bits_per_limb));
59  */
60  mpz_t mant;
61  mpz_t tmp;
62  mpz_init(mant);
63  mpz_init(tmp);
64  mpz_set_ui(mant,0);
65  mpz_set_ui(tmp,0);
66  // as mpfr uses whole limbs (64-bit) we can cut away the additional zeroes (offset), if there are any
67  unsigned offset = mp_bits_per_limb - (a->_mpfr_prec % mp_bits_per_limb);
68  //std::cout << "Offset is " << offset << " bits" << std::endl;
69  //std::cout << "Mantissa is ";
70  char outStr[1024];
71 
72  // assemble the integer representation of the mantissa. The limbs are stored in reversed order, least significant first.
73  while( limbs > 0 ){
74  mpz_set_ui(tmp, a->_mpfr_d[limbs-1]);
75  //std::cout << "Shift: " << (mp_bits_per_limb*(limbs-1)) << " bits" << std::endl;
76  mpz_mul_2exp(tmp, tmp, (mp_bits_per_limb*(limbs-1)));
77  mpz_add(mant, mant, tmp);
78  --limbs;
79  }
80  // cut away unnecessary zeroes at the end (divide by 2^offset -> left-shift).
81  mpz_cdiv_q_2exp(mant, mant, offset);
82 
83  mpz_get_str(outStr, 2,mant);
84  std::cout << "Mantissa: " << std::string(outStr) << std::endl;
85 
86  unsigned correction = (a->_mpfr_exp - a->_mpfr_prec);
87 
88  std::cout << "correction: " << correction << std::endl;
89 
90  mpz_mul_2exp(mant, mant, correction);
91 
92  /*
93  mpz_get_str(outStr, 2,mant);
94  std::cout << std::string(outStr) << std::endl;
95  mpz_get_str(outStr, 10,mant);
96  std::cout << std::string(outStr) << std::endl;
97  */
98 
99  // cleanup.
100  mpz_clear(mant);
101  mpz_clear(tmp);
102 }
103 
104 #endif
105 
106 int main (int argc, char** argv)
107 {
108  /*
109  char outStr[1024];
110 
111  mpfr_t a;
112  mpfr_init(a);
113  mpfr_set_ui(a,10, mpfr_rnd_t::MPFR_RNDN);
114 
115  mpfr_sprintf(outStr, "%.20RDe", a);
116  std::cout << std::string(outStr) << std::endl;
117 
118  mpz_t b;
119  mpz_init(b);
120  mpz_set_ui(b,0);
121 
122  to_int(b,a);
123 
124  mpz_get_str(outStr, 10,b);
125  std::cout << std::string(outStr) << std::endl;
126  */
127 #ifdef USE_MPFR_FLOAT
128  compare();
129 #endif
130  return 0;
131 }
int main(int argc, char **argv)
Definition: MpfrTest.cpp:106
Interval< Number > ceil(const Interval< Number > &_in)
Method which returns the next larger integer of the passed number or the number itself,...
Definition: Interval.h:1535
Interval< Number > abs(const Interval< Number > &_in)
Method which returns the absolute value of the passed number.
Definition: Interval.h:1511
signed compare(const BasicConstraint< Pol > &_constraintA, const BasicConstraint< Pol > &_constraintB)
Compares _constraintA with _constraintB.
Definition: Comparison.h:25
Integer to_int(const Interval< Number > &_floatInterval)
Casts the Interval to an arbitrary integer type which has a constructor for a native int.
Definition: Interval.h:1500