Java浮点基元是否有IEEE 754标准实现? [英] Is there any IEEE 754 standard implementations for Java floating point primitives?

查看:52
本文介绍了Java浮点基元是否有IEEE 754标准实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Java是否使用IEEE 754标准来实现其浮点运算感兴趣. 此处我在文档中看到了这种情况:

IEEE 754-2008中定义的操作

据我所知,IEEE 754的积极方面是提高浮点运算的精度,因此,如果我在Java中使用doublefloat,计算的精度是否与BigDecimal中的相同?如果不是,那么在 Math 类?

解决方案

我对Java是否使用IEEE 754标准来实现其浮点运算感兴趣.

IEEE-754为多个浮点类型定义了标准.多年来,它们都是 binary 浮点数;这就是Java的floatdouble的含义:float是32位IEEE-754二进制浮点值(标准调用 binary64 ).这些二进制浮点数对于计算机而言非常有效,但是由于它们以二进制形式工作,而我们以十进制形式工作,因此存在一些期望值不匹配的情况.例如,0.1不能精确地存储在double中,并且会出现类似0.1 + 0.2的奇怪现象,结果是0.30000000000000004.有关详细信息,请参见 浮点数学运算是否已损坏? .例如,它们不是进行财务计算的好选择.

BigDecimal是一个Java类,它以任意精度实现 decimal 小数.它比使用double慢得多,但结果符合我们十进制的期望(例如,0.1 + 0.2将为0.3).

IEEE-754的2008年版添加了重要的新格式,尤其是 decimal32 decimal64 Wikipedia上的IEEE-754

  • BigDecimal JavaDoc
  • I'm interested if Java is using IEEE 754 standard for implementing its floating point arithmetic. Here I saw this kind of thing in documentation:

    operation defined in IEEE 754-2008

    As I understand positive side of IEEE 754 is to increase precision of floating point arithmetics so if I'll use double or float in Java would presision of computations be same as in BigDecimal? And if not than what's the point of using IEEE 754 standard in Math class?

    解决方案

    I'm interested if Java is using IEEE 754 standard for implementing it's floating point arithmetic.

    IEEE-754 defines standards for multiple floating-point types. For many years, they were all binary floating point; that's what Java's float and double are: float is a 32-bit IEEE-754 binary floating point value (what the standard calls binary32). double is a 64-bit one (what the standard calls binary64). These binary floating point numbers are very efficient for computers to calculate, but because they work in binary and we work in decimal, there are some expectation mismatches; for instance, 0.1 cannot be stored precisely in a double, and you get oddities like 0.1 + 0.2 turning out to be 0.30000000000000004. See Is floating point math broken? for details. They're not a good choice for financial calculations, for instance.

    BigDecimal is a Java class that implements decimal fractional numbers with arbitrary precision. It's much slower than using double, but the results fit with our decimal-minded expectations (for instance, that 0.1 + 0.2 will be 0.3).

    The 2008 edition of IEEE-754 adds significant new formats, in particular decimal32, decimal64, and decimal128. These are decimal floating point, and so they work the same way that we do. 0.1 can be accurately stored in a decimal64. 0.1 + 0.2 is 0.3 in decimal64. However, as far as I can tell, they aren't really relevant to your question.

    Since BigDecimal predates IEEE-754 2008 (by some margin), it defines its own semantics.

    And if not than what's the point of using IEEE 754 standard in Math class?

    JDK9 adds new operations to Math that do things defined by the IEEE-754 2008 spec (such as fma, which does a fused multiply-add), and so it defines those operations with reference to the IEEE-754 2008 spec, for clarity.

    More reading:

    这篇关于Java浮点基元是否有IEEE 754标准实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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