# [plt-scheme] modulo with real

At Mon, 16 May 2005 18:05:29 +0200 (CEST), Bocdu jonf wrote:
>* How can I express " a modulo b" with dr scheme if "a" is a number (real or
*>* not) and "b" is a real ?
*
Assuming that you want something like C's `fmod', you might use the
code in plt/collects/mzscheme/examples, "fmod.c" or "fmod-ez.c".
Here's a quick attempt directly in Scheme:
(define (mod a b)
(with-handlers ([exn:fail?
(lambda (exn)
;; Maybe failed because of inf/nan arguments:
(if (or (member a '(+inf.0 -inf.0 +nan.0))
(member b '(+inf.0 -inf.0 +nan.0)))
+nan.0
(raise exn)))])
(let* ([ae (inexact->exact (abs a))]
[be (inexact->exact (abs b))]
[k (floor (/ ae be))]
[m ((if (negative? a) - +) (- ae (* be k)))])
(if (or (inexact? a)
(inexact? b))
(exact->inexact m)
m))))
Matthew