精度损失 - int - >浮动或双重 [英] Loss of precision - int -> float or double

查看:99
本文介绍了精度损失 - int - >浮动或双重的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我正在修改的考试题目,问题是4分。


在java中,我们可以分配一个int到双重或浮动。这样会丢失信息,为什么?


我已经认为,因为int通常是固定的长度或大小 - 存储数据的精度是有限的,其中在浮点中存储信息可以是无限的,基本上我们失去信息,因为这个



现在我有点粗略地说,我是否打了这里的正确区域。我非常确定它会失去精确度,但我不能把我的手指放在为什么。我可以获得一些帮助吗?

解决方案

在Java中整数使用32位来表示其值。



在Java中,FLOAT使用23位尾数,因此大于2 ^ 23的整数将使其最低有效位被截断。例如,33554435(或0x200003)将被截断到大约33554432 +/- 4



在Java中,DOUBLE使用52位尾数,因此将能够表示32位整数而不丢失数据。



另见浮动点在维基百科上


I have an exam question I am revising for and the question is for 4 marks.

"In java we can assign a int to a double or a float". Will this ever lose information and why?

I have put that because ints are normally of fixed length or size - the precision for storing data is finite, where storing information in floating point can be infinite, essentially we lose information because of this

Now I am a little sketchy as to whether or not I am hitting the right areas here. I very sure it will lose precision but I can't exactly put my finger on why. Can I get some help, please?

解决方案

In Java Integer uses 32 bits to represent its value.

In Java a FLOAT uses a 23 bit mantissa, so integers greater than 2^23 will have their least significant bits truncated. For example 33554435 (or 0x200003) will be truncated to around 33554432 +/- 4

In Java a DOUBLE uses a 52 bit mantissa, so will be able to represent a 32bit integer without lost of data.

See also "Floating Point" on wikipedia

这篇关于精度损失 - int - >浮动或双重的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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