Define `pi' in elisp, even with bigfr. (Closes bug #176)
[sxemacs] / src / ent / ent-mpfr.h
1 /*
2   ent-mpfr.c -- Numeric types for SXEmacs
3   Copyright (C) 2005, 2006 Sebastian Freundt
4
5   Author:  Sebastian Freundt
6
7 This file is part of SXEmacs
8
9 SXEmacs is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 SXEmacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program.  If not, see <http://www.gnu.org/licenses/>. */
21
22
23 #ifndef INCLUDED_number_mpfr_h_
24 #define INCLUDED_number_mpfr_h_
25
26 #ifdef UNO
27 /* Uno complains about several inline functions that include conditions with
28    assignments and side effects if we don't do this */
29 #undef __GNUC__
30 #endif
31
32 #include <mpfr.h>
33
34 #include <ent/ent-indef.h>
35
36 typedef mpfr_t bigfr;
37 typedef mp_prec_t bigfr_prec;
38 typedef mp_rnd_t bigfr_rnd;
39
40 extern Lisp_Object Qbigfrp;
41
42 extern Lisp_Object Veuler;
43 extern Lisp_Object Veuler_mascheroni;
44 extern Lisp_Object Vbigfr_pi;
45
46 \f
47 typedef struct Lisp_Bigfr Lisp_Bigfr;
48 struct Lisp_Bigfr
49 {
50         struct lrecord_header lheader;
51         bigfr data;
52 };
53
54 DECLARE_LRECORD(bigfr, Lisp_Bigfr);
55 #define XBIGFR(x) XRECORD(x, bigfr, Lisp_Bigfr)
56 #define wrap_bigfr(p) wrap_object(p)
57 #define BIGFRP(x) RECORDP(x, bigfr)
58 #define CHECK_BIGFR(x) CHECK_RECORD(x, bigfr)
59 #define CONCHECK_BIGFR(x) CONCHECK_RECORD(x, bigfr)
60
61 #define bigfr_data(f) ((f)->data)
62 #define XBIGFR_DATA(x) bigfr_data(XBIGFR(x))
63 #define XBIGFR_GET_PREC(x) bigfr_get_prec(XBIGFR_DATA(x))
64 #define XBIGFR_SET_PREC(x,p) bigfr_set_prec(XBIGFR_DATA(x), p)
65
66 #define BIGFR_ARITH_RETURN(f,op) do                                     \
67 {                                                                       \
68         Lisp_Object retval = make_bigfr_bfr(XBIGFR_DATA(f));            \
69         bigfr_##op(XBIGFR_DATA(retval), XBIGFR_DATA(f));                \
70         return retval;                                                  \
71 } while (0)
72
73 #define BIGFR_ARITH_RETURN1(f,op,arg) do                                \
74 {                                                                       \
75         Lisp_Object retval = make_bigfr_bfr(XBIGFR_DATA(f));            \
76         bigfr_##op(XBIGFR_DATA(retval), XBIGFR_DATA(f), arg);           \
77         return retval;                                                  \
78 } while (0)
79
80 #define BIGFR_INIT_PREC(f, prec) do                                     \
81 {                                                                       \
82         bigfr_init_prec(f, internal_get_precision(prec));               \
83 } while (0)
84
85 extern bigfr ent_scratch_bigfr;
86
87 \f
88 /******************************** Bigfrs ********************************/
89
90 /***** Bigfr: basic functions *****/
91 #define bigfr_init(f)                mpfr_init(f)
92 #define bigfr_init_prec(f,prec)      mpfr_init2(f, prec)
93 #define bigfr_fini(f)                mpfr_clear(f)
94 #define bigfr_hashcode(f)            mpfr_get_ui(f, GMP_RNDN)
95 #define bigfr_sign(f)                mpfr_sgn(f)
96 #define bigfr_get_prec(f)            mpfr_get_prec(f)
97 #define bigfr_set_prec(f, prec)      mpfr_set_prec(f, prec)
98 #define bigfr_set_default_prec(prec) mpfr_set_default_prec(prec)
99 #define bigfr_get_default_prec()     mpfr_get_default_prec()
100
101 /***** Bigfr: conversions *****/
102 extern Bufbyte *bigfr_to_string (bigfr, int);
103 #define bigfr_to_int(f)              ((int)mpfr_get_si(f), GMP_RNDN)
104 #define bigfr_to_uint(f)             ((unsigned int)mpfr_get_ui(f), GMP_RNDN)
105 #define bigfr_to_long(f)             mpfr_get_si(f, GMP_RNDN)
106 #define bigfr_to_ulong(f)            mpfr_get_ui(f, GMP_RNDN)
107 #if fpfloat_double_p
108 #define bigfr_to_fpfloat(f)             mpfr_get_d(f, GMP_RNDN)
109 #elif fpfloat_long_double_p
110 #define bigfr_to_fpfloat(f)             mpfr_get_ld(f, GMP_RNDN)
111 #endif
112
113 #if defined HAVE_MPZ && (defined WITH_GMP || defined WITH_MP)
114 #define bigz_set_bigfr(b,f)          mpfr_get_z(b, f, GMP_RNDN)
115 #endif
116 #if defined HAVE_MPQ && defined WITH_GMP
117 #define bigq_set_bigfr(q, f)         mpfr_get_q(q, f, GMP_RNDN);
118 #endif
119 #if defined HAVE_MPF && defined WITH_GMP
120 #define bigf_set_bigfr(f, fr)        mpfr_get_f(f, fr, GMP_RNDN);
121 #endif
122
123 /***** Bigfr: conversion predicates *****/
124 #define bigfr_fits_int(f)            mpfr_fits_sint_p(f, GMP_RNDN)
125 #define bigfr_fits_uint(f)           mpfr_fits_uint_p(f, GMP_RNDN)
126 #define bigfr_fits_long(f)           mpfr_fits_slong_p(f, GMP_RNDN)
127 #define bigfr_fits_ulong(f)          mpfr_fits_ulong_p(f, GMP_RNDN)
128
129 /***** Bigfr: converting assignments *****/
130 #define bigfr_set(f1,f2)             mpfr_set(f1, f2, GMP_RNDN)
131 #define bigfr_set_string(f,str,base) mpfr_set_str(f, str, base, GMP_RNDN)
132 #define bigfr_set_long(f,l)          mpfr_set_si(f, l, GMP_RNDN)
133 #define bigfr_set_ulong(f,l)         mpfr_set_ui(f, l, GMP_RNDN)
134 #if fpfloat_double_p
135 #define bigfr_set_fpfloat(f, d)         mpfr_set_d(f, d, GMP_RNDN)
136 #elif fpfloat_long_double_p
137 #define bigfr_set_fpfloat(f, d)         mpfr_set_ld(f, d, GMP_RNDN)
138 #endif
139 #define bigfr_set_bigz(f,b)          mpfr_set_z(f, b, GMP_RNDN)
140 #define bigfr_set_bigq(f,r)          mpfr_set_q(f, r, GMP_RNDN)
141 #define bigfr_set_bigf(f,r)          mpfr_set_f(f, r, GMP_RNDN)
142
143 #define bigfr_set_pinf(f)            mpfr_set_inf(f, 1)
144 #define bigfr_set_ninf(f)            mpfr_set_inf(f, -1)
145 #define bigfr_set_nan(f)             mpfr_set_nan(f)
146
147 #define bigfr_inf_p(f)               mpfr_inf_p(f)
148 #define bigfr_nan_p(f)               mpfr_nan_p(f)
149
150 /***** Bigfr: comparisons *****/
151 #define bigfr_cmp(f1,f2)             mpfr_cmp(f1, f2)
152 #define bigfr_lt(f1,f2)              mpfr_less_p(f1, f2)
153 #define bigfr_le(f1,f2)              mpfr_lessequal_p(f1, f2)
154 #define bigfr_eq(f1,f2)                 mpfr_equal_p(f1, f2)
155 #define bigfr_ge(f1,f2)              mpfr_greaterequal_p(f1, f2)
156 #define bigfr_gt(f1,f2)              mpfr_greater_p(f1, f2)
157 #define bigfr_ne(f1, f2)             mpfr_lessgreater_p(f1, f2)
158 #define bigfr_nc(f1, f2)             mpfr_unordered_p(f1, f2)
159
160 /***** Bigfr: arithmetic *****/
161 #define bigfr_neg(f,f2)              mpfr_neg(f, f2, GMP_RNDN)
162 #define bigfr_abs(f,f2)              mpfr_abs(f, f2, GMP_RNDN)
163 #define bigfr_add(res,f1,f2)         mpfr_add(res, f1, f2, GMP_RNDN)
164 #define bigfr_sub(res,f1,f2)         mpfr_sub(res, f1, f2, GMP_RNDN)
165 #define bigfr_mul(res,f1,f2)         mpfr_mul(res, f1, f2, GMP_RNDN)
166 #define bigfr_div(res,f1,f2)         mpfr_div(res, f1, f2, GMP_RNDN)
167
168 #define bigfr_rint(res,f)            mpfr_rint(res, f, GMP_RNDN)
169 #define bigfr_ceil(res,f)            mpfr_ceil(res, f)
170 #define bigfr_floor(res,f)           mpfr_floor(res, f)
171 #define bigfr_trunc(res,f)           mpfr_trunc(res, f)
172 #define bigfr_sqrt(res,f)            mpfr_sqrt(res, f, GMP_RNDN)
173 #define bigfr_sqrt_ui(res,f)         mpfr_sqrt_ui(res, f, GMP_RNDN)
174 #define bigfr_cbrt(res,f)            mpfr_cbrt(res, f, GMP_RNDN)
175 #define bigfr_root(res,f,rad)        mpfr_root(res, f, rad, GMP_RNDN)
176 #define bigfr_pow(res,f,exp)         mpfr_pow_ui(res, f, exp, GMP_RNDN)
177
178 /* Advanced functions */
179 #define bigfr_exp(res, f)            mpfr_exp(res, f, GMP_RNDN);
180 #define bigfr_exp2(res, f)           mpfr_exp2(res, f, GMP_RNDN);
181 #define bigfr_exp10(res, f)          mpfr_exp10(res, f, GMP_RNDN);
182
183 #define bigfr_log(res, f)            mpfr_log(res, f, GMP_RNDN);
184 #define bigfr_log2(res, f)           mpfr_log2(res, f, GMP_RNDN);
185 #define bigfr_log10(res, f)          mpfr_log10(res, f, GMP_RNDN);
186
187 #define bigfr_erf(res, f)            mpfr_erf(res, f, GMP_RNDN);
188 #define bigfr_erfc(res, f)           mpfr_erfc(res, f, GMP_RNDN);
189 #define bigfr_lgamma(res, f)         mpfr_lngamma(res, f, GMP_RNDN);
190
191 #define bigfr_cos(res, f)            mpfr_cos(res, f, GMP_RNDN);
192 #define bigfr_sin(res, f)            mpfr_sin(res, f, GMP_RNDN);
193 #define bigfr_tan(res, f)            mpfr_tan(res, f, GMP_RNDN);
194 #define bigfr_sec(res, f)            mpfr_sec(res, f, GMP_RNDN);
195 #define bigfr_csc(res, f)            mpfr_csc(res, f, GMP_RNDN);
196 #define bigfr_cot(res, f)            mpfr_cot(res, f, GMP_RNDN);
197
198 #define bigfr_acos(res, f)           mpfr_acos(res, f, GMP_RNDN);
199 #define bigfr_asin(res, f)           mpfr_asin(res, f, GMP_RNDN);
200 #define bigfr_atan(res, f)           mpfr_atan(res, f, GMP_RNDN);
201 #define bigfr_atan2(res, f, g)       mpfr_atan2(res, f, g, GMP_RNDN);
202
203 #define bigfr_cosh(res, f)           mpfr_cosh(res, f, GMP_RNDN);
204 #define bigfr_sinh(res, f)           mpfr_sinh(res, f, GMP_RNDN);
205 #define bigfr_tanh(res, f)           mpfr_tanh(res, f, GMP_RNDN);
206 #define bigfr_sech(res, f)           mpfr_sech(res, f, GMP_RNDN);
207 #define bigfr_csch(res, f)           mpfr_csch(res, f, GMP_RNDN);
208 #define bigfr_coth(res, f)           mpfr_coth(res, f, GMP_RNDN);
209
210 #define bigfr_acosh(res, f)          mpfr_acosh(res, f, GMP_RNDN);
211 #define bigfr_asinh(res, f)          mpfr_asinh(res, f, GMP_RNDN);
212 #define bigfr_atanh(res, f)          mpfr_atanh(res, f, GMP_RNDN);
213
214 extern Lisp_Object read_bigfr_string(char*);
215 extern Lisp_Object make_bigfr(fpfloat, unsigned long);
216 #if defined HAVE_MPF && defined WITH_GMP
217 extern Lisp_Object make_bigfr_bf(bigf);
218 #endif
219 extern Lisp_Object make_bigfr_bfr(bigfr);
220 extern Lisp_Object make_indef_bfr(bigfr);
221 extern_inline Lisp_Object ent_mpfr_wipe_indef(bigfr);
222
223 extern_inline Lisp_Object
224 ent_mpfr_wipe_indef(bigfr n)
225 {
226         if (!bigfr_nan_p(n) && !bigfr_inf_p(n))
227                 return make_bigfr_bfr(n);
228         else if (bigfr_nan_p(n))
229                 return make_indef(NOT_A_NUMBER);
230         else if (bigfr_inf_p(n)) {
231                 if (bigfr_sign(n) > 0)
232                         return make_indef(POS_INFINITY);
233                 else
234                         return make_indef(NEG_INFINITY);
235         }
236         return ent_nullop_zero(BIGFR_T);
237 }
238
239 extern void init_optables_BIGFR_T(void);
240 extern void init_ent_mpfr(void);
241 extern void vars_of_ent_mpfr(void);
242 extern void syms_of_ent_mpfr(void);
243
244 #endif /* INCLUDED_number_mpfr_h_ */