Search code examples
gnuplotnumber-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?


Solution

  • 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.