Debian Codegraph @

ext/scheme/flonum.scm

raw
1 ;;;
2 ;;; scheme.flonum - Flonums (R7Rs Tangerine)
3 ;;;
4 ;;; Copyright (c) 2019-2024 Shiro Kawai <[email protected]>
5 ;;;
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
8 ;;; are met:
9 ;;;
10 ;;; 1. Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
12 ;;;
13 ;;; 2. Redistributions in binary form must reproduce the above copyright
14 ;;; notice, this list of conditions and the following disclaimer in the
15 ;;; documentation and/or other materials provided with the distribution.
16 ;;;
17 ;;; 3. Neither the name of the authors nor the names of its contributors
18 ;;; may be used to endorse or promote products derived from this
19 ;;; software without specific prior written permission.
20 ;;;
21 ;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 ;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 ;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 ;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 ;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 ;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 ;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 ;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 ;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 ;;;
33
34 ;; Originally SRFI-144
35 (define-module scheme.flonum
36 (use math.const :prefix const:)
37 (export fl-e fl-1/e fl-e-2 fl-e-pi/4 fl-log2-e fl-log10-e
38 fl-log-2 fl-1/log-2 fl-log-3 fl-log-pi fl-log-10 fl-1/log-10
39 fl-pi fl-1/pi fl-2pi fl-pi/2 fl-pi/4 fl-pi-squared
40 fl-degree fl-2/pi fl-2/sqrt-pi
41 fl-sqrt-2 fl-sqrt-3 fl-sqrt-5 fl-sqrt-10 fl-1/sqrt-2
42 fl-cbrt-2 fl-cbrt-3 fl-4thrt-2 fl-phi fl-log-phi
43 fl-1/log-phi fl-euler fl-e-euler fl-sin-1 fl-cos-1
44 fl-gamma-1/2 fl-gamma-1/3 fl-gamma-2/3
45
46 fl-greatest fl-least fl-epsilon fl-fast-fl+*
47 fl-integer-exponent-zero fl-integer-exponent-nan
48
49 flonum fladjacent flcopysign make-flonum
50
51 flinteger-fraction flexponent flinteger-exponent
52 flnormalized-fraction-exponent flsign-bit
53
54 flonum? fl=? fl<? fl>? fl<=? fl>=?
55 flunordered? flinteger? flzero? flpositive? flnegative?
56 flodd? fleven? flfinite? flinfinite? flnan?
57 flnormalized? fldenormalized?
58
59 flmax flmin fl+ fl* fl+* fl- fl/ flabs flabsdiff
60 flposdiff flsgn flnumerator fldenominator
61 flfloor flceiling flround fltruncate
62
63 flexp flexp2 flexp-1 flsquare flsqrt flcbrt flhypot flexpt fllog
64 fllog1+ fllog2 fllog10 make-fllog-base
65
66 flsin flcos fltan flasin flacos flatan
67 flsinh flcosh fltanh flasinh flacosh flatanh
68
69 flquotient flremainder flremquo
70
71 flgamma flloggamma flfirst-bessel flsecond-bessel
72 flerf flerfc
73 ))
74 (select-module scheme.flonum)
75
76 (inline-stub
77 (.include <math.h>))
78
79 ;;;
80 ;;; Constants
81 ;;;
82 (define-constant fl-e const:e)
83 (define-constant fl-1/e (/ const:e))
84 (define-constant fl-e-2 7.38905609893065) ; e*e yields 1ulp error
85 (define-constant fl-e-pi/4 (real-exp (/ const:pi 4)))
86 (define-constant fl-log2-e (/ (real-ln 2)))
87 (define-constant fl-log10-e 0.4342944819032518); (/ (real-ln 10)) yields 1ulp error
88 (define-constant fl-log-2 (real-ln 2))
89 (define-constant fl-1/log-2 (/ (real-ln 2)))
90 (define-constant fl-log-3 (real-ln 3))
91 (define-constant fl-log-pi (real-ln const:pi))
92 (define-constant fl-log-10 (real-ln 10))
93 (define-constant fl-1/log-10 0.4342944819032518); (/ (%log 10)) yields 1ulp error
94 (define-constant fl-pi const:pi)
95 (define-constant fl-1/pi (/ const:pi))
96 (define-constant fl-2pi (* const:pi 2))
97 (define-constant fl-pi/2 const:pi/2)
98 (define-constant fl-pi/4 const:pi/4)
99 (define-constant fl-pi-squared (* const:pi const:pi))
100 (define-constant fl-degree const:pi/180)
101 (define-constant fl-2/pi (/ const:pi/2))
102 (define-constant fl-2/sqrt-pi (/ 2 (real-sqrt const:pi)))
103 (define-constant fl-sqrt-2 (real-sqrt 2))
104 (define-constant fl-sqrt-3 (real-sqrt 3))
105 (define-constant fl-sqrt-5 (real-sqrt 5))
106 (define-constant fl-sqrt-10 (real-sqrt 10))
107 (define-constant fl-1/sqrt-2 (/ (real-sqrt 2)))
108 (define-constant fl-cbrt-2 (real-expt 2 1/3))
109 (define-constant fl-cbrt-3 (real-expt 3 1/3))
110 (define-constant fl-4thrt-2 (real-expt 2 1/4))
111 (define-constant fl-phi (/ (+ 1 (real-sqrt 5)) 2))
112 (define-constant fl-log-phi (real-ln fl-phi))
113 (define-constant fl-1/log-phi 2.07808692123502753); (/ fl-log-phi) yields 1ulp error
114 (define-constant fl-euler 0.5772156649015329)
115 (define-constant fl-e-euler (exp fl-euler))
116 (define-constant fl-sin-1 (real-sin 1))
117 (define-constant fl-cos-1 (real-cos 1))
118 (define-constant fl-gamma-1/2 1.77245385090551603); (gamma 1/2) yields 1ulp error on MinGW
119 (define-constant fl-gamma-1/3 2.67893853470774763); (gamma 1/3) yields 1ulp error
120 (define-constant fl-gamma-2/3 (gamma 2/3))
121
122 (define-constant fl-greatest (greatest-positive-flonum))
123 ;; NB: Denormalized flonum support may differ between the target platform
124 ;; and the precompiling platform. Without-precompiling ensures the call
125 ;; to least-positive-flonum is done at the load time.
126 (without-precompiling
127 (define-constant fl-least (least-positive-flonum)))
128 (define-constant fl-epsilon (flonum-epsilon))
129 (define-constant fl-fast-fl+* #t)
130
131 ;; these two needs to be 'define-inline', for ilogb can't be used
132 ;; before loading this module.
133 (define-inline fl-integer-exponent-zero (ilogb 0))
134 (define-inline fl-integer-exponent-nan (ilogb +nan.0))
135
136 ;;;
137 ;;; constructors
138 ;;;
139
140 ;; See post-finalization note in srfi about returning +nan.0
141 (define-inline (flonum n) (if (real? n) (inexact n) +nan.0))
142
143 (define-cproc fladjacent (x::<double> y::<double>)
144 ::<double> :constant :fast-flonum
145 (return (nextafter x y)))
146
147 (define-cproc flcopysign (x::<double> y::<double>)
148 ::<double> :constant :fast-flonum
149 (return (copysign x y)))
150
151 (define-inline (make-flonum x n) (ldexp x n))
152
153 ;;;
154 ;;; accessors
155 ;;;
156
157 (define-cproc logb (x::<double>) ::<double> :constant :fast-flonum logb)
158 (define-cproc ilogb (x::<double>) ::<int> :constant :fast-flonum ilogb)
159
160 (define-inline (flinteger-fraction x)
161 (receive (r q) (modf x) (values q r)))
162 (define-inline (flexponent x) (logb x))
163 (define-inline (flinteger-exponent x) (ilogb x))
164 (define-inline (flnormalized-fraction-exponent x) (frexp x))
165 (define-cproc flsign-bit (x::<double>) ::<int> :constant :fast-flonum
166 (return (?: (signbit x) 1 0)))
167
168 ;;;
169 ;;; predicates
170 ;;;
171
172 ;; flonum? - built-in
173
174 ;; we don't check types of arguments in these; the point of flonum-specific
175 ;; ops is speed, so it's less useful if these ones are slower than the
176 ;; generic version.
177 (define-inline (fl=? . args) (apply = args))
178 (define-inline (fl<? . args) (apply < args))
179 (define-inline (fl<=? . args) (apply <= args))
180 (define-inline (fl>? . args) (apply > args))
181 (define-inline (fl>=? . args) (apply >= args))
182
183 (define-inline (flunordered? x y) (or (nan? x) (nan? y)))
184 (define-inline (flinteger? x) (and (flonum? x) (integer? x)))
185 (define-inline (flzero? x) (and (flonum? x) (zero? x)))
186 (define-inline (flpositive? x) (and (flonum? x) (positive? x)))
187 (define-inline (flnegative? x) (and (flonum? x) (negative? x)))
188 (define-inline (flodd? x) (and (flonum? x) (odd? x)))
189 (define-inline (fleven? x) (and (flonum? x) (even? x)))
190 (define-inline (flfinite? x) (finite? x))
191 (define-inline (flinfinite? x) (infinite? x))
192 (define-inline (flnan? x) (nan? x))
193
194 (inline-stub
195 ;; On MinGW 32bit, fpclassify is broken.
196 ;; cf. https://github.com/shirok/Gauche/pull/465
197 (define-cfn flonum_classify (d::double) ::int :static
198 (.if (and (defined __MINGW32__) (not (defined __MIGW64__)))
199 (return (__builtin_fpclassify FP_INFINITE FP_NAN FP_NORMAL FP_SUBNORMAL
200 FP_ZERO d))
201 (return (fpclassify d)))))
202
203 (define-cproc flnormalized? (x::<double>) ::<boolean> :constant :fast-flonum
204 (return (== (flonum_classify x) FP_NORMAL)))
205 (define-cproc fldenormalized? (x::<double>) ::<boolean> :constant :fast-flonum
206 (return (== (flonum_classify x) FP_SUBNORMAL)))
207
208 ;;;
209 ;;; Arithmetic
210 ;;;
211
212 ;; Again, we don't check the argument types for the sake of the speed.
213
214 (define-inline (flmax . args) (if (null? args) -inf.0 (apply max args)))
215 (define-inline (flmin . args) (if (null? args) +inf.0 (apply min args)))
216
217 (define-inline (fl+ . args) (apply +. args))
218 (define-inline (fl* . args) (apply *. args))
219 (define-cproc fl+* (x::<double> y::<double> z::<double>)
220 ::<double> :fast-flonum :constant
221 (return (fma x y z)))
222
223 (define-inline (fl- x . args) (apply -. x args))
224 (define-inline (fl/ x . args) (apply /. x args))
225
226 (define-cproc flabs (x::<double>) ::<double> :fast-flonum :constant fabs)
227 (define-cproc flabsdiff (x::<double> y::<double>)
228 ::<double> :fast-flonum :constant
229 (return (fabs (- x y))))
230 (define-cproc flposdiff (x::<double> y::<double>)
231 ::<double> :fast-flonum :constant
232 (return (?: (> x y) (- x y) 0.0)))
233 (define-cproc flsgn (x::<double>) ::<double> :fast-flonum :constant
234 (return (?: (signbit x) -1.0 1.0)))
235
236 (define (flnumerator x)
237 (if (or (infinite? x) (nan? x) (zero? x))
238 x ; -0.0 is handled here
239 (inexact (numerator (exact x)))))
240
241 (define (fldenominator x)
242 (cond [(or (infinite? x) (zero? x)) 1.0]
243 [(nan? x) x]
244 [else (inexact (denominator (exact x)))]))
245
246 (define-cproc flfloor (x::<double>) ::<double> :fast-flonum :constant floor)
247 (define-cproc flceiling (x::<double>) ::<double> :fast-flonum :constant ceil)
248 (define (flround x) (assume (flonum? x)) (round x))
249 (define-cproc fltruncate (x::<double>) ::<double> :fast-flonum :constant trunc)
250
251 (define-inline (flexp x) (real-exp x))
252 (define-inline (flexp2 x) (real-expt 2.0 x))
253 (define-cproc flexp-1 (x::<double>) ::<double> :fast-flonum :constant expm1)
254 (define-inline (flsquare x) (*. x x))
255 (define-inline (flsqrt x) (real-sqrt x))
256 (define (flcbrt x)
257 (assume (real? x))
258 ;; (%expt x 1/3) may give us a complex root, so we roll our own.
259 (if (or (nan? x) (infinite? x))
260 x
261 (let loop ([r (flcopysign (magnitude (real-expt x 1/3)) x)])
262 (if (zero? r)
263 r
264 (let1 r+ (- r (/ (- (* r r r) x)
265 (* 3 r r)))
266 (if (= r r+)
267 r
268 (loop r+)))))))
269 (define-cproc flhypot (x::<double> y::<double>)
270 ::<double> :fast-flonum :constant hypot)
271 (define-inline (flexpt x y) (real-expt x y))
272 (define-inline (fllog x) (real-ln x))
273 (define-cproc fllog1+ (x::<double>) ::<double> :fast-flonum :constant log1p)
274 (define-cproc fllog2 (x::<double>) ::<double> :fast-flonum :constant log2)
275 (define-cproc fllog10 (x::<double>) ::<double> :fast-flonum :constant log10)
276 (define (make-fllog-base base) (^x (/. (real-ln x) (real-ln base))))
277
278 (define-inline (flsin x) (real-sin x))
279 (define-inline (flcos x) (real-cos x))
280 (define-inline (fltan x) (real-tan x))
281 (define-inline (flasin x) (real-asin x))
282 (define-inline (flacos x) (real-acos x))
283 (define-inline (flatan y . x) (apply real-atan y x))
284 (define-inline (flsinh x) (real-sinh x))
285 (define-inline (flcosh x) (real-cosh x))
286 (define-inline (fltanh x) (real-tanh x))
287 (define-cproc flasinh (x::<double>) ::<double> :fast-flonum :constant asinh)
288 (define-cproc flacosh (x::<double>) ::<double> :fast-flonum :constant acosh)
289 (define-cproc flatanh (x::<double>) ::<double> :fast-flonum :constant atanh)
290
291 (define (flquotient x y) (fltruncate (/. x y)))
292 (define (flremainder x y) (-. x (*. y (flquotient x y))))
293 (define-cproc flremquo (x::<double> y::<double>) ::(<double> <int>)
294 (let* ([quo::int]
295 [rem::double (remquo x y (& quo))])
296 (result rem quo)))
297
298 (define-inline (flgamma x) (gamma x))
299 (define (flloggamma x)
300 (values (lgamma x)
301 (cond [(<= 0 x) 1.0]
302 [(infinite? x) +nan.0] ; sign of gamma(-inf.0) undecidable
303 [(nan? x) +nan.0]
304 [(odd? (floor x)) -1.0]
305 [else 1.0])))
306
307 (define-cproc flfirst-bessel (n::<int> x::<double>)
308 ::<double> :fast-flonum :constant
309 jn)
310 (define-cproc flsecond-bessel (n::<int> x::<double>)
311 ::<double> :fast-flonum :constant
312 ;; As of 2019, NB: MinGW's yn returns NaN if x = 0, as opposed to the
313 ;; POSIX definition that says -HUGE_VAL.
314 (.if (defined GAUCHE_WINDOWS)
315 (if (== x 0.0)
316 (return SCM_DBL_NEGATIVE_INFINITY)
317 (return (yn n x)))
318 (return (yn n x))))
319
320 (define-cproc flerf (x::<double>)
321 ::<double> :fast-flonum :constant
322 erf)
323 (define-cproc flerfc (x::<double>)
324 ::<double> :fast-flonum :constant
325 erfc)