how does Java convert floats to strings

后端 未结 3 784
广开言路
广开言路 2021-01-03 04:11

Here\'s what I\'m looking at:

float p=1.15f;
BigDecimal bdp=new BigDecimal(p);
float q=1.1499999f;
float r=1.14999999f;

System.out.println(p);   //1.15
Syst         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-03 04:52

    Thanks mttdbrd ... that helps. From your first paragraph then I think that the answer to my question is: yes, Java does this rounding internally in accordance with the IEEE spec.

    Here's the output of a program that let's me see a little bit of that rounding in action:

    -------------------------------------------
    string            1.49999
    bigdec of float   1.499989986419677734375
    float back to str 1.49999
    -------------------------------------------
    string            1.4999991
    bigdec of float   1.49999904632568359375
    float back to str 1.499999
    -------------------------------------------
    string            1.4999992
    bigdec of float   1.49999916553497314453125
    float back to str 1.4999992
    -------------------------------------------
    string            1.4999993
    bigdec of float   1.4999992847442626953125
    float back to str 1.4999993
    -------------------------------------------
    string            1.4999994
    bigdec of float   1.49999940395355224609375
    float back to str 1.4999994
    -------------------------------------------
    string            1.4999995
    bigdec of float   1.499999523162841796875
    float back to str 1.4999995
    -------------------------------------------
    string            1.4999996
    bigdec of float   1.49999964237213134765625
    float back to str 1.4999996
    -------------------------------------------
    string            1.4999997
    bigdec of float   1.49999964237213134765625
    float back to str 1.4999996
    -------------------------------------------
    string            1.4999998
    bigdec of float   1.4999997615814208984375
    float back to str 1.4999998
    -------------------------------------------
    string            1.4999999
    bigdec of float   1.49999988079071044921875
    float back to str 1.4999999
    -------------------------------------------
    string            1.15
    bigdec of float   1.14999997615814208984375
    float back to str 1.15
    -------------------------------------------
    string            1.49999964237213134765626
    bigdec of float   1.49999964237213134765625
    float back to str 1.4999996
    

    And here's the program if anyone wants it:

    public static void floatAccuracy3()
    {
        printFloatAndBigDec("1.49999");
        for (int i = 1; i < 10; i++) {
            String s="1.499999";
            s+=i;
            printFloatAndBigDec(s);         
        }
        printFloatAndBigDec("1.15");
        printFloatAndBigDec("1.49999964237213134765626");
    }
    
    public static void printFloatAndBigDec(String s)
    {
        Float f=new Float(s);
        BigDecimal bdf=new BigDecimal(f);
        System.out.println("-------------------------------------------");
        System.out.println("string            "+s);
        System.out.println("bigdec of float   "+bdf);
        System.out.println("float back to str "+f);
    }
    

    And some other links in case they're helpful to anyone else researching this stuff:

    • floating point basics
    • princeton cs intro floating points
    • What Every Computer Scientist Should Know About Floating-Point Arithmetic
    • Why 0.1 Does Not Exist In Floating-Point
    • JLS 4.2.4 Floating-Point Operations
    • IEEE round to nearest

提交回复
热议问题