You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Thomas Neidhart <th...@gmail.com> on 2014/01/12 14:57:38 UTC

[math] Rounding of negative zero

Hi,

in MATH-1070 we have discovered that calling Precision.round(x, y)
behaves differently for float and double values when the value to be
rounded is negative zero:

Precision.round(-0.0d, 0) = 0.0
Precision.round(-0.0f, 0) = -0.0

The reason being that in the double case, the value is converted to a
BigDecimal, which loses the sign in this case, see
http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#BigDecimal%28java.lang.String%29.

I could not find anything related to the IEEE standard for rounding
numbers for this case. Looking at octave and python, there the sign is
kept and this would also be a reasonable behavior imho.

Thus I would propose to add a special case for this to the
Precision.round method.

Thomas

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [math] Rounding of negative zero

Posted by Phil Steitz <ph...@gmail.com>.
On 1/12/14, 6:10 AM, Luc Maisonobe wrote:
> Le 12/01/2014 14:57, Thomas Neidhart a écrit :
>> Hi,
>>
>> in MATH-1070 we have discovered that calling Precision.round(x, y)
>> behaves differently for float and double values when the value to be
>> rounded is negative zero:
>>
>> Precision.round(-0.0d, 0) = 0.0
>> Precision.round(-0.0f, 0) = -0.0
>>
>> The reason being that in the double case, the value is converted to a
>> BigDecimal, which loses the sign in this case, see
>> http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#BigDecimal%28java.lang.String%29.
>>
>> I could not find anything related to the IEEE standard for rounding
>> numbers for this case. Looking at octave and python, there the sign is
>> kept and this would also be a reasonable behavior imho.
>>
>> Thus I would propose to add a special case for this to the
>> Precision.round method.
> This sounds reasonable to me.

+1

Phil
>
> Luc
>
>> Thomas
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
>> For additional commands, e-mail: dev-help@commons.apache.org
>>
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [math] Rounding of negative zero

Posted by Luc Maisonobe <lu...@spaceroots.org>.
Le 12/01/2014 14:57, Thomas Neidhart a écrit :
> Hi,
> 
> in MATH-1070 we have discovered that calling Precision.round(x, y)
> behaves differently for float and double values when the value to be
> rounded is negative zero:
> 
> Precision.round(-0.0d, 0) = 0.0
> Precision.round(-0.0f, 0) = -0.0
> 
> The reason being that in the double case, the value is converted to a
> BigDecimal, which loses the sign in this case, see
> http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#BigDecimal%28java.lang.String%29.
> 
> I could not find anything related to the IEEE standard for rounding
> numbers for this case. Looking at octave and python, there the sign is
> kept and this would also be a reasonable behavior imho.
> 
> Thus I would propose to add a special case for this to the
> Precision.round method.

This sounds reasonable to me.

Luc

> 
> Thomas
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
> 
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org