标准浮点数在0和1之间有多少个唯一值? [英] How many unique values are there between 0 and 1 of a standard float?

查看:916
本文介绍了标准浮点数在0和1之间有多少个唯一值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我猜这个问题的另一种方法是使用一个 float 的小数位,它只能在0到1之间吗?



我试图通过查看 MSDN <一>。哪个说精度是7位数。我认为这意味着它只能跟踪 0.0000001 的变化。然而,如果我这样做:

  float test = 0.00000000000000000000000000000000000000000001f; 
Console.WriteLine(test);

写出 9.949219E-44



如果我添加更多的零,它会输出 0



我很确定我在这里错过了一些东西,因为这个准确度似乎大错特错。主要是作为一个浮点数是32位的大小,只是从0-1的准确性水平包含1e + 44可能的数字...


标准浮点数在0和1之间有多少个唯一值?

是不是真的是你想要回答的问题,但答案是,不包括 0 1 本身,在此范围内有 2 ** 23 - 1 次正常数字和 126 * 2 ** 23 正常数字,总共 127 * 2 ** 23 - 1 ,或 1,065,353,215

但请注意,这些数字在 0 和<$之间的间隔上均匀分布 C $ C> 1 。在从 0f 1f的循环中使用 1f / 1065353215f 的delta

如果你想用0.0到1.0的步长(十进制)形式0.00 ... 01 ,也许你应该使用 decimal 来代替 float 。如果你坚持 float ,试着用来表示这个数字。 0.000001 (比建议价值高10倍),但是请注意,在执行非常多的添加时会出现错误,其中包含一个不可表示的数字。

<另外请注意:有几个域,您甚至不能计算 float 的前七位重要小数位数。尝试将 0.000986f 0.000987f 的值保存到 float 变量(确保优化不会将值保存在更宽的存储位置)并写出该变量。前七位数字不同于 0.0009860000 和resp。 0.0009870000 。如果您想使用小数展开为short的数字,您可以再次使用 decimal

编辑:如果你可以在你的循环中使用一个二进制的步骤,请尝试:

  float delta = (float)Math.Pow(2,-24); 

或等同于文字:

  const float delta = 5.96046448e-8f; 

这个增量的好处是你在循环中的所有值都可以在你的。就在(<)之前( 1f ),你将会采取尽可能短的步骤。

I guess another way of phrasing this question is what decimal place can you go to using a float that will only be between 0 and 1?

I've tried to work it out by looking at the MSDN. Which says the precision is 7 digits. I thought that meant it could only track changes of 0.0000001.

However if I do:

float test = 0.00000000000000000000000000000000000000000001f;
Console.WriteLine(test);

It writes out 9.949219E-44

If I add any more zeroes, it will output 0.

I'm pretty sure I'm missing something here as that degree of accuracy seems massively wrong. Mainly as a float is 32bits in size, and just from 0-1 at that level of accuracy contains 1e+44 possible numbers...

解决方案

How many unique values are there between 0 and 1 of a standard float?

This is not really the question you want an answer for, but the answer is, not including 0 and 1 themselves, that there are 2**23 - 1 subnormal numbers and 126 * 2**23 normal numbers in this range, for a total of 127 * 2**23 - 1, or 1,065,353,215.

But note that these numbers are not evenly distributed on the interval between 0 and 1. Using a "delta" of 1f / 1065353215f in a loop from 0f to 1f will not work for you.

If you want to step from 0.0 to 1.0 with eqally long steps of the (decimal) form 0.00...01, maybe you should use decimal instead of float. It will represent numbers like that exactly.

If you stick to float, try with 0.000001 (ten times greater than your proposed value), but note that errors can build up when performing very many additions with a non-representable number.

Also note: There are a few "domains" where you can't even count on the first seven significant decimal digits of a float. Try for example saving the value 0.000986f or 0.000987f to a float variable (be sure the optimization doesn't hold the value in a "wider" storage location) and write out that variable. The first seven digits are not identical to 0.0009860000 resp. 0.0009870000. Again you can use decimal if you want to work with numbers whose decimal expansions are "short".

Edit: If you can use a "binary" step for your loop, try with:

float delta = (float)Math.Pow(2, -24);

or equivalently as a literal:

const float delta = 5.96046448e-8f;

The good thing about this delta is that all values you encouter through the loop are exactly representable in your float. Just before (under) 1f, you will be taking the shortest possible steps possible for that magnitude.

这篇关于标准浮点数在0和1之间有多少个唯一值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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