Java中的浮点投射 [英] Floating Point Casting in Java

查看:124
本文介绍了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.

至少到Java 7,我明白浮点不能用于按位操作。这是有道理的,因为它们如何在内部存储。有什么重要的,需要知道浮点如何操作或投入吗?

At least up to Java 7, I understand that floating points cannot be used in bitwise operations. 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?

所以,总结:

重要的是理解浮点类似整数的内部工作原理吗?

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调用符合IEEE-754标准的机器码指令。 Java没有什么可以这样做。如果你想知道如何铸造工作,我建议你阅读标准。

Java calls the machine code instruction which does this in compliance with the IEEE-754 standard. There is nothing for Java to do as such. If you want to know how casting works I suggest you read the standard.

基本上,尾数被移位指数和应用的符号。即浮点数是符号* 2 ^指数*尾数,它所做的就是执行此计算和丢弃和小数部分。

Basically, the mantissa is shifted by the exponent and the sign applied. i.e. a floating point number is sign * 2^exponent * mantissa and all it does is perform this calculation and drop and fractional parts.

这篇关于Java中的浮点投射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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