Search code examples
schemeracketarithmetic-expressions

How do you get the remainder of a decimal in Racket?


I'm looking at Racket as a friendly introduction to lisp, but I'm missing something. I need to simplify angles, so all I need to do get n mod 360. The problem is n may be a decimal. I looked through the documentation, but "remainder" and "modulo" both expect integers.

The flonum module looked promising, but doesn't look like it has a % function.

I ended up reimplementing it as this expression, which works well enough:

(define (float-modulo n m)
  (- n (* (floor (/ n m)) m)))

But I'd rather not do that if this is already supported.


Solution

  • Racket provides the R6RS division operators via the rnrs/base-6 library, so you can do:

    (require rnrs/base-6)
    (mod 370.25 360)
    > 10.25