;; ;; Conversion of floating-point numbers between IEEE binary ;; representation and decimal string representation. ;; ;; Based on the gdtoa library by David M. Gay ;; ;; ;; This Chicken extension was written by Ivan Raikov. ;; ;; Permission to use, copy, modify, and distribute this software and ;; its documentation for any purpose and without fee is hereby ;; granted, provided that the above copyright notice appear in all ;; copies and that both that the copyright notice and this permission ;; notice appear in supporting documentation. ;; (module fpio (export fp->string string->fp) (import scheme chicken data-structures foreign) (define (fp->string x . rest) (let-optionals rest ((ndig 0 )) (let* ((bufsize (max (+ ndig 20) 1024)) (buffer (make-blob bufsize))) (dtoa buffer x ndig bufsize)))) (define (string->fp s . rest) (let-optionals rest ((rounding 'toward-zero )) (strtord s (case rounding ((toward-zero) 0) ((nearest) 1) ((toward+Inf) 2) ((toward-Inf) 3))))) #> #include <# ;; strtord(CONST char *s, char **sp, int rounding, double *d) ;; rounding_mode values: ;; 0 = toward zero ;; 1 = nearest ;; 2 = toward +Infinity ;; 3 = toward -Infinity (define strtord (foreign-lambda* double ((nonnull-c-string s) (integer rounding)) #<