You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Carlos Rego (JIRA)" <ji...@apache.org> on 2011/04/13 11:05:07 UTC

[jira] [Created] (LANG-693) Method createNumber from NumberUtils doesn't work for floating point numbers other than Float

Method createNumber from NumberUtils doesn't work for floating point numbers other than Float
---------------------------------------------------------------------------------------------

                 Key: LANG-693
                 URL: https://issues.apache.org/jira/browse/LANG-693
             Project: Commons Lang
          Issue Type: Bug
          Components: lang.math.*
    Affects Versions: 2.6
            Reporter: Carlos Rego
            Priority: Minor


Method createNumber from NumberUtils is trying to parse a string with a floating point number always first as a Float, that will cause that if we send a string with a number that will need a Double or even a BigDecimal the number will be truncate to accommodate into the Float without an exception to be thrown, so in fact we will no be returning ever neither a Double nor a BigDecimal.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (LANG-693) Method createNumber from NumberUtils doesn't work for floating point numbers other than Float

Posted by "Carlos Rego (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-693?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13025634#comment-13025634 ] 

Carlos Rego commented on LANG-693:
----------------------------------

In an automatic conversion to string of a number you will not have the qualifier, so if you try to undo the conversion with this method it will fail, because the approach when you don't sent the qualifier is not working. Anyway, we are working with BigDecimal instances that don't have a qualifier.

If you don't want a whole refactor of the method you could at least check if the resulted number has the same toString value than the parameter and if not try with a larger type, that should avoid this kind of problem (not very nice approach anyway).

> Method createNumber from NumberUtils doesn't work for floating point numbers other than Float
> ---------------------------------------------------------------------------------------------
>
>                 Key: LANG-693
>                 URL: https://issues.apache.org/jira/browse/LANG-693
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.math.*
>    Affects Versions: 2.6
>            Reporter: Carlos Rego
>            Priority: Minor
>
> Method createNumber from NumberUtils is trying to parse a string with a floating point number always first as a Float, that will cause that if we send a string with a number that will need a Double or even a BigDecimal the number will be truncate to accommodate into the Float without an exception to be thrown, so in fact we will no be returning ever neither a Double nor a BigDecimal.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (LANG-693) Method createNumber from NumberUtils doesn't work for floating point numbers other than Float

Posted by "Sebb (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-693?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13495060#comment-13495060 ] 

Sebb commented on LANG-693:
---------------------------

bq. So this proposal is not b-compatible ...

I assume you must mean behaviour compatible rather than binary compatible.

As far as I can tell, if the code is changed to try double and then float, the result will always be a double.
I'm not sure that is much of an improvement.

                
> Method createNumber from NumberUtils doesn't work for floating point numbers other than Float
> ---------------------------------------------------------------------------------------------
>
>                 Key: LANG-693
>                 URL: https://issues.apache.org/jira/browse/LANG-693
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.math.*
>    Affects Versions: 2.6
>            Reporter: Carlos Rego
>            Priority: Minor
>             Fix For: 3.x
>
>
> Method createNumber from NumberUtils is trying to parse a string with a floating point number always first as a Float, that will cause that if we send a string with a number that will need a Double or even a BigDecimal the number will be truncate to accommodate into the Float without an exception to be thrown, so in fact we will no be returning ever neither a Double nor a BigDecimal.

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (LANG-693) Method createNumber from NumberUtils doesn't work for floating point numbers other than Float

Posted by "Carlos Rego (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-693?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13025113#comment-13025113 ] 

Carlos Rego commented on LANG-693:
----------------------------------

We've have tested it in a JUnit with the following string: "0.9090909090909091", that debugging it returns a float with the value 0.90909094, which clearly is wrong. That's why we thought the approach for the conversion is wrong.
Did you tried to debug it just to be sure that the execution flow is going as you expect?

> Method createNumber from NumberUtils doesn't work for floating point numbers other than Float
> ---------------------------------------------------------------------------------------------
>
>                 Key: LANG-693
>                 URL: https://issues.apache.org/jira/browse/LANG-693
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.math.*
>    Affects Versions: 2.6
>            Reporter: Carlos Rego
>            Priority: Minor
>
> Method createNumber from NumberUtils is trying to parse a string with a floating point number always first as a Float, that will cause that if we send a string with a number that will need a Double or even a BigDecimal the number will be truncate to accommodate into the Float without an exception to be thrown, so in fact we will no be returning ever neither a Double nor a BigDecimal.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (LANG-693) Method createNumber from NumberUtils doesn't work for floating point numbers other than Float

Posted by "Sebb (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-693?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13495424#comment-13495424 ] 

Sebb commented on LANG-693:
---------------------------

Examples of valid numbers that would trigger an upscale:

Float.MAX_VALUE is 3.4028235e+38 - i.e. 8 digits; that would convert to Double
Double.MAX_VALUE is 1.7976931348623157e+308 - i.e. 17 digits; would convert to BigDecimal

In theory one could do extra work to handle the boundary cases.
I've not looked at what is involved.
                
> Method createNumber from NumberUtils doesn't work for floating point numbers other than Float
> ---------------------------------------------------------------------------------------------
>
>                 Key: LANG-693
>                 URL: https://issues.apache.org/jira/browse/LANG-693
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.math.*
>    Affects Versions: 2.6
>            Reporter: Carlos Rego
>            Priority: Minor
>             Fix For: 3.x
>
>
> Method createNumber from NumberUtils is trying to parse a string with a floating point number always first as a Float, that will cause that if we send a string with a number that will need a Double or even a BigDecimal the number will be truncate to accommodate into the Float without an exception to be thrown, so in fact we will no be returning ever neither a Double nor a BigDecimal.

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (LANG-693) Method createNumber from NumberUtils doesn't work for floating point numbers other than Float

Posted by "Sebb (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-693?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13495379#comment-13495379 ] 

Sebb commented on LANG-693:
---------------------------

As pointed out in the original issue description, the same problem applies to double/BigDecimal.
If the code is changed to try double first, then the method will never create a BigDecimal unless it is larger than Double.MAX_VALUE, thus potentially losing precision again.

As I see it, the problem is that the method currently only switches output type based on magnitude, not on precision.

Would it make sense to check the precision of the input number and use that to choose the starting conversion?

e.g. if more than 6 digits are present, use double; if more than 17 digits, use BigDecimal.

This should be fairly cheap to do, and it would be easy to determine in advance what number type would be created.

It would not always choose the minimal representation, but at least it would never lose precision.


[AFAICT:

6 is the max number of digits in a number guaranteed not to lose precision (some 7/8 digit numbers are also OK).
Similarly, double can support 15 digits guaranteed (to 17 max).]
                
> Method createNumber from NumberUtils doesn't work for floating point numbers other than Float
> ---------------------------------------------------------------------------------------------
>
>                 Key: LANG-693
>                 URL: https://issues.apache.org/jira/browse/LANG-693
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.math.*
>    Affects Versions: 2.6
>            Reporter: Carlos Rego
>            Priority: Minor
>             Fix For: 3.x
>
>
> Method createNumber from NumberUtils is trying to parse a string with a floating point number always first as a Float, that will cause that if we send a string with a number that will need a Double or even a BigDecimal the number will be truncate to accommodate into the Float without an exception to be thrown, so in fact we will no be returning ever neither a Double nor a BigDecimal.

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Updated] (LANG-693) Method createNumber from NumberUtils doesn't work for floating point numbers other than Float

Posted by "Henri Yandell (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/LANG-693?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Henri Yandell updated LANG-693:
-------------------------------

    Fix Version/s: 3.x

Setting fix version to 3.x (i.e. post 3.0). Seems like a very fair issue to take a stab at fixing.

> Method createNumber from NumberUtils doesn't work for floating point numbers other than Float
> ---------------------------------------------------------------------------------------------
>
>                 Key: LANG-693
>                 URL: https://issues.apache.org/jira/browse/LANG-693
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.math.*
>    Affects Versions: 2.6
>            Reporter: Carlos Rego
>            Priority: Minor
>             Fix For: 3.x
>
>
> Method createNumber from NumberUtils is trying to parse a string with a floating point number always first as a Float, that will cause that if we send a string with a number that will need a Double or even a BigDecimal the number will be truncate to accommodate into the Float without an exception to be thrown, so in fact we will no be returning ever neither a Double nor a BigDecimal.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (LANG-693) Method createNumber from NumberUtils doesn't work for floating point numbers other than Float

Posted by "Sebb (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-693?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13025161#comment-13025161 ] 

Sebb commented on LANG-693:
---------------------------

I can confirm that "0.9090909090909091" is returned as a float 0.90909094.

Whether this is wrong is a different matter. The Javadoc says:

{quote}
Turns a string value into a java.lang.Number.

First, the value is examined for a type qualifier on the end ('f','F','d','D','l','L'). If it is found, it starts trying to create successively larger types from the type specified until one is found that can represent the value.

If a type specifier is not found, it will check for a decimal point and then try successively larger types from Integer to BigInteger and from Float to BigDecimal.
{quote}

> Method createNumber from NumberUtils doesn't work for floating point numbers other than Float
> ---------------------------------------------------------------------------------------------
>
>                 Key: LANG-693
>                 URL: https://issues.apache.org/jira/browse/LANG-693
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.math.*
>    Affects Versions: 2.6
>            Reporter: Carlos Rego
>            Priority: Minor
>
> Method createNumber from NumberUtils is trying to parse a string with a floating point number always first as a Float, that will cause that if we send a string with a number that will need a Double or even a BigDecimal the number will be truncate to accommodate into the Float without an exception to be thrown, so in fact we will no be returning ever neither a Double nor a BigDecimal.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (LANG-693) Method createNumber from NumberUtils doesn't work for floating point numbers other than Float

Posted by "Thomas Neidhart (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-693?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13495135#comment-13495135 ] 

Thomas Neidhart commented on LANG-693:
--------------------------------------

Maybe I was unclear, but the float part afterwards would not be needed anymore, as this would never succeed.

Another idea would be to add something like this (pseudo-code)

{noformat}
//Must be a float,double,BigDec
boolean allZeros = isAllZeros(mant) && isAllZeros(exp);
try {
    Float f = createFloat(str);
    BigDecimal one = createBigDecimal(str);
    BigDecimal two = new BigDecimal(f);

    // check for loss of precision in the conversion
    if (one.compareTo(two) == 0) {
       ....

} catch (NumberFormatException nfe) { // NOPMD
    // ignore the bad number
}
{noformat}

Maybe the getPrecision() method from BigDecimal could be used too.
It is also a problem of definition, as a floating-point number can not always precisely expressed in the related data types, so the question is how to define when a float will be returned, and when a double.

That's why I put my original proposal to just assume a double in case of a missing type qualifier, as this is the default in java anyway.
                
> Method createNumber from NumberUtils doesn't work for floating point numbers other than Float
> ---------------------------------------------------------------------------------------------
>
>                 Key: LANG-693
>                 URL: https://issues.apache.org/jira/browse/LANG-693
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.math.*
>    Affects Versions: 2.6
>            Reporter: Carlos Rego
>            Priority: Minor
>             Fix For: 3.x
>
>
> Method createNumber from NumberUtils is trying to parse a string with a floating point number always first as a Float, that will cause that if we send a string with a number that will need a Double or even a BigDecimal the number will be truncate to accommodate into the Float without an exception to be thrown, so in fact we will no be returning ever neither a Double nor a BigDecimal.

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (LANG-693) Method createNumber from NumberUtils doesn't work for floating point numbers other than Float

Posted by "Gary Gregory (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-693?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13495401#comment-13495401 ] 

Gary Gregory commented on LANG-693:
-----------------------------------

I like preserving precision.
                
> Method createNumber from NumberUtils doesn't work for floating point numbers other than Float
> ---------------------------------------------------------------------------------------------
>
>                 Key: LANG-693
>                 URL: https://issues.apache.org/jira/browse/LANG-693
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.math.*
>    Affects Versions: 2.6
>            Reporter: Carlos Rego
>            Priority: Minor
>             Fix For: 3.x
>
>
> Method createNumber from NumberUtils is trying to parse a string with a floating point number always first as a Float, that will cause that if we send a string with a number that will need a Double or even a BigDecimal the number will be truncate to accommodate into the Float without an exception to be thrown, so in fact we will no be returning ever neither a Double nor a BigDecimal.

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Issue Comment Edited] (LANG-693) Method createNumber from NumberUtils doesn't work for floating point numbers other than Float

Posted by "Sebb (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-693?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13025161#comment-13025161 ] 

Sebb edited comment on LANG-693 at 4/26/11 10:29 AM:
-----------------------------------------------------

I can confirm that "0.9090909090909091" is returned as a float 0.90909094.

Whether this is wrong is a different matter. The Javadoc says:

{quote}
Turns a string value into a java.lang.Number.

First, the value is examined for a type qualifier on the end ('f','F','d','D','l','L'). If it is found, it starts trying to create successively larger types from the type specified until one is found that can represent the value.

If a type specifier is not found, it will check for a decimal point and then try successively larger types from Integer to BigInteger and from Float to BigDecimal.
{quote}

The second paragraph does not specifically say that the code will return the "best" match, nor does the 3rd para. It's not clear to me whether the method was ever intended to return a "best" match (for some definintion of best). If such behaviour is intended, then the Javadoc needs to be clarified, and the code changed accordingly.

Note that the test of Double.MAX_VALUE works because this is converted to Float.INFINITY.
Non-infinite values are rounded by Float.valueOf(String) to the nearest value, so are never checked as doubles.

      was (Author: sebb@apache.org):
    I can confirm that "0.9090909090909091" is returned as a float 0.90909094.

Whether this is wrong is a different matter. The Javadoc says:

{quote}
Turns a string value into a java.lang.Number.

First, the value is examined for a type qualifier on the end ('f','F','d','D','l','L'). If it is found, it starts trying to create successively larger types from the type specified until one is found that can represent the value.

If a type specifier is not found, it will check for a decimal point and then try successively larger types from Integer to BigInteger and from Float to BigDecimal.
{quote}
  
> Method createNumber from NumberUtils doesn't work for floating point numbers other than Float
> ---------------------------------------------------------------------------------------------
>
>                 Key: LANG-693
>                 URL: https://issues.apache.org/jira/browse/LANG-693
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.math.*
>    Affects Versions: 2.6
>            Reporter: Carlos Rego
>            Priority: Minor
>
> Method createNumber from NumberUtils is trying to parse a string with a floating point number always first as a Float, that will cause that if we send a string with a number that will need a Double or even a BigDecimal the number will be truncate to accommodate into the Float without an exception to be thrown, so in fact we will no be returning ever neither a Double nor a BigDecimal.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Updated] (LANG-693) Method createNumber from NumberUtils doesn't work for floating point numbers other than Float

Posted by "Henri Yandell (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/LANG-693?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Henri Yandell updated LANG-693:
-------------------------------

    Fix Version/s:     (was: 3.0.2)
                   3.0.3

Pushing back to 3.0.3; I don't think is ready yet as the toString approach is likely to lead to bugs and there's not another solution on the table.

> Method createNumber from NumberUtils doesn't work for floating point numbers other than Float
> ---------------------------------------------------------------------------------------------
>
>                 Key: LANG-693
>                 URL: https://issues.apache.org/jira/browse/LANG-693
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.math.*
>    Affects Versions: 2.6
>            Reporter: Carlos Rego
>            Priority: Minor
>             Fix For: 3.0.3
>
>
> Method createNumber from NumberUtils is trying to parse a string with a floating point number always first as a Float, that will cause that if we send a string with a number that will need a Double or even a BigDecimal the number will be truncate to accommodate into the Float without an exception to be thrown, so in fact we will no be returning ever neither a Double nor a BigDecimal.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

[jira] [Updated] (LANG-693) Method createNumber from NumberUtils doesn't work for floating point numbers other than Float

Posted by "Henri Yandell (Updated) (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/LANG-693?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Henri Yandell updated LANG-693:
-------------------------------

    Fix Version/s:     (was: 3.2)
                   3.x
    
> Method createNumber from NumberUtils doesn't work for floating point numbers other than Float
> ---------------------------------------------------------------------------------------------
>
>                 Key: LANG-693
>                 URL: https://issues.apache.org/jira/browse/LANG-693
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.math.*
>    Affects Versions: 2.6
>            Reporter: Carlos Rego
>            Priority: Minor
>             Fix For: 3.x
>
>
> Method createNumber from NumberUtils is trying to parse a string with a floating point number always first as a Float, that will cause that if we send a string with a number that will need a Double or even a BigDecimal the number will be truncate to accommodate into the Float without an exception to be thrown, so in fact we will no be returning ever neither a Double nor a BigDecimal.

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

[jira] [Commented] (LANG-693) Method createNumber from NumberUtils doesn't work for floating point numbers other than Float

Posted by "Thomas Neidhart (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-693?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13495412#comment-13495412 ] 

Thomas Neidhart commented on LANG-693:
--------------------------------------

sounds pretty reasonable.
                
> Method createNumber from NumberUtils doesn't work for floating point numbers other than Float
> ---------------------------------------------------------------------------------------------
>
>                 Key: LANG-693
>                 URL: https://issues.apache.org/jira/browse/LANG-693
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.math.*
>    Affects Versions: 2.6
>            Reporter: Carlos Rego
>            Priority: Minor
>             Fix For: 3.x
>
>
> Method createNumber from NumberUtils is trying to parse a string with a floating point number always first as a Float, that will cause that if we send a string with a number that will need a Double or even a BigDecimal the number will be truncate to accommodate into the Float without an exception to be thrown, so in fact we will no be returning ever neither a Double nor a BigDecimal.

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Updated] (LANG-693) Method createNumber from NumberUtils doesn't work for floating point numbers other than Float

Posted by "Henri Yandell (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/LANG-693?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Henri Yandell updated LANG-693:
-------------------------------

    Fix Version/s:     (was: 3.x)
                   3.0.2

> Method createNumber from NumberUtils doesn't work for floating point numbers other than Float
> ---------------------------------------------------------------------------------------------
>
>                 Key: LANG-693
>                 URL: https://issues.apache.org/jira/browse/LANG-693
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.math.*
>    Affects Versions: 2.6
>            Reporter: Carlos Rego
>            Priority: Minor
>             Fix For: 3.0.2
>
>
> Method createNumber from NumberUtils is trying to parse a string with a floating point number always first as a Float, that will cause that if we send a string with a number that will need a Double or even a BigDecimal the number will be truncate to accommodate into the Float without an exception to be thrown, so in fact we will no be returning ever neither a Double nor a BigDecimal.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

[jira] [Commented] (LANG-693) Method createNumber from NumberUtils doesn't work for floating point numbers other than Float

Posted by "Henri Yandell (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-693?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13021987#comment-13021987 ] 

Henri Yandell commented on LANG-693:
------------------------------------

Can you provide more data? If I run the following test it looks to happily be returning Doubles (and already had a test for BigDecimal):

        // LANG-693
        assertEquals("createNumber(String) LANG-693 failed", Double.MAX_VALUE, NumberUtils
                    .createNumber("" + Double.MAX_VALUE));
        System.err.println( NumberUtils.createNumber(""+Double.MAX_VALUE));
        System.err.println( NumberUtils.createNumber(""+Double.MAX_VALUE).getClass());   // prints java.lang.Double

> Method createNumber from NumberUtils doesn't work for floating point numbers other than Float
> ---------------------------------------------------------------------------------------------
>
>                 Key: LANG-693
>                 URL: https://issues.apache.org/jira/browse/LANG-693
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.math.*
>    Affects Versions: 2.6
>            Reporter: Carlos Rego
>            Priority: Minor
>
> Method createNumber from NumberUtils is trying to parse a string with a floating point number always first as a Float, that will cause that if we send a string with a number that will need a Double or even a BigDecimal the number will be truncate to accommodate into the Float without an exception to be thrown, so in fact we will no be returning ever neither a Double nor a BigDecimal.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (LANG-693) Method createNumber from NumberUtils doesn't work for floating point numbers other than Float

Posted by "Gary Gregory (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-693?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13494894#comment-13494894 ] 

Gary Gregory commented on LANG-693:
-----------------------------------

So this proposal is not b-compatible but it sounds like an improvement, or even a bug fix if you consider the loss of precision. +1.
                
> Method createNumber from NumberUtils doesn't work for floating point numbers other than Float
> ---------------------------------------------------------------------------------------------
>
>                 Key: LANG-693
>                 URL: https://issues.apache.org/jira/browse/LANG-693
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.math.*
>    Affects Versions: 2.6
>            Reporter: Carlos Rego
>            Priority: Minor
>             Fix For: 3.x
>
>
> Method createNumber from NumberUtils is trying to parse a string with a floating point number always first as a Float, that will cause that if we send a string with a number that will need a Double or even a BigDecimal the number will be truncate to accommodate into the Float without an exception to be thrown, so in fact we will no be returning ever neither a Double nor a BigDecimal.

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (LANG-693) Method createNumber from NumberUtils doesn't work for floating point numbers other than Float

Posted by "Thomas Neidhart (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-693?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13494866#comment-13494866 ] 

Thomas Neidhart commented on LANG-693:
--------------------------------------

Why not trying to create a double first in the createNumber method:

{noformat}
                //Must be a float,double,BigDec
                boolean allZeros = isAllZeros(mant) && isAllZeros(exp);
                try {
                    Float f = createFloat(str);
                    if (!(f.isInfinite() || (f.floatValue() == 0.0F && !allZeros))) {
                        return f;
                    }
                } catch (NumberFormatException nfe) { // NOPMD
                    // ignore the bad number
                }
                try {
                    Double d = createDouble(str);
                    if (!(d.isInfinite() || (d.doubleValue() == 0.0D && !allZeros))) {
                        return d;
                    }
                } catch (NumberFormatException nfe) { // NOPMD
                    // ignore the bad number
                }
{noformat}

Right now, we first try to convert it to a float, and if it does not fit, we try it with double.
But this way we will always lose the precision unless one specifies the 'd' suffix.

The default in java for decimal values is double anyway (without specifying a type qualifier), so it makes sense to use the same behavior in the createNumber method imho.

If somebody really wants to have a float, he/she can specify the 'f' type qualifier.
                
> Method createNumber from NumberUtils doesn't work for floating point numbers other than Float
> ---------------------------------------------------------------------------------------------
>
>                 Key: LANG-693
>                 URL: https://issues.apache.org/jira/browse/LANG-693
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.math.*
>    Affects Versions: 2.6
>            Reporter: Carlos Rego
>            Priority: Minor
>             Fix For: 3.x
>
>
> Method createNumber from NumberUtils is trying to parse a string with a floating point number always first as a Float, that will cause that if we send a string with a number that will need a Double or even a BigDecimal the number will be truncate to accommodate into the Float without an exception to be thrown, so in fact we will no be returning ever neither a Double nor a BigDecimal.

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (LANG-693) Method createNumber from NumberUtils doesn't work for floating point numbers other than Float

Posted by "Henri Yandell (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-693?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13089318#comment-13089318 ] 

Henri Yandell commented on LANG-693:
------------------------------------

The toString may not be the nicest, but it definitely works :)

The only test I get failing if I use it is the LANG-521 "2." use case. That switches from being a Float to being a BigDecimal. I suspect there might be problems with exponents though.

All the problems would seem to be creating BigDecimals unnecessarily, rather than creating Floats and losing data. So worst case the String test seems preferable. 


> Method createNumber from NumberUtils doesn't work for floating point numbers other than Float
> ---------------------------------------------------------------------------------------------
>
>                 Key: LANG-693
>                 URL: https://issues.apache.org/jira/browse/LANG-693
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.math.*
>    Affects Versions: 2.6
>            Reporter: Carlos Rego
>            Priority: Minor
>             Fix For: 3.0.2
>
>
> Method createNumber from NumberUtils is trying to parse a string with a floating point number always first as a Float, that will cause that if we send a string with a number that will need a Double or even a BigDecimal the number will be truncate to accommodate into the Float without an exception to be thrown, so in fact we will no be returning ever neither a Double nor a BigDecimal.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira