Mailing List Archive: 49091 messages

## [REBOL] Re: mathematical rounding...

### From: ryanc:iesco-dms at: 6-Nov-2001 10:23

```
Performance was pretty good for what you get, but not as fast as the functions I
posted. One thing though...

>> round -5.5
== -5

This appears to be the wrong answer to me, but I am hardly a math scholar.

--Ryan

Gregg Irwin wrote:

> 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}
>     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
>
> --
> To unsubscribe from this list, please send an email to
> [rebol-request--rebol--com] with "unsubscribe" in the
> subject, without the quotes.

--

Ryan Cole
Programmer Analyst
www.iesco-dms.com
707-468-5400

The contradiction so puzzling to the ordinary way
of thinking comes from the fact that we have to use
language to communicate our inner experience
which in its very nature transcends lingistics.
-D.T. Suzuki
```