[REBOL] Re: mathematical rounding...
From: greggirwin:mindspring at: 6-Nov-2001 10:58
Hi Max,
Here's is Ladislav's rounding.r module, though there may be others out there
as well.
Rebol [
Title: "Rounding"
Purpose: {Rounding functions}
Author: "Ladislav Mecir"
Date: 2/8/2000
Email: [lmecir--geocities--com]
File: %rounding.r
Category: [Math]
]
mod: func [
{compute a non-negative remainder}
a [number!]
b [number!]
/local r
] [
either negative? r: a // b [
r + abs b
] [r]
]
round: func ["Round a number"
n [number!]
/places
p [integer!] {Decimal places - can be negative}
/local factor r
] [
factor: either places [10 ** (- p)] [1]
n: 0.5 * factor + n
n - mod n factor
]
floor: func [
n [number!]
/places
p [integer!] {Decimal places - can be negative}
/local factor r
] [
factor: either places [10 ** (- p)] [1]
n - mod n factor
]
ceiling: func [
n [number!]
/places
p [integer!] {Decimal places - can be negative}
/local factor r
] [
factor: either places [10 ** (- p)] [1]
n + mod (- n) factor
]
truncate: func [
n [number!]
/places
p [integer!] {Decimal places - can be negative}
/local factor r
] [
factor: either places [10 ** (- p)] [1]
n - (n // factor)
]
--Gregg