How to compute a floating point modulo in Racket? [flmod]

Viewed 116

The standard modulo in Racket doesn't work with floating points.

How do I compute modulo with non-integer arguments (flmod)?

1 Answers

In some languages the modulo operator also works for non-integer arguments. In Racket the modulo operator is for integer arguments only,

In the case you need one that works with floating points, use the following definition:

(define (flmod x m)
    (- x (* (floor (/ x m)) m))

There is no standard definition for such an operation (different programming languages interprets this operation differently), so check that the function above matches you expectations first.

Related