The following SAS code:
以下SAS代码:
data _null_;
format t u best32.;
t = 10000000000000000000000000;
u = 1e25;
put t u;
if t ne u then put 'diff';
run;
on my Windows machine prints out:
在我的Windows机器上输出:
10000000000000000905969664 9999999999999998758486016
diff
While I understand that only the first 15-16 digits are to be trusted, why do they give different underlying numbers at all? How is SAS computing 1e25?
虽然我知道只有前15-16个数字值得信任,但他们为什么要提供不同的底层数字呢? SAS计算1e25如何?
Edit: I've been asked to give the output for other powers of 10 up to 1e25. The following program:
编辑:我被要求输出其他10的功率,最高为1e25。以下程序:
%macro doit;
data _null_;
format t u best32.;
%let t=1;
%do i=1 %to 25;
%let t=&t.0;
t = &t;
u = 1e&i;
put t u;
%end;
run;
%mend;
%doit;
gives the following output:
给出以下输出:
10 10
100 100
1000 1000
10000 10000
100000 100000
1000000 1000000
10000000 10000000
100000000 100000000
1000000000 1000000000
10000000000 10000000000
100000000000 100000000000
1000000000000 1000000000000
10000000000000 10000000000000
100000000000000 100000000000000
1000000000000000 1000000000000000
10000000000000000 10000000000000000
100000000000000000 100000000000000000
1000000000000000000 1000000000000000000
10000000000000000000 10000000000000000000
100000000000000000000 100000000000000000000
1000000000000000000000 1000000000000000000000
10000000000000000000000 10000000000000000000000
99999999999999991611392 99999999999999991611392
999999999999999983222784 999999999999999983222784
10000000000000000905969664 9999999999999998758486016
1 个解决方案
#1
It looks like it might actually be calculating 1 * 10 * 10 ... * 10
and the errors are creeping in as soon as you get beyond the significant number of bits for the underlying float type.
看起来它实际上可能正在计算1 * 10 * 10 ... * 10,并且一旦超出底层浮点类型的大量位,错误就会逐渐消失。
But I don't believe that would happen for an IEE754 type float/double since it's quite able to represent 1x10full-range without loss of precision.
但我不相信IEE754类型的float / double会发生这种情况,因为它能够在不损失精度的情况下代表1x10full范围。
One possibility is that the problem you are having is related to the ability to store floats with less precision than allowed (which could possibly screw up a 1e25 calculation) - see http://www.uc.edu/sashtml/lrcon/z0695157.htm#z0695187 for an explanation.
一种可能性是,您遇到的问题与存储浮点数的能力有关,精度低于允许值(这可能会导致1e25计算失败) - 请参阅http://www.uc.edu/sashtml/lrcon/z0695157。 htm#z0695187的解释。
Update 1:
Okay, so based on your comment, you're not restricting the length. What does the following code give you?
好的,所以根据你的评论,你不会限制长度。以下代码为您提供了什么?
t = 10;
u = 1e1;
put t u;
t = 100;
u = 1e2;
put t u;
t = 1000;
u = 1e3;
put t u;
: : :
t = 10000000000000000000000000;
u = 1e25;
put t u;
Based on that output, we can probably deduce what's going on under the covers.
根据该输出,我们可以推断出幕后发生的事情。
#1
It looks like it might actually be calculating 1 * 10 * 10 ... * 10
and the errors are creeping in as soon as you get beyond the significant number of bits for the underlying float type.
看起来它实际上可能正在计算1 * 10 * 10 ... * 10,并且一旦超出底层浮点类型的大量位,错误就会逐渐消失。
But I don't believe that would happen for an IEE754 type float/double since it's quite able to represent 1x10full-range without loss of precision.
但我不相信IEE754类型的float / double会发生这种情况,因为它能够在不损失精度的情况下代表1x10full范围。
One possibility is that the problem you are having is related to the ability to store floats with less precision than allowed (which could possibly screw up a 1e25 calculation) - see http://www.uc.edu/sashtml/lrcon/z0695157.htm#z0695187 for an explanation.
一种可能性是,您遇到的问题与存储浮点数的能力有关,精度低于允许值(这可能会导致1e25计算失败) - 请参阅http://www.uc.edu/sashtml/lrcon/z0695157。 htm#z0695187的解释。
Update 1:
Okay, so based on your comment, you're not restricting the length. What does the following code give you?
好的,所以根据你的评论,你不会限制长度。以下代码为您提供了什么?
t = 10;
u = 1e1;
put t u;
t = 100;
u = 1e2;
put t u;
t = 1000;
u = 1e3;
put t u;
: : :
t = 10000000000000000000000000;
u = 1e25;
put t u;
Based on that output, we can probably deduce what's going on under the covers.
根据该输出,我们可以推断出幕后发生的事情。