0之间有多少双数

0之间有多少双数

本文介绍了0.0和1.0之间有多少双数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是多年来一直存在的问题,但我从来没有花时间询问。



许多(伪)随机数生成器生成一个随机数介于0.0和1.0之间。数学上在这个范围内有无数的数字,但 double 是一个浮点数,因此有一个有限的精度。



所以问题是:


  1. 只有几个 double 0.0和1.0?

  2. 1到2之间的数字是多少?在100到101之间?在10 ^ 100和10 ^ 100 + 1之间?

注意:如果它有所作为,我对Java的定义感兴趣 double 特别是。

解决方案

Java double 在格式,因此他们有52位分数;在两个相邻的两个权力之间(包括一个而不是另一个)的两个权力之间,因此将有2到52的权力不同 double s(即他们的4503599627370496 )。例如,这是不包括在0.5包含和1.0之间的不同的 double 的数量,而且还有很多也包括在1.0包含和2.0之间的排除,等等。 >

在0.0和1.0之间计算双打比两者之间的权力更难,因为有两个权力包含很多权力在这个范围内,还有一个人陷入了非正规化数字的棘手问题。指数的11位中有10位覆盖了所讨论的范围,因此,包括非规范化数字(我认为几种 NaN ),您将具有1024次 double s在两个幂之间 - 不超过 2 ** 62 。排除非正规化&C,我相信这个数字将是1023倍 2 ** 52



范围就像100到100.1,它更加困难,因为上限不能被正确地表示为一个 double (不是任何两个权力的确切倍数)。作为一个方便的近似,由于两个幂的进化是线性的,你可以说所述范围是两个周围力量之间的距离(64),所述范围是 0.1 / 64

 (0.1 / 64)* 2 ** 52 

distinct double s - 来到 7036874417766.4004 ...给或取一或两个; - )。


This is something that's been on my mind for years, but I never took the time to ask before.

Many (pseudo) random number generators generate a random number between 0.0 and 1.0. Mathematically there are infinite numbers in this range, but double is a floating point number, and therefore has a finite precision.

So the questions are:

  1. Just how many double numbers are there between 0.0 and 1.0?
  2. Are there just as many numbers between 1 and 2? Between 100 and 101? Between 10^100 and 10^100+1?

Note: if it makes a difference, I'm interested in Java's definition of double in particular.

解决方案

Java doubles are in IEEE-754 format, therefore they have a 52-bit fraction; between any two adjacent powers of two (inclusive of one and exclusive of the next one), there will therefore be 2 to the 52th power different doubles (i.e., 4503599627370496 of them). For example, that's the number of distinct doubles between 0.5 included and 1.0 excluded, and exactly that many also lie between 1.0 included and 2.0 excluded, and so forth.

Counting the doubles between 0.0 and 1.0 is harder than doing so between powers of two, because there are many powers of two included in that range, and, also, one gets into the thorny issues of denormalized numbers. 10 of the 11 bits of the exponents cover the range in question, so, including denormalized numbers (and I think a few kinds of NaN) you'd have 1024 times the doubles as lay between powers of two -- no more than 2**62 in total anyway. Excluding denormalized &c, I believe the count would be 1023 times 2**52.

For an arbitrary range like "100 to 100.1" it's even harder because the upper bound cannot be exactly represented as a double (not being an exact multiple of any power of two). As a handy approximation, since the progression between powers of two is linear, you could say that said range is 0.1 / 64th of the span between the surrounding powers of two (64 and 128), so you'd expect about

(0.1 / 64) * 2**52

distinct doubles -- which comes to 7036874417766.4004... give or take one or two;-).

这篇关于0.0和1.0之间有多少双数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 08:35