Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
479 views
in Technique[技术] by (71.8m points)

number formatting - Inconsistency with gnuplot format specifiers %t and %T?

If you use the gnuplot format specifiers %t and %T, you will observe some inconsistent behaviour.

### gnuplot format specifiers

Numbers = "94 95 99 100 101"

do for [n in Numbers] {
    print gprintf("%3g",n)." = ".gprintf("%t",n)." x 10^".gprintf("%T",n)
}

Mantissa(n) = real(n)/10**floor(log10(n))
Power(n) = floor(log10(n))
do for [n in Numbers] {
    print gprintf("%3g",n)." = ",Mantissa(n)," x 10^",Power(n)
}
### end of code

Result:

 94 = 9.400000 x 10^1
 95 = 0.950000 x 10^2
 99 = 0.990000 x 10^2
100 = 1.000000 x 10^2
101 = 1.010000 x 10^2
 94 = 9.4 x 10^1
 95 = 9.5 x 10^1
 99 = 9.9 x 10^1
100 = 1.0 x 10^2
101 = 1.01 x 10^2

Why, for example, is 95 shown as 0.95 x 10^2 instead of 9.5 x 10^1? What is the reasoning behind this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Actually, besides gprintf("%t",95) and gprintf("%T",95) not showing the expected mantissa and power, also the formula floor(log10(n)) sometimes does not show the correct power of n. (see here: gnuplot: how to get correct order of magnitude?)

Suggestion for workaround: the following formulas make a detour via string formatting, but at least they should always give the correct mantissa and power.

Mantissa(n) = real(sprintf("%.15e",n)[1:strstrt(sprintf("%.15e",n),"e")-1])

Power(n) = int(sprintf("%.15e",n)[strstrt(sprintf("%.15e",n),"e")+1:])

In the longterm, the functions gprintf("%t",...), gprintf("%T",...) should be fixed in the gnuplot source code.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...