0.0 和 1.0 之间有多少个双数? [英] How many double numbers are there between 0.0 and 1.0?

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

问题描述

这是我多年来一直在想的事情,但我以前从未花时间问过.

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

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

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.

所以问题是:

  1. 0.0 和 1.0 之间有多少个 double 数字?
  2. 1 和 2 之间的数字是否一样多?在 100 和 101 之间?在 10^100 和 10^100+1 之间?

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

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

推荐答案

Java doubles are in IEEE-754 格式,因此它们有 52 位小数;在任何两个相邻的 2 次幂之间(包括 1 次幂且不包括下一个次幂),因此将有 2 的 52 次幂不同的double(即,4503599627370496).例如,这就是介于 0.5 包含和 1.0 排除之间的不同 double 的数量,并且正是这些数量也位于 1.0 包含和 2.0 排除之间,依此类推.

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.

计算 0.0 和 1.0 之间的 doubles 比在 2 的幂之间计算要困难,因为在该范围内包含许多 2 的幂,而且,还有一个棘手的问题非规范化数字.指数的 11 位中有 10 位涵盖了所讨论的范围,因此,包括非规范化数字(我认为几种 NaN),您将获得 double 的 1024 倍>s 介于 2 的幂之间 - 无论如何总共不超过 2**62 .排除非规范化 &c,我相信计数将是 2**52 的 1023 次.

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.

对于像100 到 100.1"这样的任意范围,它甚至更难,因为上限不能完全表示为 double(不是任何 2 的任何幂的精确倍数).作为一个方便的近似值,由于 2 的幂之间的级数是线性的,您可以说所述范围是周围 2 的幂(64 和 128)之间跨度的 0.1/64th,因此您预计

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

不同的 double s -- 这涉及到 7036874417766.4004... 给或取一两个;-)

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

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

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