]> Shamusworld >> Repos - architektonas/blob - fparser/mpfr/MpfrFloat.hh
Adding fparser v4.3...
[architektonas] / fparser / mpfr / MpfrFloat.hh
1 #ifndef ONCE_FP_MPFR_FLOAT_
2 #define ONCE_FP_MPFR_FLOAT_
3
4 #include <iostream>
5
6 class MpfrFloat
7 {
8  public:
9     /* A default of 256 bits will be used unless changed with this function.
10        Note that all existing and cached GMP objects will be resized to the
11        specified precision (which can be a somewhat heavy operation).
12     */
13     static void setDefaultMantissaBits(unsigned long bits);
14
15     static unsigned long getCurrentDefaultMantissaBits();
16
17     /* The default constructor initializes the object to the value 0.
18        It's efficient to instantiate such zero-initialized objects because
19        all of them will share the same mpfr data. (Also any object initialized
20        with or assigned the explicit value of zero will also share that one
21        mpfr data.) Thus multiple zero-initialized MpfrFloat instances won't
22        consume significant amounts of memory (until they are modified to
23        contain some other value, of course).
24
25        Important caveat:
26        ----------------
27        Note that initializing an MpfrFloat object with, for example, 0.1 will
28        suffer from accuracy problems (at least if the MpfrFloat object has
29        more mantissa bits than a double). The C++ double value 0.1 has only
30        53 mantissa bits, while the MpfrFloat object usually has more. If the
31        MpfrFloat object is initialized with a double, only that many bits of
32        accuracy will end up in the value of the MpfrFloat object. This can
33        create significant rounding/accuracy problems in some cases.
34        If you need to initialize the MpfrObject with some value (which cannot
35        be represented accurately by base-2 floating point numbers, eg. 0.1)
36        at full mantissa precision, you have to use parseValue("0.1") instead,
37        rather than relying on the constructor taking a double type value.
38     */
39     MpfrFloat();
40     MpfrFloat(double value);
41     MpfrFloat(long double value);
42     MpfrFloat(long value);
43     MpfrFloat(int value);
44     MpfrFloat(const char* value, char** endptr);
45
46     ~MpfrFloat();
47
48     MpfrFloat(const MpfrFloat&);
49
50     MpfrFloat& operator=(const MpfrFloat&);
51     MpfrFloat& operator=(double value);
52     MpfrFloat& operator=(long double value);
53     MpfrFloat& operator=(long value);
54     MpfrFloat& operator=(int value);
55     //MpfrFloat& operator=(const char* value);
56
57     void parseValue(const char* value);
58     void parseValue(const char* value, char** endptr);
59
60
61     /* This function can be used to retrieve the raw mpfr_t data structure
62        used by this object. (The template trick is used to avoid a dependency
63        of this header file with <mpfr.h>.)
64        In other words, it can be called like:
65
66          mpfr_t raw_mpfr_data;
67          floatValue.get_raw_mpfr_data(raw_mpfr_data);
68
69        Note that the returned mpfr_t should be considered as read-only and
70        not be modified from the outside because it may be shared among
71        several objects. If the calling code needs to modify the data, it
72        should copy it for itself first with the appropriate MPFR library
73        functions.
74      */
75     template<typename Mpfr_t>
76     void get_raw_mpfr_data(Mpfr_t& dest_mpfr_t);
77
78
79     /* Note that the returned char* points to an internal (shared) buffer
80        which will be valid until the next time this function is called
81        (by any object).
82     */
83     const char* getAsString(unsigned precision) const;
84
85     bool isInteger() const;
86     long toInt() const;
87     double toDouble() const;
88
89     MpfrFloat& operator+=(const MpfrFloat&);
90     MpfrFloat& operator+=(double);
91     MpfrFloat& operator-=(const MpfrFloat&);
92     MpfrFloat& operator-=(double);
93     MpfrFloat& operator*=(const MpfrFloat&);
94     MpfrFloat& operator*=(double);
95     MpfrFloat& operator/=(const MpfrFloat&);
96     MpfrFloat& operator/=(double);
97     MpfrFloat& operator%=(const MpfrFloat&);
98
99     void negate();
100     void abs();
101
102     MpfrFloat operator+(const MpfrFloat&) const;
103     MpfrFloat operator+(double) const;
104     MpfrFloat operator-(const MpfrFloat&) const;
105     MpfrFloat operator-(double) const;
106     MpfrFloat operator*(const MpfrFloat&) const;
107     MpfrFloat operator*(double) const;
108     MpfrFloat operator/(const MpfrFloat&) const;
109     MpfrFloat operator/(double) const;
110     MpfrFloat operator%(const MpfrFloat&) const;
111
112     MpfrFloat operator-() const;
113
114     bool operator<(const MpfrFloat&) const;
115     bool operator<(double) const;
116     bool operator<=(const MpfrFloat&) const;
117     bool operator<=(double) const;
118     bool operator>(const MpfrFloat&) const;
119     bool operator>(double) const;
120     bool operator>=(const MpfrFloat&) const;
121     bool operator>=(double) const;
122     bool operator==(const MpfrFloat&) const;
123     bool operator==(double) const;
124     bool operator!=(const MpfrFloat&) const;
125     bool operator!=(double) const;
126
127     static MpfrFloat log(const MpfrFloat&);
128     static MpfrFloat log2(const MpfrFloat&);
129     static MpfrFloat log10(const MpfrFloat&);
130     static MpfrFloat exp(const MpfrFloat&);
131     static MpfrFloat exp2(const MpfrFloat&);
132     static MpfrFloat exp10(const MpfrFloat&);
133     static MpfrFloat cos(const MpfrFloat&);
134     static MpfrFloat sin(const MpfrFloat&);
135     static MpfrFloat tan(const MpfrFloat&);
136     static MpfrFloat sec(const MpfrFloat&);
137     static MpfrFloat csc(const MpfrFloat&);
138     static MpfrFloat cot(const MpfrFloat&);
139     static void sincos(const MpfrFloat&, MpfrFloat& sin, MpfrFloat& cos);
140     static MpfrFloat acos(const MpfrFloat&);
141     static MpfrFloat asin(const MpfrFloat&);
142     static MpfrFloat atan(const MpfrFloat&);
143     static MpfrFloat atan2(const MpfrFloat&, const MpfrFloat&);
144     static MpfrFloat hypot(const MpfrFloat&, const MpfrFloat&);
145     static MpfrFloat cosh(const MpfrFloat&);
146     static MpfrFloat sinh(const MpfrFloat&);
147     static MpfrFloat tanh(const MpfrFloat&);
148     static MpfrFloat acosh(const MpfrFloat&);
149     static MpfrFloat asinh(const MpfrFloat&);
150     static MpfrFloat atanh(const MpfrFloat&);
151     static MpfrFloat sqrt(const MpfrFloat&);
152     static MpfrFloat cbrt(const MpfrFloat&);
153     static MpfrFloat root(const MpfrFloat&, unsigned long root);
154     static MpfrFloat pow(const MpfrFloat&, const MpfrFloat&);
155     static MpfrFloat pow(const MpfrFloat&, long exponent);
156     static MpfrFloat abs(const MpfrFloat&);
157     static MpfrFloat dim(const MpfrFloat&, const MpfrFloat&);
158     static MpfrFloat round(const MpfrFloat&);
159     static MpfrFloat ceil(const MpfrFloat&);
160     static MpfrFloat floor(const MpfrFloat&);
161     static MpfrFloat trunc(const MpfrFloat&);
162
163     static MpfrFloat parseString(const char* str, char** endptr);
164
165     // These values are cached (and recalculated every time the mantissa bits
166     // change), so it's efficient to call these repeatedly:
167     static MpfrFloat const_pi();
168     static MpfrFloat const_e();
169     static MpfrFloat const_log2();
170     static MpfrFloat someEpsilon();
171
172
173  private:
174     struct MpfrFloatData;
175     class MpfrFloatDataContainer;
176
177     MpfrFloatData* mData;
178
179     enum DummyType { kNoInitialization };
180     MpfrFloat(DummyType);
181     MpfrFloat(MpfrFloatData*);
182
183     void copyIfShared();
184     static MpfrFloatDataContainer& mpfrFloatDataContainer();
185
186     friend MpfrFloat operator+(double lhs, const MpfrFloat& rhs);
187     friend MpfrFloat operator-(double lhs, const MpfrFloat& rhs);
188 };
189
190 MpfrFloat operator+(double lhs, const MpfrFloat& rhs);
191 MpfrFloat operator-(double lhs, const MpfrFloat& rhs);
192 MpfrFloat operator*(double lhs, const MpfrFloat& rhs);
193 MpfrFloat operator/(double lhs, const MpfrFloat& rhs);
194 MpfrFloat operator%(double lhs, const MpfrFloat& rhs);
195
196 inline bool operator<(double lhs, const MpfrFloat& rhs) { return rhs > lhs; }
197 inline bool operator<=(double lhs, const MpfrFloat& rhs) { return rhs >= lhs; }
198 inline bool operator>(double lhs, const MpfrFloat& rhs) { return rhs < lhs; }
199 inline bool operator>=(double lhs, const MpfrFloat& rhs) { return rhs <= lhs; }
200 inline bool operator==(double lhs, const MpfrFloat& rhs) { return rhs == lhs; }
201 inline bool operator!=(double lhs, const MpfrFloat& rhs) { return rhs != lhs; }
202
203 // This function takes into account the value of os.precision()
204 std::ostream& operator<<(std::ostream& os, const MpfrFloat& value);
205
206 #endif