这个问题/问题可能与这个话题 https://stackoverflow.com/questions/55130265/inconsistency-with-gnuplot-format-specifiers-t-and-t.
如果您输入:
print log10(1e7)
你会得到7.0
.
print int(log10(1e7))
你会得到7
.
但是,如果您输入
print log10(1e6)
你会得到6.0
.
print int(log10(1e6))
你会得到5
.
这些可能是与以下相关的舍入误差log10
并且无法(?)避免。
因为如果你输入
print sprintf("%.20e",log10(1e6))
gives 5.99999999999999911182e+00
print sprintf("%.20e",log10(1e7))
gives 7.00000000000000000000e+00
您可以将其扩展并总结为一个图:
代码:
### power problem in gnuplot
reset session
set colorsequence classic
set key left
set samples 41
set xrange[-20:20]
plot int(log10(10**x)) w lp pt 7,\
x w lp pt 7
### end of code
Result:
您会发现,在不规则的距离中,预期结果与获得的结果之间存在差异。
所以,我仍然缺少一个函数,它总是给我正确的数量级。也许将所有数字四舍五入到小数点后 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
Result:
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:])
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)