当我使用浮点数时,如果我限制范围,是否会失去精度? [英] When I use floating point numbers, do I lose precision if I limit the range?

查看:147
本文介绍了当我使用浮点数时,如果我限制范围,是否会失去精度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试阅读关于浮点实现的IEEE标准,但是对于我来说这是相当先进的,我不是为了谋生而编程的。

我想知道。

假设我有一个介于0到N之间的浮点型变量,我会在需要的时候扩展它,但是中间的计算会在这个非标准化的值中完成。 p>

从逻辑上讲,范围 [0.0,1.0] 就足够了。但是,如果我将数字限制在0和1之间,我是否会以某种方式失去精度?如果我使用 [0,10.0] [0,1000000.0]



谢谢。

解决方案

你的想法很好,因为从某种意义上讲,浮点数是为你设计的。



在内部,浮点数有两部分,称为尾数和指数。尾数相当于你的范围,指数就是把它变成真实价值所需的比例。



你给的任何数字都被归一化到一个范围[1-9.999 ...]加上10乘数的功率。所以你几乎所有的数值都能得到尾数的精确度(唯一的例外是当你达到指数的极限 - 这些被称为非规格化数字(或低于正常),但是非常小(接近于零)或大,而不是你需要担心的,通常)。

因此,无论你输入1.23或12.3或123,它存储为1.23(尾数)加上一个因子(1,10或100,指数)在内部。实际上,这是不正确的,因为它使用2(二进制)的幂而不是十的幂(十进制)(请参阅sixlettervariable的评论),但它是相同的一般想法。


I tried to read on the IEEE standard about floating point implementation but it is quite advanced for me and I don't program for a living.

This is something I wondered.

Suppose I have a float variable between 0 and N and I will scale it up when needed, but intermediary calculations will be done in this unnormalized value.

Logically, the range [0.0, 1.0] will be enough. But if I limit my numbers between 0 and 1, do I lose precision some way? Do I gain anything if I use [0, 10.0], or [0, 1000000.0]?

Thank you.

解决方案

your idea is a good one, but you don't save anything because, in a sense, floating point numbers are designed to do this for you.

internally, a float has two parts, called mantissa and exponent. the mantissa is equivalent to your range, and the exponent is the scaling needed to get it to the "real" value.

any number you give is normalised to a range like [1-9.999...] plus a power of 10 multiplier. so you get the precision of the mantissa for almost all values (the only exceptions are when you get to the extremes of the exponent - these are called "denormalized numbers" (or "subnormal"), but are extremely small (close to zero) or large, and not something you need worry about, normally).

so whether you enter 1.23 or 12.3 or 123, it's stored as 1.23 (the mantissa) plus a factor (1, 10 or 100, the exponent) internally.

actually, that's not quite true, because it uses powers of 2 (binary) rather than powers of ten (decimal) (see the comment from sixlettervariable), but it's the same general idea.

这篇关于当我使用浮点数时,如果我限制范围,是否会失去精度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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