How modulo and remainder should work in Scheme?

14.8k Views Asked by At

I'm reading R5RS spec and it shows this:

(modulo 13 4)                   ===>  1
(remainder 13 4)                ===>  1

(modulo -13 4)                  ===>  3
(remainder -13 4)               ===>  -1

(modulo 13 -4)                  ===>  -3
(remainder 13 -4)               ===>  1

(modulo -13 -4)                 ===>  -1
(remainder -13 -4)              ===>  -1

(remainder -13 -4.0)            ===>  -1.0  ; inexact

Is this correct? I thought that modulo and remainder differ only in minus sign. And here it shows that (modulo -13 4) should return 3, in JavaScript it returns 1.

What are proper algorithms to calculate modulo and remainder? I need this for my Scheme in JavaScript implementation.

I've found this code at quora.

function modulo(num1, num2) {
  if (num2 === 0 || isNaN(num1) || isNaN(num2)) {
    return NaN;
  }

  var isPositive = num1 >= 0;

  num1 = Math.abs(num1);
  num2 = Math.abs(num2);

  while (num1 >= num2) {
    num1 = num1 - num2;
  }

  return isPositive ? num1 : -num1;
}

but it don't work like in R5RS spec, it returns -1 for modulo(-13, 4). Also I thought that JavaScript's % is the same as remainder. How to implement both functions in JavaScript or in Scheme?

My exact question is: how the algorithm for both functions should look like or how JavaScript code to calculate them both should look like?

4

There are 4 best solutions below

0
jcubic On BEST ANSWER

If someone is interested I've asked the same question on Reddit (with link to this question) and got the answer with exact scheme code:

https://www.reddit.com/r/scheme/comments/fpt1b8/help_with_modulo_and_reminder_in_r5rs/

(define (modulo a b)
  (- a (* b (floor (/ a b)))))

(define (remainder a b)
  (- a (* b (truncate (/ a b)))))

;; as @soegaard show reminder is just JavaScript % so this can be
;; if % is proper function
(define (remainder a b)
  (% a b))

it works the same with examples from R5RS:

(list
  (= (modulo 13 4) 1)
  (= (remainder 13 4) 1)      ;; ===>  1

  (= (modulo -13 4) 3)        ;; ===>  3
  (= (remainder -13 4) -1)    ;; ===>  -1

  (= (modulo 13 -4) -3)       ;; ===>  -3
  (= (remainder 13 -4) 1)     ;; ===>  1

  (= (modulo -13 -4) -1)      ;; ===>  -1
  (= (remainder -13 -4) -1)   ;; ===>  -1

  (= (remainder -13 -4.0) -1.0)) ;; ===>  -1.0  ; inexact

floor is Math.floor and truncate is:

var truncate = (function() {
    if (Math.trunc) {
        return Math.trunc;
    } else {
        return function(x) {
            if (x === 0) {
                return 0;
            } else if (x < 0) {
                return Math.ceil(x);
            } else {
                return Math.floor(x);
            }
        };
    }
})();
1
ceving On
6
soegaard On

Here is how I implemented the functions in Urlang:

    (define/export/arity (quotient n m) 
      (Math.floor (/ n m)))

    (define/export/arity (remainder n m)
      (% n m))

    (define/export/arity (quotient/remainder n m)
      (values (quotient n m) (remainder n m)))

    (define/export/arity (modulo n m)
      (var [who (λ() (string->symbol "modulo"))])
      (unless (and (number? n) (not (infinite? n))) 
         (raise-argument-error (who) "integer?" 1 n m))
      (unless (and (number? m) (not (infinite? m))) 
         (raise-argument-error (who) "integer?" 2 n m))
      (% (+ (% n m) m) m))

Here Math.floor, % and + are the standard JavaScript functions/operators.

For the curious, here is the JavaScript produced:

function remainder(n,m){if(!((arguments.length===2)===false))VOID;else (raise_arity_error_m((string__gsymbol("remainder")),2,arguments));;return (n%m);};
function quotient_qremainder(n,m){if(!((arguments.length===2)===false))VOID;else (raise_arity_error_m((string__gsymbol("quotient/remainder")),2,arguments));;return (values((quotient(n,m)),(remainder(n,m))));};
function modulo(n,m){if(!((arguments.length===2)===false))VOID;else (raise_arity_error_m((string__gsymbol("modulo")),2,arguments));;var who=(function(){return (string__gsymbol("modulo"));});(((!((number_p(n))&&(!(infinite_p(n)))))===false)?undefined:((function(){return (raise_argument_error((who()),"integer?",1,n,m));})()));(((!((number_p(m))&&(!(infinite_p(m)))))===false)?undefined:((function(){return (raise_argument_error((who()),"integer?",2,n,m));})()));return (((n%m)+m)%m);};

UPDATE

Here is a little context. The code implements remainder and modulo for a Scheme runtime. The runtime is implemented in Urlang (which is JavaScript with s-expression-syntax).

From the output JavaScript, you can see that:

  • Scheme remainder is implemented as n%m.
  • Scheme modulo is implemented as ((n%m)+m)%m

This assumes that Scheme numbers are represented as JavaScript numbers (i.e. no bignums).

2
Arthur A. Gleckler On

You might be interested in SRFI 141: Integer division, by Taylor Campbell. It is a proposed extension to Scheme that "...provides a fairly complete set of integral division and remainder operators."