git @ Cat's Eye Technologies Robin / master stdlib / remainder.robin
master

Tree @master (Download .tar.gz)

remainder.robin @masterraw · history · blame

;'<<SPEC'

<!--
Copyright (c) 2012-2024, Chris Pressey, Cat's Eye Technologies.
This file is distributed under a 2-clause BSD license.  See LICENSES/ dir.
SPDX-License-Identifier: LicenseRef-BSD-2-Clause-X-Robin
-->

### `remainder` ###

    -> Tests for functionality "Evaluate Robin Expression (with Arith)"

`remainder` evaluates both of its arguments to numbers and evaluates to the
remainder of the division of the first number by the second.

    | (remainder 12 3)
    = 0

    | (remainder 11 3)
    = 2

    | (remainder 10 3)
    = 1

    | (remainder 9 3)
    = 0

The remainder is *always non-negative*.

    | (remainder (subtract 0 10) 3)
    = 2

    | (remainder 10 (subtract 0 3))
    = 2

Trying to find the remainder of a division by zero is undefined, and an
abort value will be produced.

    | (remainder 10 0)
    ? abort (division-by-zero 10)

`remainder` expects exactly two arguments, both numbers.

    | (remainder 14)
    ? abort (illegal-arguments

    | (remainder 14 23 57)
    ? abort (illegal-arguments

    | (remainder 14 #t)
    ? abort (expected-number #t)

    | (remainder #t 51)
    ? abort (expected-number #t)

'<<SPEC'

(define remainder (fun (n d)
  (bind remainder-r-pos (fun (self n d acc) ;(d is positive)
    (if (gt? d n)
      n
      (self self (subtract n d) d (add 1 acc))))
    (bind remainder-r-neg (fun (self n d acc) ;(d is negative)
      (if (gt? (abs d) n)
        (add 1 n)
        (self self (add n d) d (add 1 acc))))
      (if (equal? d 0)
        (abort (list (literal division-by-zero) n))
        (bind n-prime (if (lt? n 0) (subtract 0 n) n)
          (bind d-prime (if (lt? n 0) (subtract 0 d) d)
            (if (gt? d-prime 0)
              (remainder-r-pos remainder-r-pos n-prime d-prime 0)
              (remainder-r-neg remainder-r-neg n-prime d-prime 0)))))))))