[ 
https://issues.apache.org/jira/browse/NUMBERS-148?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17162320#comment-17162320
 ] 

Alex Herbert commented on NUMBERS-148:
--------------------------------------

The following functions are present in {{FastMath}}:
{code:java}
int getExponent(double);
int getExponent(float);
{code}
You have ported the double version but not the float version. Note that 
{{FastMath}} contains mostly the same functions as {{java.lang.Math}} which 
includes these two methods to get the exponent. So these are not really 
required.

You have also created new functions that are not present in Commons Math:
{code:java}
int getRawExponent(double);
double updateExponent(double d, int exp);
{code}
What is the use case for getRawExponent? The unbiased exponent is more of an 
implementation detail rather than a practical piece of information about your 
floating-point number.

The {{updateExponent}} method will not handle overflow to infinite or underflow 
to sub-normal or zero. Exponent manipulation requires very careful use of 
masking to extract the exponent and then to mask the updated exponent before 
recombination with the other bits. The later use of a mask before recombination 
you have not done meaning that your bits can rollover into the sign bit when 
scaling up (adding exponent) or into the mantissa when scaling down 
(subtracting exponent). For example this can create NaNs for overflow or zeros 
as shown below. Scaling is better left to {{java.lang.Math.scalb(double, int)}} 
and the equivalent for float. Although they do not work via direct bit 
manipulation their use of multiplication ensures the result is correct for 
over/underflow. Currently the method presented in the PR is buggy:
{code:java}
double d = 1.01;
d = Precision.updateExponent(d, 1024);

// d == NaN
// It should be +Infinity

d = 0x1.0p1023;
d = Precision.updateExponent(d, 2);
// d is -0.0
// It should be +Infinity
{code}
I would say that these functions are not needed.

> Extract double exponent manipulation routines from commons-math to numbers
> --------------------------------------------------------------------------
>
>                 Key: NUMBERS-148
>                 URL: https://issues.apache.org/jira/browse/NUMBERS-148
>             Project: Commons Numbers
>          Issue Type: Improvement
>          Components: core
>            Reporter: Mohammad Rezaei
>            Priority: Minor
>          Time Spent: 10m
>  Remaining Estimate: 0h
>




--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to