You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sebb <se...@gmail.com> on 2013/03/26 16:08:42 UTC

Re: svn commit: r1461159 - /commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/FastMath.java

On 26 March 2013 14:56,  <lu...@apache.org> wrote:
> Author: luc
> Date: Tue Mar 26 14:56:01 2013
> New Revision: 1461159
>
> URL: http://svn.apache.org/r1461159
> Log:
> FastMath.abs() without branching for float and double primitive types.
>
> JIRA: MATH-954
>
> Modified:
>     commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/FastMath.java
>
> Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/FastMath.java
> URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/FastMath.java?rev=1461159&r1=1461158&r2=1461159&view=diff
> ==============================================================================
> --- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/FastMath.java (original)
> +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/FastMath.java Tue Mar 26 14:56:01 2013
> @@ -3011,10 +3011,7 @@ public class FastMath {
>       * @return abs(x)
>       */
>      public static float abs(final float x) {
> -        if ((Float.floatToRawIntBits(x) & Integer.MIN_VALUE) == 0) {
> -            return x;
> -        }
> -        return -x;
> +        return Float.intBitsToFloat(Integer.MAX_VALUE & Float.floatToRawIntBits(x));

It looks wrong using MAX_VALUE as a mask (even though the value is correct).
Is there a no better constant?

If not, it would be better to create one with appropriate Javadoc.

>      }
>
>      /**
> @@ -3023,10 +3020,7 @@ public class FastMath {
>       * @return abs(x)
>       */
>      public static double abs(double x) {
> -        if ((Double.doubleToRawLongBits(x) & Long.MIN_VALUE) == 0) {
> -            return x;
> -        }
> -        return -x;
> +        return Double.longBitsToDouble(Long.MAX_VALUE & Double.doubleToRawLongBits(x));
>      }
>
>      /**
>
>

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


Re: svn commit: r1461159 - /commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/FastMath.java

Posted by sebb <se...@gmail.com>.
On 26 March 2013 19:49, Luc Maisonobe <Lu...@free.fr> wrote:
> Le 26/03/2013 16:08, sebb a écrit :
>> On 26 March 2013 14:56,  <lu...@apache.org> wrote:
>>> Author: luc
>>> Date: Tue Mar 26 14:56:01 2013
>>> New Revision: 1461159
>>>
>>> URL: http://svn.apache.org/r1461159
>>> Log:
>>> FastMath.abs() without branching for float and double primitive types.
>>>
>>> JIRA: MATH-954
>>>
>>> Modified:
>>>     commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/FastMath.java
>>>
>>> Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/FastMath.java
>>> URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/FastMath.java?rev=1461159&r1=1461158&r2=1461159&view=diff
>>> ==============================================================================
>>> --- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/FastMath.java (original)
>>> +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/FastMath.java Tue Mar 26 14:56:01 2013
>>> @@ -3011,10 +3011,7 @@ public class FastMath {
>>>       * @return abs(x)
>>>       */
>>>      public static float abs(final float x) {
>>> -        if ((Float.floatToRawIntBits(x) & Integer.MIN_VALUE) == 0) {
>>> -            return x;
>>> -        }
>>> -        return -x;
>>> +        return Float.intBitsToFloat(Integer.MAX_VALUE & Float.floatToRawIntBits(x));
>>
>> It looks wrong using MAX_VALUE as a mask (even though the value is correct).
>> Is there a no better constant?
>>
>> If not, it would be better to create one with appropriate Javadoc.
>
> You are right.
> I have fixed that with new constants (see r1461283).

Thanks, that looks much better.

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


Re: svn commit: r1461159 - /commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/FastMath.java

Posted by Luc Maisonobe <Lu...@free.fr>.
Le 26/03/2013 16:08, sebb a écrit :
> On 26 March 2013 14:56,  <lu...@apache.org> wrote:
>> Author: luc
>> Date: Tue Mar 26 14:56:01 2013
>> New Revision: 1461159
>>
>> URL: http://svn.apache.org/r1461159
>> Log:
>> FastMath.abs() without branching for float and double primitive types.
>>
>> JIRA: MATH-954
>>
>> Modified:
>>     commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/FastMath.java
>>
>> Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/FastMath.java
>> URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/FastMath.java?rev=1461159&r1=1461158&r2=1461159&view=diff
>> ==============================================================================
>> --- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/FastMath.java (original)
>> +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/FastMath.java Tue Mar 26 14:56:01 2013
>> @@ -3011,10 +3011,7 @@ public class FastMath {
>>       * @return abs(x)
>>       */
>>      public static float abs(final float x) {
>> -        if ((Float.floatToRawIntBits(x) & Integer.MIN_VALUE) == 0) {
>> -            return x;
>> -        }
>> -        return -x;
>> +        return Float.intBitsToFloat(Integer.MAX_VALUE & Float.floatToRawIntBits(x));
> 
> It looks wrong using MAX_VALUE as a mask (even though the value is correct).
> Is there a no better constant?
> 
> If not, it would be better to create one with appropriate Javadoc.

You are right.
I have fixed that with new constants (see r1461283).

Thanks for the hint
Luc

> 
>>      }
>>
>>      /**
>> @@ -3023,10 +3020,7 @@ public class FastMath {
>>       * @return abs(x)
>>       */
>>      public static double abs(double x) {
>> -        if ((Double.doubleToRawLongBits(x) & Long.MIN_VALUE) == 0) {
>> -            return x;
>> -        }
>> -        return -x;
>> +        return Double.longBitsToDouble(Long.MAX_VALUE & Double.doubleToRawLongBits(x));
>>      }
>>
>>      /**
>>
>>
> 
> ---------------------------------------------------------------------
> 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