简体   繁体   English

自动截断浮点值

[英]Auto truncation of float value

Need help with the following. 在以下方面需要帮助。 I following value is getting truncated as follows: 我以下值被截断如下:

    float data1 = 48.12345678f;
    float data2 = 12.12345678f;
    float data3 = 90.12345678f;
    float data4 = 56.12345678f;

    System.out.println(data1);
    System.out.println(data2);
    System.out.println(data3);
    System.out.println(data4);

Output -

48.123455
12.123457
90.12346
56.123455

It looks like a float value property and it will get fixed if we use double value. 它看起来像一个float值属性,如果我们使用double值,它将得到固定。

This is varying across different system even though the JVM version seems same. 即使JVM版本似乎相同,这在不同的系统上也会有所不同。 So I need to find an explanation on what basis the rounding off is taking place. 因此,我需要根据四舍五入的基础找到一个解释。 In one instance it is incrementing the digit (12.123457) and in one decrementing (48.123455). 一种情况是递增数字(12.123457),另一种递减数字(48.123455)。 So could you please help me understand the reason of this ambiguity and what property is varying the result from different system. 因此,能否请您帮助我理解这种歧义的原因,以及哪些属性正在改变来自不同系统的结果。

To start with your questions,the rounding off is obviously system-specific as the CPU represents floating point data as per it's own way. 首先从您的问题开始,四舍五入显然是特定于系统的,因为CPU按照自己的方式表示浮点数据。

So in your case---> In one instance it is incrementing the digit (12.123457) and in one decrementing (48.123455).It is happening on different machines or systems. 因此,在您的情况下--->一种情况是数字递增(12.123457),另一种数字递减(48.123455),这发生在不同的机器或系统上。

As the numbers are stored in the system in binaries,SO The binary representation of the numbers may not be exact. 由于数字以二进制形式存储在系统中, The binary representation of the numbers may not be exact.

It is not unusual for specific floating point or double precision numbers to not be represented exactly as desired. 特定的浮点数或双精度数字不能完全按要求表示是很正常的。 Floating-point decimal values generally do not have an exact binary representation due to how the CPU represents floating point data. 由于CPU如何表示浮点数据,因此浮点十进制值通常没有精确的二进制表示形式。 For this reason, you may experience a loss of precision, and some floating-point operations may produce unexpected results 因此,您可能会遇到精度损失,并且某些浮点运算可能会产生意外结果

This is due to how float numbers are represented in the computer memory. 这是由于在计算机内存中如何表示浮点数。 Here's an online demo of how float numbers are stored in memory (binary representation) 这是有关浮点数如何存储在内存中的在线演示(二进制表示)

http://www.h-schmidt.net/FloatConverter/IEEE754.html http://www.h-schmidt.net/FloatConverter/IEEE754.html

According to the IEEE 754 standard, a single precision number is stored in a 32bit word composed of one sign bit, 8 exponents bits, and 23 mantissa bits. 根据IEEE 754标准,单个精度数字存储在一个32位字中,该字由一个符号位,8个指数位和23个尾数位组成。 If you follow the steps of how to represent the number 48.12345678 you will find that there's underflow and some bits are truncated because it exceeds the 23 mantissa bits 如果按照如何表示数字48.12345678的步骤进行48.12345678您会发现存在下溢并且某些位被截断,因为它超过了23个尾数位

Example, in the above link, try to plug: 48.12345678 in the Decimal Representation, then press enter. 例如,在上面的链接中,尝试插入:在十进制表示中插入48.12345678,然后按Enter。 You will see that the number will be converted to 48.123455 and above it you will find the single precision representation of the number in binary 您将看到该数字将转换为48.123455并且在该数字之上,您将找到二进制的数字单精度表示形式

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

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