简体   繁体   English

Coretto Java 浮点数

[英]Coretto Java Floating Point

Java typically has had a problem with inclusion of floating point arithmetic denormal and pronormal value inclusion upon operations such as this: Java 通常在包含浮点算术非正规和 pronormal 值的操作时遇到问题,例如:

double a = 0.1;
double b = 0.1;
double x = a*b;
out.println();
out.println(x);

by means of the float or double types.通过 float 或 double 类型。

Can someone please tell me how to tell Coretto Java to enter non denormal, accurate floating point mode?有人可以告诉我如何告诉 Coretto Java 进入非规范的、准确的浮点模式吗?

“Can someone please tell me how to tell Coretto Java to enter non denormal, accurate floating point mode?” “有人能告诉我如何告诉 Coretto Java 进入非规范的、准确的浮点模式吗?”

This is not a Corretto-specific problem.这不是 Corretto 特有的问题。 It's a general Java problem.这是一个普遍的 Java 问题。 The simple answer is in the same way you do it in other versions of OpenJDK.简单的答案与您在其他 OpenJDK 版本中的做法相同。

You can't use Java primitive types like float and double.您不能使用像 float 和 double 这样的 Java 基本类型。 You have to resort to another math library.你必须求助于另一个数学库。 java.math.数学BigDecimal might be a good start. BigDecimal可能是一个好的开始。

jshell> import static java.math.BigDecimal.*;
jshell> var a = ONE.divide(TEN)
a ==> 0.1
jshell> var b = ONE.divide(TEN) 
b ==> 0.1
jshell> a.add(b)
$16 ==> 0.2

TLDR:域名注册地址:

Primitive types float and double are defined in Java Language Spec. Java 语言规范中定义了原始类型 float 和 double。 Java implements the IEEE 754 definition of their representation. Java 实现了它们表示的 IEEE 754 定义。

4.2.3. 4.2.3. Floating-Point Types, Formats, and Values The floating-point types are float and double, which are conceptually associated with the single-precision 32-bit and double-precision 64-bit format IEEE 754 values and operations as specified in IEEE Standard for Binary Floating-Point Arithmetic, ANSI/IEEE Standard 754-1985 (IEEE, New York).浮点类型、格式和值 浮点类型是 float 和 double,它们在概念上与 IEEE 标准中指定的单精度 32 位和双精度 64 位格式 IEEE 754 值和操作相关联。二进制浮点运算,ANSI/IEEE 标准 754-1985(IEEE,纽约)。

https://docs.oracle.com/javase/specs/jls/se11/html/jls-4.html#jls-4.2.3 https://docs.oracle.com/javase/specs/jls/se11/html/jls-4.html#jls-4.2.3

As a result, the following output is expected because of IEEE 754 binary representation.因此,由于 IEEE 754 二进制表示,预计会出现以下输出。

jshell> double a = 0.1;
a ==> 0.1
jshell> double b = 0.1;
b ==> 0.1
jshell> double x = a*b;
x ==> 0.010000000000000002

References:参考:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM