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

Alex Herbert edited comment on NUMBERS-148 at 7/21/20, 8:53 PM:
----------------------------------------------------------------

Does the Simplex ever rescale at the end (i.e. to get the final solution) away 
from 1.0?

The functions will work for any non subnormal number to scale towards 1.0. With 
a sub-normal number you cannot scale it using an exponent update. All 
sub-normals have an exponent of -1023 although the number may be as small as 
2^-1074. So you should detect sub normals and handle them correctly when 
scaling. You can get the exponent using this function:

{code:java}
private static int getScale(double a) {
    final long bits = Double.doubleToRawLongBits(a);

    // Get the unbiased exponent
    int exp = (((int) (bits >>> 52)) & 0x7ff) - 1023;

    // Handle sub-normal numbers
    if (exp == Double.MIN_EXPONENT - 1) {
        // Special case for zero, return as nan/inf to indicate scaling is not 
possible
        if (bits == 0) {
            return Double.MAX_EXPONENT + 1;
        }
        // A sub-normal number has an exponent below -1022. The amount below
        // is defined by the number of shifts of the most significant bit in
        // the mantissa that is required to get a 1 at position 53 (i.e. as
        // if it were a normal number with assumed leading bit)
        final long mantissa = bits & 0xf_ffff_ffff_ffffL;
        exp -= Long.numberOfLeadingZeros(mantissa << 12);
    }
    return exp;
}
{code}

The function gets a scale to be used by {{Math.scalb}} to transform the input 
non-zero finite number to the range {{[1, 2)}}. Infinite, NaN or zero will 
return 1024 and so have no effect on the number when passed to {{Math.scalb}}.

This method is untested but based on the code in 
{{o.a.c.numbers.complex.Complex.getScale}}. I modified it here to work on 1 
input number whereas the {{Complex}} class handles two inputs (real and 
imaginary).





was (Author: alexherbert):
Does the Simplex ever rescale at the end (i.e. to get the final solution) away 
from 1.0?

The functions will work for any non subnormal number to scale towards 1.0. With 
a sub-normal number you cannot scale it using an exponent update. All 
sub-normals have an exponent of -1023 although the number may be as small as 
2^-1074. So you should detect sub normals and handle them correctly when 
scaling. You can get the exponent using this function:

{code:java}
private static int getScale(double a) {
    final long bits = Double.doubleToRawLongBits(a);

    // Get the unbiased exponent
    int exp = (((int) (bits >>> 52)) & 0x7ff) - 1023;

    // Handle sub-normal numbers
    if (exp == Double.MIN_EXPONENT - 1) {
        // Special case for zero, return as nan/inf to indicate scaling is not 
possible
        if (bits == 0) {
            return Double.MAX_EXPONENT + 1;
        }
        // A sub-normal number has an exponent below -1022. The amount below
        // is defined by the number of shifts of the most significant bit in
        // the mantissa that is required to get a 1 at position 53 (i.e. as
        // if it were a normal number with assumed leading bit)
        final long mantissa = bits & 0xf_ffff_ffff_ffff;
        exp -= Long.numberOfLeadingZeros(mantissa << 12);
    }
    return exp;
}
{code}

The function gets a scale to be used by {{Math.scalb}} to transform the input 
non-zero finite number to the range {{[1, 2)}}. Infinite, NaN or zero will 
return 1024 and so have no effect on the number when passed to {{Math.scalb}}.

This method is untested but based on the code in 
{{o.a.c.numbers.complex.Complex.getScale}}. I modified it here to work on 1 
input number whereas the {{Complex}} class handles two inputs (real and 
imaginary).




> 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: 20m
>  Remaining Estimate: 0h
>




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

Reply via email to