float和double的不同值 [英] different values for float and double

查看:141
本文介绍了float和double的不同值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么浮点值与double值不同。从下面的示例可以看出,浮点数为同一操作提供的结果与double不同:

I don't understand why are float values different from double values. From the example bellow it appears that float provides different result than double for the same operation:

public class Test {

    public static void main(String[] args) {
        double a = 99999.8d;
        double b = 99999.65d;
        System.out.println(a + b);

        float a2 = 99999.8f;
        float b2 = 99999.65f;
        System.out.println(a2 + b2);
    }
}

输出:

199999.45
199999.44

你能解释一下浮点数和双精度之间的区别吗?

Can you explain what makes this difference between float and double?

推荐答案

浮点数是32位IEEE 754浮点数。

A float is a 32 bit IEEE 754 floating point.

双精度是64位IEEE 754浮点数。

A double is a 64 bit IEEE 754 floating point.

所以这只是一个精度问题,因为它们都没有小数部分.8和.65具有终止二进制表示,因此存在一些舍入误差。 double具有更高的精度,因此它的舍入误差略小。

so it is just a matter of precision because neither of the fraction portions .8 and .65 have a terminating binary representation, so there is some rounding error. the double has more precision so it has slightly less rounding error.

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

这篇关于float和double的不同值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆