commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ted Dunning <ted.dunn...@gmail.com>
Subject Re: [MATH] generating -0.0 rather than 0.0 when incrementing a negative number
Date Wed, 19 Jan 2011 16:25:44 GMT
Slightly slower,

    double y = floor(x):
    double d = x - y;
    if (d > 0.5) {
         return ceil(x);
    } else {
         return y;
    }

but it gives your desired result with little (visible) special casing.

On Wed, Jan 19, 2011 at 8:04 AM, sebb <sebbaz@gmail.com> wrote:

> FastMath.rint(x) has the following code:
>
>        double y = floor(x);
>        double d = x - y;
>
>        if (d > 0.5) {
>            return y+1.0; // round up
>        }
>  ...
>
> For -0.5 < x < 0 the rounding up generates +0.0  - rather than -0.0,
> as expected by the sign of the input parameter.
>
> Is there an easy way to fix this, other than explicitly checking for y ==
> -1.0?
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message