其实c中也一样,都是遵循ieee754规则
我这里说double 64位在thinking in java 第3版中,作者关于random能产生多少个浮点数是这里解释的,原文(注释中的说明)Chuck Allison writes: The total number of numbers in a floating-point number system is
2(M-m+1)b^(p-1) + 1
where b is the base (usually 2), p is the precision (digits in the mantissa), M is the largest exponent, and m is the smallest exponent. IEEE 754 uses:
M = 1023, m = -1022, p = 53, b = 2
so the total number of numbers is
2(1023+1022+1)2^52
= 2((2^10-1) + (2^10-1))2^52
= (2^10-1)2^54
= 2^64 - 2^54
Half of these numbers (corresponding to exponents in the range [-1022, 0]) are less than 1 in magnitude (both positive and negative), so 1/4 of that expression, or 2^62 - 2^52 + 1 (approximately 2^62) is in the range [0,1). See my paper at http://www.freshsources.com/1995006a.htm (last of text).Math.random()产生的double范围在[0,1)
根据上面的文章,应该产生约2^62=4611686018427387904个值 19位10进制数而实际上浮点数的有效位数约为16到17位,而不是19位
你可以测试设置一个20位的浮点数然后打印
谁可以解释的清楚?
http://www.freshsources.com/1995006a.htm 这里文中的链接也没有了,谁可以提供一下?
ieee754的部分说明
double 型態: 用 8 個 bytes 儲存, 也就是 64 bits.
                各個 bit 的用途如下:          bit      63      52~62        0~51
               ┌───┬────┬───────┐
               │正負號│  指數  │     底數     │
               └───┴────┴───────┘     < 說明 >   正負號 (sign): 1 為負, 0 為正.                指數 (exponential): 將底數乘上 2 的指數次方後就是原來的數.
                      須注意的是: float 時, 因有 8 bits, 所以能表示的有 2 的
                      256 次方, 但因為指數應可正可負, 所以 IEEE 規定, 此處算
                      出的次方須減去 127 才是真的指數,所以 float 的指數可從
                      -126 到 128.
                      同理, double 型態有 11 bits, 算出的值須減去 1023, 所以
                      double 的指數可從 -1022 到 1024.                底數 (mantissa): 此部份格式實在難以用文字說明,
                      請參考下面的例子.     < 特例 >   0 因為無法用任何 2 的次方表示, 所以 0 的表示法就是
                float : 00 00 00 00
                double: 00 00 00 00 00 00 00 00

解决方案 »

  1.   

    Java Language Specification 3 中提到了 这个 IEEE 754。
      

  2.   

    谁能说明 java double的有效位数到底为多少位?
    why?
      

  3.   

    http://blog.csdn.net/treeroot/archive/2004/09/05/95071.aspx
      

  4.   

    http://blog.csdn.net/treeroot/archive/2004/10/27/155076.aspx
      

  5.   

    to treeroot(旗鲁特):有参考价值,不过我说的具体的是关于有多少有效位数,而不是能表达数的范围.对于float来说,最大可以支持到(2-2^(-23))*2^127并不能说明他可以表达这么多数字对于1.245678901e35f和1.245678902e35f 来说,他们是一样大的
      

  6.   

    具体的针对thinking in java来说,我觉得作者关于Math.random()可以产生多少个数的这段说错了,因为有效位数达不到,所以实际上产生不了这么多有效数.