Java中的INFINITY常量是什么? [英] What are the INFINITY constants in Java, really?

查看:446
本文介绍了Java中的INFINITY常量是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚遇到原始类型包装类中的常量,如 Double.POSITIVE_INFINITY Double.NEGATIVE_INFINITY 。在API中,它将第一个定义为:

I just recently ran across the constants in the primitive type wrapper classes like Double.POSITIVE_INFINITY and Double.NEGATIVE_INFINITY. In the API, it defines the first as:


保持double类型的正无穷大的常量。它等于Double.longBitsToDouble(0x7ff0000000000000L)返回的值。

A constant holding the positive infinity of type double. It is equal to the value returned by Double.longBitsToDouble(0x7ff0000000000000L).

其他人在这些行上有定义。

The others have definitions along these same lines.

我遇到的问题是理解这些常量究竟是什么。它们实际上不能 代表正/负无穷大,因为系统本质上是有限的。它是Java创建者认为会定义无限概念的一些任意位置吗?或者这些确实具有某种特殊价值?如果 只是一个被解释为 double 的任意字符串,那么当它被解释为<$时,会有一些正常的数字c $ c> double 将返回 POSITIVE_INFINITY 而不是实际预期的值?

What I'm having trouble with is understanding what these constants actually are. They can't actually be or represent positive/negative infinities, because the system is by nature finite. Is it just some arbitrary setting of bits which the Java creators deemed would define the concept of infinity? Or do these actually have some kind of special value? If it is just an arbitrary string of bits interpreted as a double, then is there some normal number out there that, when interpreted as a double will return POSITIVE_INFINITY instead of whatever value is actually expected?

请原谅我,如果给出 Double.longBitsToDouble(0x7ff0000000000000L)部分API,答案就很明显了。说实话,这个描述对我来说是相当神秘的,我不会假装理解十六进制值实际意味着或代表什么。

Forgive me if the answer to this is obvious given the Double.longBitsToDouble(0x7ff0000000000000L) part of the API. Truthfully, that description is pretty arcane to me and I won't pretend to understand what the hexadecimal values actually mean or represent.

推荐答案

Java浮点基于IEEE 754二进制浮点标准浮点标准,其第一个版本已发布在大约1985年,所以它比Java更老。鉴于定义Java时IEEE 754的广泛硬件实现,Java创建者别无选择。

Java floating point is based on the IEEE 754 binary floating point standard Floating Point Standard, the first version of which was issued in about 1985, so it is much older than Java. Given widespread hardware implementation of IEEE 754 by the time Java was being defined, the Java creators had little choice.

每个IEEE 754浮点数有三个组成部分,一个符号位,指数和尾数。大大简化,正常数字的大小是:

Each IEEE 754 floating point number has three components, a sign bit, an exponent, and a mantissa. Simplifying considerably, the magnitude of a normal number is:

 mantissa * (2 ** exponent)

其中**代表电力。

领先位是标志位。在双精度数中,接下来的11位是指数。

The leading bit is the sign bit. In doubles, the next 11 bits are the exponent.

所有指数位的位模式都保留给无穷大和NaN。所有正常数字在指数中至少有一个零位。两个无穷大通过使所有指数位开启并且所有尾数位为零来表示。主要标志位区分正负无穷大。

The bit patterns with all the exponent bits on are reserved for infinities and NaNs. All normal numbers have at least one zero bit in the exponent. The two infinities are represented by having all exponent bits on, and all mantissa bits zero. The leading sign bit distinguishes positive and negative infinity.

特殊情况下所有指数位的选择不是任意的。切断一个极端比处理一系列数字中间的间隙更容易,特别是对于硬件实现。对特殊情况采用所有位指数将阻止编码为零,所有位都关闭模式,并且将给出最大的绝对幅度值,无穷大,最小指数,这也将使硬件更复杂。指数上的所有位绝对是无穷大的最佳选择。

The choice of all exponent bits one for the special cases is not arbitrary. It is easier to chop off one of the extremes than to deal with a gap in the middle of a range of numbers, especially for hardware implementations. Taking the all bits off exponent for special cases would have prevented encoding zero with the all bits off pattern, and would have given the largest absolute magnitude values, the infinities, the smallest exponent, which would have also made hardware more complicated. The all bits on exponent is definitely the best choice for the infinities.

两个无穷大都用于表示两个事物,实际上是无限结果和结果太大了在正常数字系统中表示的绝对量值,大于Double.MAX_VALUE或小于-Double.MAX_VALUE的数字。 1.0 / 0.0是无限的。所以是2 * Double.MAX_VALUE。

The two infinities are both used to represent two things, actually infinite results and results that are too large in absolute magnitude to represent in the normal number system, numbers larger than Double.MAX_VALUE or smaller than -Double.MAX_VALUE. 1.0/0.0 is infinite. So is 2*Double.MAX_VALUE.

通过允许中间结果在任何意义上都是无限的,有些算法可以简化,特殊情况更少。这样做也允许例如甚至是与y轴平行的线,以具有可用于计算的可存储梯度。

There are some algorithms that can be simplified, with fewer special cases, by allowing intermediate results to be infinite, in either sense. Doing so also allows e.g. even a line parallel to the y-axis to have a storable gradient that can be used in calculations.

这篇关于Java中的INFINITY常量是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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