Hi all,

I was experimenting with rounding again and noticed this:

mod1: function [
    {Compute a non-negative remainder}
    a [number!]
    b [number!]
] [r] [
    either negative? r: a // b [r + abs b] [r]
]

mod2: func [
    {Compute a non-negative remainder}
    a [number!]
    b [number!]
] [
    a // b + (abs b) // b
]

round: func [
    {Round a number}
    n [number!]
    /to
    factor [number!] {the number a multiply of which to round to}
] [
    if not to [factor: 1]
    n: 0.5 * factor + n
    n - mod n factor
]

foreach f [mod1 mod2] [
    mod: get f
    print ["mod:" mold :mod]
    foreach num [-5e-3 -4e-3 -3e-3 -2e-3 -1e-3 0 1e-3 2e-3 3e-3 4e-3] [
        print ["num:" num "rounded:" round/to num 1e-2]
    ]
]

The results are:

mod: func [
    "Compute a non-negative remainder"
    a [number!]
    b [number!] /local r][
    either negative? r: a // b [r + abs b] [r]
]
num: -5E-3 rounded: 0
num: -4E-3 rounded: 0
num: -3E-3 rounded: 0
num: -2E-3 rounded: 0
num: -1E-3 rounded: 0
num: 0 rounded: 0
num: 1E-3 rounded: 0
num: 2E-3 rounded: 0
num: 3E-3 rounded: 0
num: 4E-3 rounded: 0
mod: func [
    "Compute a non-negative remainder"
    a [number!]
    b [number!]
][
    a // b + (abs b) // b
]
num: -5E-3 rounded: 0
num: -4E-3 rounded: 8.67361737988404E-19
num: -3E-3 rounded: 0
num: -2E-3 rounded: -8.67361737988404E-19
num: -1E-3 rounded: 0
num: 0 rounded: 8.67361737988404E-19
num: 1E-3 rounded: 0
num: 2E-3 rounded: -8.67361737988404E-19
num: 3E-3 rounded: -1.73472347597681E-18
num: 4E-3 rounded: -1.73472347597681E-18

Cheers
    L

-- 
To unsubscribe from this list, please send an email to
[EMAIL PROTECTED] with "unsubscribe" in the 
subject, without the quotes.

Reply via email to