Debian Codegraph @

src/libnum.scm

raw
1 ;;;
2 ;;; libnum.stub - builtin number libraries
3 ;;;
4 ;;; Copyright (c) 2000-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 (select-module gauche.internal)
35
36 (inline-stub
37 (.include "gauche/priv/configP.h"
38 "gauche/vminsn.h"
39 "gauche/priv/bignumP.h"
40 <stdlib.h>
41 <float.h>
42 <math.h>)
43 (.when (not (defined M_PI))
44 (.define M_PI 3.1415926535897932384)))
45
46 ;;
47 ;; Predicates
48 ;;
49
50 (select-module scheme)
51 (define-cproc number? (obj) ::<boolean> :fast-flonum :constant
52 (inliner NUMBERP) SCM_NUMBERP)
53 (define-cproc complex? (obj) ::<boolean> :fast-flonum :constant
54 (inliner NUMBERP) SCM_NUMBERP)
55 (define-cproc real? (obj) ::<boolean> :fast-flonum :constant
56 (inliner REALP) SCM_REALP)
57 (define-cproc rational? (obj)::<boolean> :fast-flonum :constant
58 (return (and (SCM_REALP obj) (Scm_FiniteP obj))))
59 (define-cproc integer? (obj) ::<boolean> :fast-flonum :constant
60 (return (and (SCM_NUMBERP obj) (Scm_IntegerP obj))))
61
62 (define-cproc exact? (obj) ::<boolean> :fast-flonum :constant SCM_EXACTP)
63 (define-cproc inexact? (obj) ::<boolean> :fast-flonum :constant SCM_INEXACTP)
64
65 (define-cproc zero? (obj::<number>) ::<boolean> :fast-flonum :constant
66 (return (and (SCM_REALP obj) (== (Scm_Sign obj) 0))))
67
68 (define-cproc positive? (obj) ::<boolean> :fast-flonum :constant
69 (return (> (Scm_Sign obj) 0)))
70 (define-cproc negative? (obj) ::<boolean> :fast-flonum :constant
71 (return (and (not (Scm_NanP obj)) (< (Scm_Sign obj) 0))))
72 (define-cproc odd? (obj) ::<boolean> :fast-flonum :constant Scm_OddP)
73 (define-cproc even? (obj) ::<boolean> :fast-flonum :constant
74 (return (not (Scm_OddP obj))))
75
76 (select-module gauche)
77 ;; fixnum? and bignum? is not :constant, since it is platform-dependent.
78 (define-cproc fixnum? (x) ::<boolean> :fast-flonum SCM_INTP)
79 (define-cproc bignum? (x) ::<boolean> :fast-flonum SCM_BIGNUMP)
80 (define-cproc flonum? (x) ::<boolean> :fast-flonum :constant SCM_FLONUMP)
81 (define-cproc ratnum? (x) ::<boolean> :fast-flonum :constant SCM_RATNUMP)
82
83 (define-cproc finite? (x::<number>) ::<boolean> :fast-flonum Scm_FiniteP)
84 (define-cproc infinite? (x::<number>) ::<boolean> :fast-flonum Scm_InfiniteP)
85 (define-cproc nan? (x::<number>) ::<boolean> :fast-flonum Scm_NanP)
86 (define-cproc negative-zero? (x::<number>) ::<boolean> :fast-flonum :constant
87 (return (and (SCM_FLONUMP x) ;only flonums have -0.0
88 (== (SCM_FLONUM_VALUE x) 0.0)
89 (!= (signbit (SCM_FLONUM_VALUE x)) 0))))
90
91 (define-cproc exact-integer? (obj) ::<boolean> :fast-flonum :constant
92 SCM_INTEGERP)
93
94 ;;
95 ;; Platform introspection
96 ;; TODO: These are not :constant for now, because of cross-compilation.
97 ;; We may need the means to provide target-specific constants during
98 ;; cross compilation.
99
100 (select-module gauche)
101 ;; Names are from R6RS.
102 (define-cproc fixnum-width () ::<int> (return (+ SCM_SMALL_INT_SIZE 1)))
103 (define-cproc least-fixnum () ::<long> (return SCM_SMALL_INT_MIN))
104 (define-cproc greatest-fixnum () ::<long> (return SCM_SMALL_INT_MAX))
105
106 (inline-stub
107 (initcode
108 ;; default-endian is defined in number.c. This call ensures it is
109 ;; initialized.
110 (Scm_DefaultEndian)))
111
112 (define-cproc native-endian () Scm_NativeEndian)
113
114 ;; DBL_EPSILON, etc.
115 (define-cproc flonum-epsilon ()
116 (let* ([x::(static ScmObj) SCM_UNBOUND])
117 (when (== x SCM_UNBOUND)
118 (set! x (Scm_MakeFlonum DBL_EPSILON)))
119 (return x)))
120 (define-cproc least-positive-normalized-flonum ()
121 (let* ([x::(static ScmObj) SCM_UNBOUND])
122 (when (== x SCM_UNBOUND)
123 (set! x (Scm_MakeFlonum DBL_MIN)))
124 (return x)))
125 (define-cproc least-positive-flonum ()
126 (let* ([x::(static ScmObj) SCM_UNBOUND])
127 (when (== x SCM_UNBOUND)
128 (let* ([m::double (Scm_EncodeFlonum (SCM_MAKE_INT 1) -1074 1)])
129 (if (== m 0.0) ; architecture doesn't support denormalized number
130 (set! x (Scm_MakeFlonum DBL_MIN))
131 (set! x (Scm_MakeFlonum m)))))
132 (return x)))
133 (define-cproc greatest-positive-flonum ()
134 (return (Scm_MakeFlonum DBL_MAX)))
135
136 ;; For backward compatibility
137 (define flonum-min-normalized least-positive-normalized-flonum)
138 (define flonum-min-denormalized least-positive-flonum)
139
140 (select-module gauche.internal)
141 (define-cproc %bignum-dump (obj) ::<void>
142 (when (SCM_BIGNUMP obj)
143 (Scm_BignumDump (SCM_BIGNUM obj) SCM_CUROUT)))
144
145 ;;
146 ;; Comparison
147 ;;
148
149 (select-module scheme)
150 (inline-stub
151 ;; NB: numeric procedures =, <, <=, >, >=, +, -, * and / have inliners
152 ;; defined in compile.scm. When one of these operators appears at the
153 ;; operator position of an expression, it is inlined so that the following
154 ;; SUBRs won't be called. N-ary arithmetic operations (N>2) are expanded
155 ;; to a series of binary arithmetic operations in the compiler.
156 ;; N-ary comparison operations (N>2) are NOT expanded by the compiler,
157 ;; and these SUBRs are called. In order to avoid extra consing for those
158 ;; N-ary comparison expressions, we use :optarray to receive the first few
159 ;; arguments on stack.
160 ;;
161 ;; SUBRs are always called when those numeric procedures are invoked via
162 ;; apply, or as a result of expression in the operator position,
163 ;; such as ((if x + *) 2 3).
164
165 (define-cise-stmt numcmp
166 [(_ compar)
167 `(begin
168 (cond [(not (,compar arg0 arg1)) (return FALSE)]
169 [(== optcnt 0) (return TRUE)]
170 [(not (,compar arg1 (aref oarg 0))) (return FALSE)]
171 [(== optcnt 1) (return TRUE)]
172 [(not (,compar (aref oarg 0) (aref oarg 1))) (return FALSE)]
173 [(and (== optcnt 2) (SCM_NULLP args)) (return TRUE)]
174 [else
175 (set! arg0 (aref oarg 1)
176 arg1 (SCM_CAR args)
177 args (SCM_CDR args))
178 (loop (cond [(not (,compar arg0 arg1)) (return FALSE)]
179 [(SCM_NULLP args) (return TRUE)]
180 [else (set! arg0 arg1
181 arg1 (SCM_CAR args)
182 args (SCM_CDR args))]))]))])
183 )
184
185 (define-cproc = (arg0 arg1 :optarray (oarg optcnt 2) :rest args)
186 ::<boolean> :fast-flonum :constant (numcmp Scm_NumEq))
187 (define-cproc < (arg0 arg1 :optarray (oarg optcnt 2) :rest args)
188 ::<boolean> :fast-flonum :constant (numcmp Scm_NumLT))
189 (define-cproc <= (arg0 arg1 :optarray (oarg optcnt 2) :rest args)
190 ::<boolean> :fast-flonum :constant (numcmp Scm_NumLE))
191 (define-cproc > (arg0 arg1 :optarray (oarg optcnt 2) :rest args)
192 ::<boolean> :fast-flonum :constant (numcmp Scm_NumGT))
193 (define-cproc >= (arg0 arg1 :optarray (oarg optcnt 2) :rest args)
194 ::<boolean> :fast-flonum :constant (numcmp Scm_NumGE))
195
196 (define-cproc max (arg0 :rest args) ::<number> :constant
197 (Scm_MinMax arg0 args NULL (& SCM_RESULT)))
198 (define-cproc min (arg0 :rest args) ::<number> :constant
199 (Scm_MinMax arg0 args (& SCM_RESULT) NULL))
200
201 (select-module gauche)
202 (define-cproc <=:< (x y z) ::<boolean> :fast-flonum :constant
203 (return (and (Scm_NumLE x y) (Scm_NumLT y z))))
204 (define-cproc <:<= (x y z) ::<boolean> :fast-flonum :constant
205 (return (and (Scm_NumLT x y) (Scm_NumLE y z))))
206 (define-cproc min&max (arg0 :rest args) ::(<top> <top>)
207 (Scm_MinMax arg0 args (& SCM_RESULT0) (& SCM_RESULT1)))
208
209 ;;
210 ;; Conversions
211 ;;
212
213 (select-module scheme)
214 (define-cproc exact->inexact (obj) :fast-flonum :constant Scm_Inexact)
215 (define-cproc inexact->exact (obj) :fast-flonum :constant Scm_Exact)
216
217 (select-module gauche)
218 (define-inline exact inexact->exact) ;R6RS
219 (define-inline inexact exact->inexact) ;R6RS
220
221 (select-module scheme)
222 (define-cproc number->string (obj :optional (radix::<fixnum> 10)
223 (flags #f)
224 (precision::<fixnum> -1))
225 :fast-flonum :constant
226 (let* ([f::u_long 0]
227 [fmt::ScmNumberFormat]
228 [o::ScmPort* (SCM_PORT (Scm_MakeOutputStringPort TRUE))])
229 (when (or (< radix SCM_RADIX_MIN) (> radix SCM_RADIX_MAX))
230 (Scm_Error "radix must be an integer between %d and %d, but got %d"
231 SCM_RADIX_MIN SCM_RADIX_MAX radix))
232 (cond [(or (SCM_FALSEP flags) (SCM_NULLP flags)) (set! f 0)]
233 [(SCM_TRUEP flags) (set! f SCM_NUMBER_FORMAT_USE_UPPER)];compatibility
234 [(SCM_PAIRP flags)
235 (unless (SCM_FALSEP (Scm_Memq 'uppercase flags))
236 (logior= f SCM_NUMBER_FORMAT_USE_UPPER))
237 (unless (SCM_FALSEP (Scm_Memq 'plus flags))
238 (logior= f SCM_NUMBER_FORMAT_SHOW_PLUS))
239 (unless (SCM_FALSEP (Scm_Memq 'radix flags))
240 (logior= f SCM_NUMBER_FORMAT_ALT_RADIX))
241 (unless (SCM_FALSEP (Scm_Memq 'notational flags))
242 (logior= f SCM_NUMBER_FORMAT_ROUND_NOTATIONAL))]
243 [else
244 (Scm_Error "flags argument must be a list of symbols (uppercase, \
245 plus, radix, notational) or a boolean, but got: %S"
246 flags)])
247 (Scm_NumberFormatInit (& fmt))
248 (set! (ref fmt radix) radix)
249 (set! (ref fmt flags) f)
250 (set! (ref fmt precision) precision)
251 (Scm_PrintNumber o obj (& fmt))
252 (return (Scm_GetOutputString o 0))))
253
254 (define-cproc string->number (obj::<string>
255 :optional (radix::<fixnum> 10)
256 (default-exactness #f))
257 (let* ([flags::u_long 0])
258 (cond
259 [(SCM_EQ default-exactness 'exact)
260 (set! flags SCM_NUMBER_FORMAT_EXACT)]
261 [(SCM_EQ default-exactness 'inexact)
262 (set! flags SCM_NUMBER_FORMAT_INEXACT)]
263 [(SCM_FALSEP default-exactness)]
264 [else (Scm_Error "default-exactness must be either #f, exact or inexact, but got: %S" default-exactness)])
265 (return (Scm_StringToNumber obj radix flags))))
266
267 (select-module gauche)
268 (define-cproc floor->exact (num) :fast-flonum :constant
269 (return (Scm_RoundToExact num SCM_ROUND_FLOOR)))
270 (define-cproc ceiling->exact (num) :fast-flonum :constant
271 (return (Scm_RoundToExact num SCM_ROUND_CEIL)))
272 (define-cproc truncate->exact (num) :fast-flonum :constant
273 (return (Scm_RoundToExact num SCM_ROUND_TRUNC)))
274 (define-cproc round->exact (num) :fast-flonum :constant
275 (return (Scm_RoundToExact num SCM_ROUND_ROUND)))
276
277 (define-cproc decode-float (num) ;from ChezScheme
278 (cond [(SCM_FLONUMP num)
279 (let* ([exp::int] [sign::int]
280 [f (Scm_DecodeFlonum (SCM_FLONUM_VALUE num) (& exp) (& sign))]
281 [v (Scm_MakeVector 3 '#f)])
282 (set! (SCM_VECTOR_ELEMENT v 0) f
283 (SCM_VECTOR_ELEMENT v 1) (Scm_MakeInteger exp)
284 (SCM_VECTOR_ELEMENT v 2) (Scm_MakeInteger sign))
285 (return v))]
286 [(SCM_INTP num)
287 (let* ([v (Scm_MakeVector 3 '#f)])
288 (set! (SCM_VECTOR_ELEMENT v 0) (Scm_Abs num)
289 (SCM_VECTOR_ELEMENT v 1) (Scm_MakeInteger 0)
290 (SCM_VECTOR_ELEMENT v 2) (Scm_MakeInteger (Scm_Sign num)))
291 (return v))]
292 [else (SCM_TYPE_ERROR num "real number") (return SCM_UNDEFINED)]))
293
294 (define-cproc fmod (x::<double> y::<double>)::<double> :constant fmod)
295
296 (define-cproc frexp (d::<double>) ::(<double> <int>) :constant
297 (set! SCM_RESULT0 (frexp d (& SCM_RESULT1))))
298
299 (define-cproc modf (x::<double>) ::(<double> <double>) :constant
300 (set! SCM_RESULT0 (modf x (& SCM_RESULT1))))
301
302 (define-cproc ldexp (x::<double> exp::<int>) ::<double> :constant ldexp)
303
304 (define-cproc log10 (x::<double>) ::<double> :constant log10)
305
306 ;; NB: Alternative implemenation of gamma and log-abs-gamma functions are
307 ;; provided in Scheme (lib/gauche/numerical.scm).
308 (select-module gauche.internal)
309 (inline-stub
310 (.when (defined HAVE_TGAMMA)
311 (define-cproc %gamma (x::<double>) ::<double> :fast-flonum :constant
312 tgamma))
313 (.when (defined HAVE_LGAMMA)
314 (define-cproc %lgamma (x::<double>) ::<double> :fast-flonum :constant
315 lgamma)))
316 ;; Returns the HalfFloat representation as integer. For now,
317 ;; we keep it in gauche.internal.
318 (define-cproc flonum->f16bits (x::<double>) ::<int> :constant Scm_DoubleToHalf)
319
320 ;;
321 ;; Arithmetics
322 ;;
323
324 ;; NB: Compile-time constant folding for these four procedures are
325 ;; handled in the compiler, so we don't need :constant flag here.
326
327 ;; For - and /, one-argument case is handled by separate C procedures.
328 ;; (Scm_Negate(), Scm_Reciprocal()). For + and *, we return the argument
329 ;; as is if it is a number, or delegate it to unary object-+ / object-*
330 ;; method.
331 (select-module scheme)
332 (define-cproc * (:rest args) ::<number> :fast-flonum
333 (cond [(not (SCM_PAIRP args)) (return (SCM_MAKE_INT 1))]
334 [(and (not (SCM_PAIRP (SCM_CDR args)))
335 (not (SCM_NUMBERP (SCM_CAR args))))
336 (let* ([unary-proc SCM_UNDEFINED])
337 (SCM_BIND_PROC unary-proc "%apply-unary-*"
338 (Scm_GaucheInternalModule))
339 (return (Scm_ApplyRec1 unary-proc (SCM_CAR args))))]
340 [else (let* ([r::ScmObj (SCM_CAR args)])
341 (dolist [v (SCM_CDR args)] (set! r (Scm_Mul r v)))
342 (return r))]))
343
344 (define-cproc + (:rest args) ::<number> :fast-flonum
345 (cond [(not (SCM_PAIRP args)) (return (SCM_MAKE_INT 0))]
346 [(and (not (SCM_PAIRP (SCM_CDR args)))
347 (not (SCM_NUMBERP (SCM_CAR args))))
348 (let* ([unary-proc SCM_UNDEFINED])
349 (SCM_BIND_PROC unary-proc "%apply-unary-+"
350 (Scm_GaucheInternalModule))
351 (return (Scm_ApplyRec1 unary-proc (SCM_CAR args))))]
352 [else (let* ([r::ScmObj (SCM_CAR args)])
353 (dolist [v (SCM_CDR args)] (set! r (Scm_Add r v)))
354 (return r))]))
355
356 (define-cproc - (arg1 :rest args) ::<number> :fast-flonum
357 (if (SCM_NULLP args)
358 (return (Scm_VMNegate arg1))
359 (begin (dolist [v args] (set! arg1 (Scm_Sub arg1 v)))
360 (return arg1))))
361
362 (define-cproc / (arg1 :rest args) ::<number> :fast-flonum
363 (if (SCM_NULLP args)
364 (return (Scm_VMReciprocal arg1))
365 (begin (dolist [v args] (set! arg1 (Scm_Div arg1 v)))
366 (return arg1))))
367
368 (select-module gauche.internal)
369 (define (%apply-unary-generic gf who obj)
370 ;; TRANSIENT: Up to 0.9.14, we return the argument as is from unary + and *
371 ;; unconditionally. For the backward compatibility if there's no unary
372 ;; object-+ / object-*, we warn so and returns the argument instead of
373 ;; raising an error. Should remove that part after a few releases.
374 ;; https://github.com/shirok/Gauche/issues/1012
375 (if (applicable? gf (class-of obj))
376 (gf obj)
377 (begin
378 (warn "operation ~a is not defined on ~s. \
379 This will be an error in future releases.\n" who obj)
380 obj)))
381 (define (%apply-unary-+ obj) (%apply-unary-generic object-+ '+ obj))
382 (define (%apply-unary-* obj) (%apply-unary-generic object-* '* obj))
383
384 (select-module scheme)
385
386 (define-cproc abs (obj) :fast-flonum :constant Scm_VMAbs)
387
388 (define-cproc quotient (n1 n2) :fast-flonum :constant
389 (return (Scm_Quotient n1 n2 NULL)))
390 (define-cproc remainder (n1 n2) :fast-flonum :constant
391 (return (Scm_Modulo n1 n2 TRUE)))
392 (define-cproc modulo (n1 n2) :fast-flonum :constant
393 (return (Scm_Modulo n1 n2 FALSE)))
394
395 (select-module gauche)
396 ;; gcd, lcm: these are the simplest ones. If you need efficiency, consult
397 ;; Knuth: "The Art of Computer Programming" Chap. 4.5.2
398 (define-in-module scheme (gcd . args)
399 (define (recn arg args)
400 (if (null? args)
401 arg
402 (recn ((with-module gauche.internal %gcd) arg (car args)) (cdr args))))
403 (let1 args (map (^[arg] (unless (integer? arg)
404 (error "integer required, but got" arg))
405 (abs arg))
406 args)
407 (cond [(null? args) 0]
408 [(null? (cdr args)) (car args)]
409 [else (recn (car args) (cdr args))])))
410
411 (define-in-module scheme (lcm . args)
412 (define (lcm2 u v)
413 (let1 g ((with-module gauche.internal %gcd) u v)
414 (if (zero? u) 0 (* (quotient u g) v))))
415 (define (recn arg args)
416 (if (null? args)
417 arg
418 (recn (lcm2 arg (car args)) (cdr args))))
419 (let1 args (map (^[arg] (unless (integer? arg)
420 (error "integer required, but got" arg))
421 (abs arg))
422 args)
423 (cond [(null? args) 1]
424 [(null? (cdr args)) (car args)]
425 [else (recn (car args) (cdr args))])))
426
427 (select-module gauche.internal)
428 (define-cproc %gcd (n1 n2) :fast-flonum :constant Scm_Gcd)
429
430 (select-module scheme)
431 (define-cproc numerator (n) :fast-flonum :constant Scm_Numerator)
432 (define-cproc denominator (n) :fast-flonum :constant Scm_Denominator)
433
434 (select-module gauche)
435 (define-in-module scheme (rationalize x e)
436 ;; NB: real->rational is in gauche/numerical.scm
437 (cond
438 [(< e 0) (error "rationalize needs nonnegative error bound, but got" e)]
439 [(or (nan? x) (nan? e)) +nan.0]
440 [(infinite? e) (if (infinite? x) +nan.0 0.0)]
441 [(infinite? x) x]
442 [(or (inexact? x) (inexact? e)) (inexact (real->rational x e e))]
443 [else (real->rational x e e)]))
444
445 (select-module scheme)
446 (define-cproc floor (v) ::<number> :fast-flonum :constant
447 (return (Scm_Round v SCM_ROUND_FLOOR)))
448 (define-cproc ceiling (v) ::<number> :fast-flonum :constant
449 (return (Scm_Round v SCM_ROUND_CEIL)))
450 (define-cproc truncate (v) ::<number> :fast-flonum :constant
451 (return (Scm_Round v SCM_ROUND_TRUNC)))
452 (define-cproc round (v) ::<number> :fast-flonum :constant
453 (return (Scm_Round v SCM_ROUND_ROUND)))
454
455 ;; Transcedental functions. First, real-only versions.
456 ;; The name real-* is coined in SRFI-94.
457 ;; NB: We don't support 'real-log' of SRFI-94. It takes base number first,
458 ;; which is reverse of R7RS 'log'. It would be too confusing.
459 (select-module gauche)
460 (define-cproc real-exp (x::<double>) ::<double> :fast-flonum :constant exp)
461 (define-cproc real-ln (x::<number>) ::<double> :fast-flonum :constant
462 (unless (SCM_REALP x) (SCM_TYPE_ERROR x "real number"))
463 (when (< (Scm_Sign x) 0)
464 (Scm_Error "Argument must be nonnegative real number: %S" x))
465 ;; We can't simply cast x to double, for x can be a large bignum
466 ;; outside of double.
467 (let* ([d::double (Scm_GetDouble x)])
468 (when (== d SCM_DBL_POSITIVE_INFINITY)
469 (if (SCM_BIGNUMP x)
470 (let* ([z::ScmBits* (cast ScmBits* (-> (SCM_BIGNUM x) values))]
471 [scale::long (- (Scm_BitsHighest1 z 0 (* (SCM_BIGNUM_SIZE x) SCM_WORD_BITS)) 53)])
472 (return (+ (log (Scm_GetDouble
473 (Scm_DivInexact x (Scm_Ash (SCM_MAKE_INT 1) scale))))
474 (* scale (log 2.0)))))
475 (return SCM_DBL_POSITIVE_INFINITY)))
476 (return (log d))))
477
478 (select-module gauche)
479 (define-cproc real-sin (x::<double>) ::<double> :fast-flonum :constant sin)
480 (define-cproc real-cos (x::<double>) ::<double> :fast-flonum :constant cos)
481 (define-cproc real-tan (x::<double>) ::<double> :fast-flonum :constant tan)
482
483 (define-cproc real-sinpi (x::<double>) ::<double> :fast-flonum :constant Scm_SinPi)
484 (define-cproc real-cospi (x::<double>) ::<double> :fast-flonum :constant Scm_CosPi)
485 (define-cproc real-tanpi (x::<double>) ::<double> :fast-flonum :constant Scm_TanPi)
486
487 (define-cproc real-asin (x::<double>) ::<number> :fast-flonum :constant
488 (cond [(> x 1.0)
489 (return (Scm_MakeComplex (/ M_PI 2.0)
490 (- (log (+ x (sqrt (- (* x x) 1.0)))))))]
491 [(< x -1.0)
492 (return (Scm_MakeComplex (/ (- M_PI) 2.0)
493 (- (log (- (- x) (sqrt (- (* x x) 1.0)))))))]
494 [else (return (Scm_VMReturnFlonum (asin x)))]))
495
496 (define-cproc real-acos (x::<double>) ::<number> :fast-flonum :constant
497 (cond [(> x 1.0)
498 (return (Scm_MakeComplex 0 (log (+ x (sqrt (- (* x x) 1.0))))))]
499 [(< x -1.0)
500 (return (Scm_MakeComplex 0 (log (+ x (sqrt (- (* x x) 1.0))))))]
501 [else (return (Scm_VMReturnFlonum (acos x)))]))
502
503 (define-cproc real-atan (z::<double> :optional x) ::<double> :fast-flonum :constant
504 (cond [(SCM_UNBOUNDP x) (return (atan z))]
505 [else (unless (SCM_REALP x) (SCM_TYPE_ERROR x "real number"))
506 (return (atan2 z (Scm_GetDouble x)))]))
507
508 (define-cproc real-sinh (x::<double>) ::<double> :fast-flonum :constant sinh)
509 (define-cproc real-cosh (x::<double>) ::<double> :fast-flonum :constant cosh)
510 (define-cproc real-tanh (x::<double>) ::<double> :fast-flonum :constant tanh)
511 (define-cproc real-asinh (x::<double>) ::<double> :fast-flonum :constant asinh)
512 (define-cproc real-acosh (x::<double>) ::<double> :fast-flonum :constant acosh)
513 (define-cproc real-atanh (x::<double>) ::<double> :fast-flonum :constant atanh)
514
515 (define-cproc real-sqrt (x::<double>) :fast-flonum :constant
516 (if (< x 0)
517 (return (Scm_MakeComplex 0.0 (sqrt (- x))))
518 (return (Scm_VMReturnFlonum (sqrt x)))))
519
520 ;; Fast path for typical case of sqrt. Handles positive flonum
521 ;; and exact integer between 0 and 2^52.
522 ;; If input is outside of the range, returns #f to fallback for
523 ;; expensive path.
524 (select-module gauche.internal)
525 (define-cproc %sqrt-fast-path (x) :fast-flonum :constant
526 (cond [(and (SCM_FLONUMP x) (>= (Scm_Sign x) 0))
527 (return (Scm_VMReturnFlonum (sqrt (SCM_FLONUM_VALUE x))))]
528 [(and (SCM_INTEGERP x)
529 (>= (Scm_Sign x) 0)
530 (>= (Scm_NumCmp SCM_2_52 x) 0))
531 (let* ([d::double (Scm_GetDouble x)]
532 [q::double (sqrt d)]
533 [qq::double (floor q)]
534 [dd::double (* qq qq)])
535 ;; NB: The result is in [0, 2^26], so we know it fits in fixnum.
536 (if (== d dd)
537 (return (SCM_MAKE_INT (cast long q)))
538 (return (Scm_VMReturnFlonum q))))]
539 [else (return SCM_FALSE)]))
540
541 (select-module gauche)
542 (define-cproc real-expt (x y) :fast-flonum :constant Scm_Expt)
543 (define-cproc exact-expt (x y::<integer>) :constant
544 (unless (SCM_EXACTP x) (SCM_TYPE_ERROR x "exact real number"))
545 (return (Scm_ExactIntegerExpt x y)))
546 (define-cproc integer-expt (x::<integer> y::<integer>) ::<integer> :constant
547 Scm_ExactIntegerExpt)
548
549 ;; Now, handles complex numbers.
550 ;; Cf. Teiji Takagi: "Kaiseki Gairon" pp.193--198
551 (define-in-module scheme (exp z)
552 (cond [(real? z) (real-exp z)]
553 [(complex? z) (make-polar (real-exp (real-part z)) (imag-part z))]
554 [else (error "number required, but got" z)]))
555
556 (define-in-module scheme (log z . base)
557 (if (null? base)
558 (cond [(real? z)
559 (if (<= 0 z)
560 (real-ln z)
561 ;; The constant pi is not avaialble here, but (* 4 (real-atan 1))
562 ;; is constand-foled to pi.
563 (make-rectangular (real-ln (- z)) (* 4 (real-atan 1))))]
564 [(complex? z) (make-rectangular (real-ln (magnitude z)) (angle z))]
565 [else (error "number required, but got" z)])
566 (/ (log z) (log (car base))))) ; R6RS addition
567
568 (select-module gauche.internal)
569 (define-in-module scheme (sqrt z)
570 (cond
571 [(%sqrt-fast-path z)] ; fast-path check
572 [(and (exact? z) (>= z 0))
573 ;; Gauche doesn't have exact complex, so we have real z.
574 (if (integer? z)
575 (receive (s r) (%exact-integer-sqrt z)
576 (if (= r 0) s (real-sqrt z)))
577 (let ([n (numerator z)]
578 [d (denominator z)])
579 (if-let1 nq (%sqrt-fast-path n)
580 (if-let1 dq (%sqrt-fast-path d)
581 (/ nq dq)
582 (receive (ds dr) (%exact-integer-sqrt d)
583 (if (= dr 0)
584 (/ nq ds)
585 (real-sqrt z))))
586 (receive (ns nr) (%exact-integer-sqrt n)
587 (if (= nr 0)
588 (receive (ds dr) (%exact-integer-sqrt d)
589 (if (= dr 0)
590 (/ ns ds)
591 (real-sqrt z)))
592 (real-sqrt z))))))]
593 [(real? z) (real-sqrt z)]
594 [(complex? z) (make-polar (real-sqrt (magnitude z)) (/ (angle z) 2.0))]
595 [else (error "number required, but got" z)]))
596
597 (define-in-module gauche.internal (%exact-integer-sqrt k) ; k >= 0
598 (if (< k 4503599627370496) ;2^52
599 ;; k can be converted to a double without loss.
600 (let1 s (floor->exact (real-sqrt k))
601 (values s (- k (* s s))))
602 ;; use Newton-Rhapson
603 ;; If k is representable with double, we use (real-sqrt k) as the initial
604 ;; estimate, for calculating double sqrt is fast. If k is too large,
605 ;; we use 2^floor((log2(k)+1)/2) as the initial value.
606 (let loop ([s (let1 ik (real-sqrt k)
607 (if (finite? ik)
608 (floor->exact (real-sqrt k))
609 (ash 1 (quotient (integer-length k) 2))))])
610 (let1 s2 (* s s)
611 (if (< k s2)
612 (loop (quotient (+ s2 k) (* 2 s)))
613 (let1 s2+ (+ s2 (* 2 s) 1)
614 (if (< k s2+)
615 (values s (- k s2))
616 (loop (quotient (+ s2 k) (* 2 s))))))))))
617
618 (define-in-module scheme (expt x y)
619 (cond [(and (exact? x) (exact? y))
620 ((with-module gauche.internal %exact-expt) x y)]
621 [(real? x)
622 (cond [(real? y) (real-expt x y)]
623 [(number? y)
624 (let1 ry (real-part y)
625 (if (and (zero? x) (positive? ry))
626 (if (exact? x) 0 0.0)
627 (* (real-expt x ry)
628 (exp (* +i (imag-part y) (real-ln x))))))]
629 [else (error "number required, but got" y)])]
630 [(number? x) (exp (* y (log x)))]
631 [else (error "number required, but got" x)]))
632
633 (select-module gauche.internal)
634 (define (%exact-expt x y) ;; x, y :: exact
635 (cond [(integer? y) (exact-expt x y)]
636 [(< x 0) (real-expt x y)] ; we don't have exact compnum
637 [(< y 0) (/ (%exact-expt x (- y)))]
638 [(integer? x)
639 (let ([a (numerator y)]
640 [b (denominator y)])
641 ;; Calculate b-th root of x by Newton-Rhapson.
642 ;;
643 ;; expt(x, 1/b) == expt(2, (log_2 x)/b)
644 ;; =:= ash(1, round((log_2 x)/b))
645 ;;
646 ;; So we start from a initial value r_0 = ash(1, round((log_2 x)/b)),
647 ;; then refine it by
648 ;;
649 ;; r_{N+1} = r_N - ceil( (r_N^b - x)/br_N^{b-1} )
650 ;;
651 ;; Since we're looking for an integer solution, we can give up
652 ;; when the following condition is met:
653 ;;
654 ;; ( r_N+1 - r_N == 1 AND r_N^b < x < r_{N+1}^b )
655 ;; OR ( r_N - r_N+1 == 1 AND r_{N+1}^b < x < r_N^b )
656 ;;
657 ;; In that case, we fall back to inexact calculation by real-expt.
658 (let* ([r (ash 1 (round->exact (/ (log x 2) b)))]
659 [s (integer-expt r b)])
660 (if (= s x)
661 (integer-expt r a)
662 (let loop ([r r] [s s])
663 (let* ([deliv (* b (integer-expt r (- b 1)))]
664 [err (- s x)]
665 [delta (if (> err 0)
666 (quotient (+ err deliv -1) deliv)
667 (quotient (- err deliv -1) deliv))]
668 [r2 (- r delta)]
669 [s2 (integer-expt r2 b)])
670 (if (= s2 x)
671 (integer-expt r2 a)
672 (if (or (and (= delta 1) (< s2 x s))
673 (and (= delta -1) (< s x s2)))
674 (real-expt x y)
675 (loop r2 s2))))))))]
676 [else
677 ;; x is rational
678 (or (and-let* ([xn (%exact-expt (numerator x) y)]
679 [ (exact? xn) ]
680 [xd (%exact-expt (denominator x) y)]
681 [ (exact? xd) ])
682 (/ xn xd))
683 (real-expt x y))]))
684
685 (select-module gauche)
686 (define-in-module scheme (cos z)
687 (cond [(real? z) (real-cos z)]
688 [(number? z)
689 (let ([x (real-part z)]
690 [y (imag-part z)])
691 (make-rectangular (* (real-cos x) (real-cosh y))
692 (- (* (real-sin x) (real-sinh y)))))]
693 [else (error "number required, but got" z)]))
694
695 (define (cosh z)
696 (cond [(real? z) (real-cosh z)]
697 [(number? z)
698 (let ([x (real-part z)]
699 [y (imag-part z)])
700 (make-rectangular (* (real-cosh x) (real-cos y))
701 (* (real-sinh x) (real-sin y))))]
702 [else (error "number required, but got" z)]))
703
704 (define-in-module scheme (sin z)
705 (cond [(real? z) (real-sin z)]
706 [(number? z)
707 (let ([x (real-part z)]
708 [y (imag-part z)])
709 (make-rectangular (* (real-sin x) (real-cosh y))
710 (* (real-cos x) (real-sinh y))))]
711 [else (error "number required, but got" z)]))
712
713 (define (sinh z)
714 (cond [(real? z) (real-sinh z)]
715 [(number? z)
716 (let ([x (real-part z)]
717 [y (imag-part z)])
718 (make-rectangular (* (real-sinh x) (real-cos y))
719 (* (real-cosh x) (real-sin y))))]
720 [else (error "number required, but got" z)]))
721
722 (define-in-module scheme (tan z)
723 (cond [(real? z) (real-tan z)]
724 [(number? z)
725 (let1 iz (* +i z)
726 (* -i
727 (/ (- (exp iz) (exp (- iz)))
728 (+ (exp iz) (exp (- iz))))))]
729 [else (error "number required, but got" z)]))
730
731 (define (tanh z)
732 (cond [(real? z) (real-tanh z)]
733 [(number? z) (/ (- (exp z) (exp (- z)))
734 (+ (exp z) (exp (- z))))]
735 [else (error "number required, but got" z)]))
736
737 (define-in-module scheme (asin z)
738 (cond [(real? z) (real-asin z)]
739 [(number? z)
740 ;; The definition of asin is
741 ;; (* -i (log (+ (* +i z) (sqrt (- 1 (* z z))))))
742 ;; This becomes unstable when the term in the log is reaching
743 ;; toward 0.0. The term, k = (+ (* +i z) (sqrt (- 1 (* z z)))),
744 ;; gets closer to zero when |z| gets bigger, but for large |z|,
745 ;; k is prone to lose precision and starts drifting around
746 ;; the point zero.
747 ;; For now, I let asin to return NaN in such cases.
748 (let1 zz (+ (* +i z) (sqrt (- 1 (* z z))))
749 (if (< (/. (magnitude zz) (magnitude z)) 1.0e-8)
750 (make-rectangular +nan.0 +nan.0)
751 (* -i (log zz))))]
752 [else (error "number required, but got" z)]))
753
754 (define (asinh z)
755 (cond [(real? z) (real-asinh z)]
756 [(number? z)
757 (let1 zz (+ z (sqrt (+ (* z z) 1)))
758 (if (< (/. (magnitude zz) (magnitude z)) 1.0e-8)
759 (make-rectangular +nan.0 +nan.0)
760 (log zz)))]
761 [else (error "number required, but got" z)]))
762
763 (define-in-module scheme (acos z)
764 (cond [(real? z) (real-acos z)]
765 [(number? z)
766 ;; The definition of acos is
767 ;; (* -i (log (+ z (* +i (sqrt (- 1 (* z z)))))))))
768 ;; This also falls in the victim of numerical unstability; worse than
769 ;; asin, sometimes the real part of marginal value "hops" between
770 ;; +pi and -pi. It's rather stable to use asin.
771 (- 1.5707963267948966 (asin z))]
772 [else (error "number required, but got" z)]))
773
774 (define (acosh z)
775 (cond [(real? z) (real-acosh z)]
776 [(number? z);; See the discussion of CLtL2, pp. 313-314
777 (* 2 (log (+ (sqrt (/ (+ z 1) 2))
778 (sqrt (/ (- z 1) 2)))))]
779 [else (error "number required, but got" z)]))
780
781 (define-in-module scheme (atan z . x)
782 (if (null? x)
783 (cond [(real? z) (real-atan z)]
784 [(number? z)
785 (let1 iz (* z +i)
786 (/ (- (log (+ 1 iz))
787 (log (- 1 iz)))
788 +2i))]
789 [else (error "number required, but got" z)])
790 (cond [(and (eq? (car x) 0) (zero? z))
791 ;; Special case. See R7RS.
792 (cond [(eq? z 0) +nan.0]
793 [(negative-zero? z) -1.5707963267948966]
794 [else +1.5707963267948966])]
795 [(and (real? z) (real? (car x))) (real-atan z (car x))]
796 [else (error "two-argument atan requires real numbers, but got"
797 (list z (car x)))])))
798
799 (define (atanh z)
800 (cond [(real? z) (real-atanh z)]
801 [(number? z) (/ (- (log (+ 1 z)) (log (- 1 z))) 2)]
802 [else (error "number required, but got" z)]))
803
804 (select-module gauche)
805 (define-cproc radians->degrees (r::<double>) ::<double> :constant :fast-flonum
806 (return (* r (/ 180 M_PI))))
807 (define-cproc degrees->radians (d::<double>) ::<double> :constant :fast-flonum
808 (return (* d (/ M_PI 180))))
809
810 (select-module gauche)
811
812 (define-cproc ash (num cnt::<fixnum>) ::<integer> :constant Scm_Ash)
813
814 (define-cproc lognot (x::<integer>) ::<integer> :constant Scm_LogNot)
815
816 (inline-stub
817 (define-cise-stmt logop
818 [(_ fn ident)
819 `(cond [(== optcnt 0) (return ,ident)]
820 [(== optcnt 1)
821 (unless (SCM_INTEGERP (aref arg2 0))
822 (Scm_Error "Exact integer required, but got %S" (aref arg2 0)))
823 (return (aref arg2 0))]
824 [else
825 (let* ([r (,fn (aref arg2 0) (aref arg2 1))])
826 (for-each (lambda (v) (set! r (,fn r v))) args)
827 (return r))])])
828 )
829
830 (define-cproc logand (:optarray (arg2 optcnt 2) :rest args) ::<integer> :constant
831 (logop Scm_LogAnd (SCM_MAKE_INT -1)))
832 (define-cproc logior (:optarray (arg2 optcnt 2) :rest args) ::<integer> :constant
833 (logop Scm_LogIor (SCM_MAKE_INT 0)))
834 (define-cproc logxor (:optarray (arg2 optcnt 2) :rest args) ::<integer> :constant
835 (logop Scm_LogXor (SCM_MAKE_INT 0)))
836
837 (define-cproc logcount (n::<integer>) ::<int> :constant
838 (cond [(SCM_EQ n (SCM_MAKE_INT 0)) (return 0)]
839 [(SCM_INTP n)
840 (let* ([z::ScmBits (cast ScmBits (cast long (SCM_INT_VALUE n)))])
841 (if (> (SCM_INT_VALUE n) 0)
842 (return (Scm_BitsCount1 (& z) 0 SCM_WORD_BITS))
843 (return (Scm_BitsCount0 (& z) 0 SCM_WORD_BITS))))]
844 [(SCM_BIGNUMP n) (return (Scm_BignumLogCount (SCM_BIGNUM n)))]
845 [else (SCM_TYPE_ERROR n "exact integer") (return 0)]))
846
847 (define-cproc logset+clear (n::<integer> sets::<integer> clears::<integer>)
848 ::<integer> :constant
849 (return (Scm_LogAnd (Scm_LogIor n sets) (Scm_LogNot clears))))
850
851 (define-cproc integer-length (n::<integer>) ::<ulong> :constant Scm_IntegerLength)
852
853 ;; Returns maximum s where (expt 2 s) is a factor of n.
854 ;; This can be (- (integer-length (logxor n (- n 1))) 1), but we can save
855 ;; creating intermediate numbers by providing this natively.
856 (define-cproc twos-exponent-factor (n::<integer>) ::<int> :constant
857 (cond [(SCM_EQ n (SCM_MAKE_INT 0)) (return -1)]
858 [(SCM_INTP n)
859 (let* ([z::ScmBits (cast ScmBits (cast long (SCM_INT_VALUE n)))])
860 (return (Scm_BitsLowest1 (& z) 0 SCM_WORD_BITS)))]
861 [(SCM_BIGNUMP n)
862 (let* ([z::ScmBits* (cast ScmBits* (-> (SCM_BIGNUM n) values))]
863 [k::int (SCM_BIGNUM_SIZE n)])
864 (return (Scm_BitsLowest1 z 0 (* k SCM_WORD_BITS))))]
865 [else (SCM_TYPE_ERROR n "exact integer") (return 0)]))
866
867 (define-cproc twos-exponent (n::<integer>) ::<integer>? :constant
868 (let* ([i::long (Scm_TwosPower n)])
869 (return (?: (>= i 0) (Scm_MakeInteger i) SCM_FALSE))))
870
871 ;; As of 0.8.8 we started to support exact rational numbers. Some existing
872 ;; code may count on exact integer division to be coerced to flonum
873 ;; if it isn't produce a whole number, and such programs start
874 ;; running very slowly on 0.8.8 by introducing unintentional exact
875 ;; rational arithmetic.
876 ;;
877 ;; For the smooth transition, we provide the original behavior as
878 ;; inexact-/. If the program uses compat.no-rational, '/' is overridden
879 ;; by inexact-/ and the old code behaves the same.
880 (define-cproc inexact-/ (arg1 :rest args)
881 (cond [(SCM_NULLP args) (return (Scm_ReciprocalInexact arg1))]
882 [else (dolist [x args] (set! arg1 (Scm_DivCompat arg1 x)))
883 (return arg1)]))
884
885 ;; Inexact arithmetics. Useful for speed-sensitive code to avoid
886 ;; accidental use of bignum or ratnum. We might want to optimize
887 ;; these more, even adding special VM insns for them.
888 (define-cproc +. (:rest args) :constant
889 (let* ([a '0.0])
890 (dolist [x args] (set! a (Scm_Add a (Scm_Inexact x))))
891 (return a)))
892 (define-cproc *. (:rest args) :constant
893 (let* ([a '1.0])
894 (dolist [x args] (set! a (Scm_Mul a (Scm_Inexact x))))
895 (return a)))
896 (define-cproc -. (arg1 :rest args) :constant
897 (cond
898 [(SCM_NULLP args) (return (Scm_Negate (Scm_Inexact arg1)))]
899 [else (dolist [x args] (set! arg1 (Scm_Sub arg1 (Scm_Inexact x))))
900 (return arg1)]))
901 (define-cproc /. (arg1 :rest args) :constant
902 (cond
903 [(SCM_NULLP args) (return (Scm_Reciprocal (Scm_Inexact arg1)))]
904 [else (dolist [x args] (set! arg1 (Scm_DivInexact arg1 x)))
905 (return arg1)]))
906
907 (define-cproc clamp (x :optional (min #f) (max #f)) :fast-flonum :constant
908 (let* ([r x] [maybe_exact::int (SCM_EXACTP x)])
909 (unless (SCM_REALP x) (SCM_TYPE_ERROR x "real number"))
910 (cond [(SCM_EXACTP min) (when (< (Scm_NumCmp x min) 0) (set! r min))]
911 [(SCM_FLONUMP min)
912 (set! maybe_exact FALSE)
913 (when (< (Scm_NumCmp x min) 0) (set! r min))]
914 [(not (SCM_FALSEP min)) (SCM_TYPE_ERROR min "real number or #f")])
915 (cond [(SCM_EXACTP max) (when (> (Scm_NumCmp x max) 0) (set! r max))]
916 [(SCM_FLONUMP max)
917 (set! maybe_exact FALSE)
918 (when (> (Scm_NumCmp x max) 0) (set! r max))]
919 [(not (SCM_FALSEP max)) (SCM_TYPE_ERROR max "real number or #f")])
920 (if (and (not maybe_exact) (SCM_EXACTP r))
921 (return (Scm_Inexact r))
922 (return r))))
923
924 (define-cproc quotient&remainder (n1 n2) ::(<top> <top>)
925 (set! SCM_RESULT0 (Scm_Quotient n1 n2 (& SCM_RESULT1))))
926
927 ;;
928 ;; Complex numbers
929 ;;
930
931 (select-module scheme)
932 (define-cproc make-rectangular (a::<double> b::<double>) ::<number> :constant
933 Scm_MakeComplex)
934 (define-cproc make-polar (r::<double> t::<double>) ::<number> :constant
935 Scm_MakeComplexPolar)
936
937 ;; we don't use Scm_RealPart and Scm_ImagPart, for preserving exactness
938 ;; and avoiding extra allocation.
939 (define-cproc real-part (z::<number>) :fast-flonum :constant
940 (if (SCM_REALP z)
941 (return z)
942 (return (Scm_VMReturnFlonum (SCM_COMPNUM_REAL z)))))
943
944 (define-cproc imag-part (z::<number>) :fast-flonum :constant
945 (cond [(SCM_EXACTP z) (return (SCM_MAKE_INT 0))]
946 [(SCM_REALP z) (return (Scm_VMReturnFlonum 0.0))]
947 [else (return (Scm_VMReturnFlonum (SCM_COMPNUM_IMAG z)))]))
948
949 (define-cproc magnitude (z::<number>) ::<number> :fast-flonum :constant Scm_VMAbs)
950 (define-cproc angle (z::<number>) ::<double> :fast-flonum :constant Scm_Angle)
951
952 ;; Utility to recognize clamp mode symbols and returns C enum
953 ;; (Mostly used in uvector-related apis)
954
955 (inline-stub
956 (define-cfn Scm_ClampMode (clamp) ::int
957 (cond [(SCM_EQ clamp 'both) (return SCM_CLAMP_BOTH)]
958 [(SCM_EQ clamp 'low) (return SCM_CLAMP_LO)]
959 [(SCM_EQ clamp 'high) (return SCM_CLAMP_HI)]
960 [(SCM_EQ clamp 'wraparound) (return SCM_CLAMP_WRAPAROUND)]
961 [(not (or (SCM_FALSEP clamp) (SCM_UNBOUNDP clamp)))
962 (Scm_Error "clamp argument must be either 'both, 'high, 'low, \
963 'wraparound or #f, but got %S" clamp)])
964 (return SCM_CLAMP_ERROR)))