[英]Floating Point Casting in Java
Casting for integers is very straightforward, the extra bits simply disappear. 整数转换非常简单,多余的位就消失了。
But, is it important to understand what is happening under the hood for casting floating point? 但是,重要的是要了解在进行浮点运算的幕后到底发生了什么? I've tried to read information on how floating point is calculated, but I have yet to find one that explains it well. 我试图阅读有关如何计算浮点数的信息,但是我还没有找到一个能很好解释这一点的信息。 At least that's my excuse. 至少那是我的借口。 I get the basic idea although the calculation of the mantissa is a bit difficult. 尽管尾数的计算有些困难,但是我得到了基本的想法。
At least up to Java 7, I understand that floating points cannot be used in bitwise operations. 至少在Java 7之前,我知道不能在按位运算中使用浮点。 Which makes sense because of how they are stored internally. 这是有道理的,因为它们是如何在内部存储的。 Is there anything important that is needed to know on how floating points operate or are cast? 是否需要了解浮点如何工作或强制转换的任何重要信息?
So, to Summarize: 因此,总结一下:
Is it important to understand the internal workings of floating point like integers? 了解浮点数(如整数)的内部工作原理是否重要?
What is the internal process of casting a floating point to an integer? 将浮点数转换为整数的内部过程是什么?
What is the internal process of casting a floating point to an integer? 将浮点数转换为整数的内部过程是什么?
Java calls the machine code instruction which does this in compliance with the IEEE-754 standard. Java调用符合IEEE-754标准的机器代码指令。 There is nothing for Java to do as such. Java并没有做任何事情。 If you want to know how casting works I suggest you read the standard. 如果您想知道铸造的工作方式,建议您阅读标准。
Basically, the mantissa is shifted by the exponent and the sign applied. 基本上,尾数会随着指数和所施加的符号而移动。 ie a floating point number is sign * 2^exponent * mantissa and all it does is perform this calculation and drop and fractional parts. 也就是说,浮点数是符号* 2 ^指数*尾数,它所做的只是执行此计算,然后计算掉和小数部分。
First, you need to understand that a floating point number is essentially an approximation. 首先,您需要了解浮点数本质上是一个近似值。 You can put in, say 1.23 and get out 1.229998 (or some such), because 1.23 is represented exactly. 您可以输入1.23,然后输入1.229998(或类似的值),因为精确地表示了1.23。 Regardless of whether you will be doing any casts, you need to understand this, and how it affects computations (and especially comparisons). 不管您是否要进行任何强制转换,都需要了解这一点,以及它如何影响计算(尤其是比较)。
From the standpoint of cast, casting a float
to a double
causes no loss of information, since a double
can contain every value that a float
can contain. 从强制转换的角度来看,将float
转换为double
float
不会造成信息丢失,因为double
float
可以包含float
可以包含的每个值。 But casting from double
to float
can cause loss of precision (and, for very large or small numbers, exponent overflow/underflow), since there's simply more information in a 64-bit value than in a 32-bit one, so some data's going to end up "on the floor". 但是,从double
精度float
转换为float
会导致精度损失(对于很大或很小的数字,都会导致指数上溢/下溢),因为64位值中的信息仅比32位值中的信息多,因此某些数据在不断增加。最终“落在地板上”。
Similarly, casting from an int
to a double
causes no loss of information, since a double
can contain every value an int
can contain and then some. 同样,从int
转换为double
不会导致信息丢失,因为double
可以包含int
可以包含的每个值,然后包含一些值。 But casting from int
to float
or from long
to double
or float
can result in loss of precision (though there can never be an exponent overflow/underflow). 但是从int
为float
或从long
为double
或float
可能会导致精度损失(尽管永远不会出现指数上溢/下溢)。
Casting from float
or double
to int
or long
can easily result in overflow/underflow and major loss of data, if the float
or double
value has a large positive exponent or any negative exponent. 如果float
或double
值具有大的正指数或任何负的指数,则从float
或double
为int
或long
会很容易导致上溢/下溢和大量数据丢失。 And, of course, when you cast from floating-point to fixed the fractional part of the number is truncated (essentially a "floor" operation). 而且,当然,当您从浮点数转换为固定数时,数字的小数部分会被截断(本质上是“ floor”运算)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.