Rebol Programming/mod

From Wikibooks, open books for an open world
Jump to navigation Jump to search

USAGE:[edit | edit source]

MOD a b 

DESCRIPTION:[edit | edit source]

Compute a nonnegative remainder of A divided by B.

MOD is a function value.

ARGUMENTS:[edit | edit source]

  • a -- (Type: number money time)
  • b -- Must be nonzero. (Type: number money time)

(SPECIAL ATTRIBUTES)[edit | edit source]

  • catch

SOURCE CODE[edit | edit source]

mod: func [
    "Compute a nonnegative remainder of A divided by B." 
    [catch] 
    a [number! money! time!] 
    b [number! money! time!] "Must be nonzero." 
    /local r
][
    all [negative? r: a // b r: r + b] 
    a: abs a 
    either all [a + r = (a + b) positive? r + r - b] [r - b] [r]
]