On 19 January 2011 18:28, Ted Dunning <[email protected]> wrote:
> No. Indeed not. And if it calls floor then it isn't likely to give the
> desired -0 result.
>
> How important is it to return -0 instead of +0 anyway?
It can mess up comparisons of Doubles:
public static void main(String z[]){
Double nz = -0.0;
Double n = -0.49;
System.out.println("nz.compare(n)
"+Double.compare(nz,n)+"\nDouble.compare(rint(nz),rint(n))
"+Double.compare(rint(nz),rint(n)));
}
produces
nz.compare(n) 1
Double.compare(rint(nz),rint(n)) -1
i.e.
nz > n yet rint(nz) < rint(n)
This is counter-intuitive.
> On Wed, Jan 19, 2011 at 10:17 AM, sebb <[email protected]> wrote:
>
>> On 19 January 2011 16:25, Ted Dunning <[email protected]> wrote:
>> > Slightly slower,
>>
>> Unfortunately, I suspect it is about twice as slow for this case,
>> because FastMath.ceil() actually calls FastMath.floor().
>>
>> Not ideal for FastMath !
>>
>> >
>> > 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 <[email protected]> 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: [email protected]
>> >> For additional commands, e-mail: [email protected]
>> >>
>> >>
>> >
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [email protected]
>> For additional commands, e-mail: [email protected]
>>
>>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]