gnuplot:如何获得正确的数量级?
这个问题/问题可能与你的工作有关 如果键入:gnuplot:如何获得正确的数量级?,gnuplot,logarithm,rounding-error,Gnuplot,Logarithm,Rounding Error,这个问题/问题可能与你的工作有关 如果键入: 打印日志10(1e7)您将获得7.0 print int(log10(1e7))您将获得7 但是,如果您键入 打印日志10(1e6)您将获得6.0 print int(log10(1e6))您将获得5 这些可能是与log10相关的舍入错误,无法避免 因为如果你打字 打印sprintf(“%.20e”,log10(1e6))给出5.99999999999911182e+00 打印sprintf(“%.20e”,log10(1e7))给出7.000000
打印日志10(1e7)
您将获得7.0
print int(log10(1e7))
您将获得7
但是,如果您键入
打印日志10(1e6)
您将获得6.0
print int(log10(1e6))
您将获得5
这些可能是与log10
相关的舍入错误,无法避免
因为如果你打字
打印sprintf(“%.20e”,log10(1e6))
给出5.99999999999911182e+00
打印sprintf(“%.20e”,log10(1e7))
给出7.00000000000000e+00
您可以将其扩展并汇总为绘图:
代码:
结果:
Number cP icP1 icP2
1e-6: -6 -5 -6
1e-4: -4 -3 -4
0.001: -3 -2 -3
0.01: -2 -1 -2
1e-2: -2 -1 -2
1000: 3 2 3
1000000: 6 5 6
-1e-6: -6 -5 -6
-1e-9: -9 -8 -9
0.99: -1 0 0
95: 1 1 2
990: 2 2 3
您将看到,在不规则的距离中,预期结果和获得的结果之间存在差异
所以,我仍然缺少一个函数,它总是给我正确的数量级。也许先把所有数字四舍五入到小数点后15位?还有其他想法吗?假设您处理的数字不超过12-15位(或者正如@Ethan所说,64位系统中超过15-16位的数字是无意义的),下面的函数应该给出整数的正确数量级。我只是测试了几个示例,并将其与其他“简单”方法进行了比较。请证明这个函数是对的还是错的
### get the correct power of a number with gnuplot
CorrectPower(n) = floor(log10(n*(1+1e-15)))
IncorrectPower1(n) = floor(log10(n))
IncorrectPower2(n) = floor(gprintf("%T",n))
Numbers = "1e-6 1e-4 0.001 0.01 1e-2 1000 1000000 -1e-6 -1e-9 0.99 95 990"
print " Number cP icP1 icP2"
do for [i=1:words(Numbers)] {
n = word(Numbers,i)
print \
sprintf("%7s:%5d%5d%5d", n, CorrectPower(n), IncorrectPower1(n), IncorrectPower2(n))
}
### end of code
结果:
Number cP icP1 icP2
1e-6: -6 -5 -6
1e-4: -4 -3 -4
0.001: -3 -2 -3
0.01: -2 -1 -2
1e-2: -2 -1 -2
1000: 3 2 3
1000000: 6 5 6
-1e-6: -6 -5 -6
-1e-9: -9 -8 -9
0.99: -1 0 0
95: 1 1 2
990: 2 2 3
加法:值得一提的是,另一个函数可以获得整数的正确幂:
CorrectPower2(n)=int(sprintf(“%.15e”,n)[19:])
打印格式为“%.20e”的双精度打印只会带来垃圾。IEEE 64位浮点数仅编码53位(小数点后15-16位)的精度。将要求的精度限制在小数点后15位不是“四舍五入”,而是确认实际存在的信息。我同意。顺便问一下,sprintf(“%.0e”,5.99)
?它似乎是圆的,不是截断的。是否总是使用此函数获取所需的整数?“%.0e”不包含小数点。请参阅libc printf系列的文档man 3 printf