As a side effect of my Joy-in-Scheme effort, I put together a tiny
Chicken shim providing access to the procedures in the ISO C89 <math.h>
header that are not available in Chicken already. Because it's so short,
I'm attaching it here.
I'm not sure what the best way is to package this. Since its overhead
is near zero, I think the best final home for it is in either the library
or the extras unit (I'm not clear on what should go where with those two)
so that the procedures will routinely be available in the interpreter.
Alternatively it could be a standard unit or an egg. Failing that,
you can just (include "mathh.scm") into code to be compiled.
--
That you can cover for the plentiful John Cowan
and often gaping errors, misconstruals, http://www.ccil.org/~cowan
and disinformation in your posts [EMAIL PROTECTED]
through sheer volume -- that is another
misconception. --Mike to Peter
;;;; mathh.scm
;;;; Provides access to ISO C math functions in <math.h>
;;;; that are not defined by Chicken
;;;; Public domain
;;; Scheme-level declarations
(declare (bound-to-procedure cosh sinh tanh log10 fmod modf ldexp frexp))
;;; Include proper C header
(foreign-declare "#include <math.h>")
;;; Hyperbolic functions
(define cosh (foreign-lambda double cosh double))
(define sinh (foreign-lambda double sinh double))
(define tanh (foreign-lambda double tanh double))
;;; Base-10 logarithm
(define log10 (foreign-lambda double log10 double))
;;; Flonum remainder
(define fmod (foreign-lambda double fmod double double))
;;; Return integer, fraction (as multiple values) of a flonum
(define modf (foreign-primitive ((double x))
"double ipart;
double result;
C_word* values;
C_word value1;
C_word value2;
result = modf(x, &ipart);
values = C_alloc(2*C_SIZEOF_FLONUM);
value1 = C_flonum(&values, ipart);
value2 = C_flonum(&values, result);
C_values(4, C_SCHEME_UNDEFINED, C_k, value1, value2);
"))
;;; Efficiently compute (* x (expt 2 y))
(define ldexp (foreign-lambda double ldexp double double))
;;; Return mantissa, exponent (as multiple values) of a flonum
(define frexp (foreign-primitive ((double x))
"int exp;
double result;
C_word* values;
C_word value1;
C_word value2;
result = frexp(x, &exp);
values = C_alloc(C_SIZEOF_FLONUM);
value1 = C_flonum(&values, result);
value2 = C_fix(exp);
C_values(4, C_SCHEME_UNDEFINED, C_k, value1, value2);
"))
_______________________________________________
Chicken-users mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/chicken-users