Skip to content

foresight_format ​

foresight_format, deterministic number-to-text conversion and XML escaping.

Output files are compared byte-for-byte against reference files, and the processor-dependent details of Fortran edit descriptors (optional leading zero of F0.d, signed zero, exponent layout) differ between compilers. Every number written by foresight therefore goes through integer arithmetic here: a value is rounded once to an integer count of 10**(-ndec) units and the decimal string is assembled from its digits.

User tick formats (gnuplot set format, a printf subset) are applied the same way, to the exact decimal value of a tick: format_decimal works on digit strings and rounds half to even, so the interactive viewer, which mirrors it, prints identical labels.

printf conversions of gnuplot's sprintf (c_real, c_integer, c_string) are C's: a real is converted from its exact binary value (exact_decimal), as glibc does, so %.2f of 2.675 is 2.67 (the double is 2.67499999...).

Text to real goes through real_from_decimal, which never raises an IEEE overflow: a debug build trapping overflows would otherwise stop on a data cell such as 1e999, inside the C library conversion.

Source: src/lib/foresight_format.F90

Dependencies

Contents ​

Variables ​

NameTypeAttributesDescription
LIMBinteger(kind=I8P)parameterBase of the exact decimal digits.
PIreal(kind=R8P)parameterC's M_PI (gnuplot's %P).
FIXED_CLAMPreal(kind=R8P)parameterMagnitude clamp keeping v * 10**ndec inside I8P for ndec <= 7.

Subroutines ​

real_from_decimal ​

Value of the decimal number text (Fortran list-directed syntax, e.g. -1.5e-3); ok false if it is malformed or beyond the real range, never raising an IEEE overflow.

Only a text whose exponent and length could reach the overflow range is read with the overflow halting off (and the overflow flag restored): the common case stays a plain read.

fortran
subroutine real_from_decimal(text, value, ok)

Arguments

NameTypeIntentAttributesDescription
textcharacter(len=*)inDecimal text.
valuereal(kind=R8P)outValue.
oklogicaloutWell formed and finite.

Call graph

decimal_of ​

Finite v rounded to 15 significant digits, as the exact decimal n * 10**e without trailing zeros in n.

Attributes: pure

fortran
subroutine decimal_of(v, n, e)

Arguments

NameTypeIntentAttributesDescription
vreal(kind=R8P)inValue.
ninteger(kind=I8P)outMantissa.
einteger(kind=I4P)outExponent.

Call graph

parse_decimal ​

Exact value of the decimal number text ([sign]digits[.digits][e[sign]digits]) as n * 10**e, without trailing zeros in n; ok false if malformed or beyond 18 significant digits.

Attributes: pure

fortran
subroutine parse_decimal(text, n, e, ok)

Arguments

NameTypeIntentAttributesDescription
textcharacter(len=*)inNumber.
ninteger(kind=I8P)outMantissa.
einteger(kind=I4P)outExponent.
oklogicaloutWell formed.

Call graph

format_decimal ​

Apply the tick format (see format_check; the format itself is returned if invalid) to the exact value n * 10**e: format_value with gnuplot's base 10 of the tick labels (%l as %t).

Attributes: pure

fortran
subroutine format_decimal(format, n, e, label, sup)

Arguments

NameTypeIntentAttributesDescription
formatcharacter(len=*)inFormat.
ninteger(kind=I8P)inMantissa.
einteger(kind=I4P)inExponent.
labelcharacter(len=:)outallocatableLabel.
supcharacter(len=:)outallocatableSuperscript, empty if none.

Call graph

format_value ​

Apply format (see format_check; the format itself is returned if invalid) to the value n * 10**e, as gnuplot's gprintf: the C conversions as printf would to that exact decimal, rounding half to even; the mantissas and powers as its mant_exp, in doubles (log10_base the logarithm of the base of %l and %L: 1 for the tick labels, 10 in gnuplot's gprintf function). sup is the exponent of the %h form.

Attributes: pure

fortran
subroutine format_value(format, n, e, log10_base, label, sup)

Arguments

NameTypeIntentAttributesDescription
formatcharacter(len=*)inFormat.
ninteger(kind=I8P)inMantissa.
einteger(kind=I4P)inExponent.
log10_basereal(kind=R8P)inlog10 of the base of %l and %L.
labelcharacter(len=:)outallocatableLabel.
supcharacter(len=:)outallocatableSuperscript, empty if none.

Call graph

classic ​

One C conversion f e E g G (or gnuplot's h H) of the exact value n * 10**e, rounding half to even.

Attributes: pure

fortran
subroutine classic(flags, width, prec, type, n, e, piece, sup)

Arguments

NameTypeIntentAttributesDescription
flagscharacter(len=*)inFlags.
widthinteger(kind=I4P)inField width, 0 if none.
precinteger(kind=I4P)inPrecision, -1 if absent.
typecharacter(len=1)inConversion.
ninteger(kind=I8P)inMantissa.
einteger(kind=I4P)inExponent.
piececharacter(len=:)outallocatableConverted text.
supcharacter(len=:)outallocatableSuperscript of h and H, empty if none.

Call graph

mant_exp ​

gnuplot's mant_exp: x = m * base**p (log10_base = log10(base)), in doubles as gnuplot; scientific: base 10 with p a multiple of 3; a mantissa within half a unit of the prec-th decimal of the base moves to the next power. 0 is 0 to the power 0.

Attributes: pure

fortran
subroutine mant_exp(log10_base, x, scientific, prec, m, p)

Arguments

NameTypeIntentAttributesDescription
log10_basereal(kind=R8P)inlog10 of the base.
xreal(kind=R8P)inValue.
scientificlogicalinPower of 1000.
precinteger(kind=I4P)inDecimals of the mantissa.
mreal(kind=R8P)outMantissa, signed.
pinteger(kind=I4P)outPower.

Call graph

next_conversion ​

From position i of format: the literal text (%% resolved) up to the next conversion and its fields, type blank at the end of the format; i left after the conversion.

Attributes: pure

fortran
subroutine next_conversion(format, i, text, flags, width, prec, type, message)

Arguments

NameTypeIntentAttributesDescription
formatcharacter(len=*)inFormat.
iinteger(kind=I4P)inoutPosition.
textcharacter(len=:)outallocatableLiteral text.
flagscharacter(len=:)outallocatableFlags.
widthinteger(kind=I4P)outField width, 0 if absent.
precinteger(kind=I4P)outPrecision, -1 if absent.
typecharacter(len=1)outConversion type, blank at the end.
messagecharacter(len=:)outallocatableProblem, empty if none.

Call graph

split_format ​

Split a tick format into its text around the one conversion and the conversion fields.

Attributes: pure

fortran
subroutine split_format(format, prefix, flags, width, prec, type, suffix, message)

Arguments

NameTypeIntentAttributesDescription
formatcharacter(len=*)inFormat.
prefixcharacter(len=:)outallocatableText before the conversion, %% resolved.
flagscharacter(len=:)outallocatableFlags.
widthinteger(kind=I4P)outField width, 0 if absent.
precinteger(kind=I4P)outPrecision, -1 if absent.
typecharacter(len=1)outConversion type.
suffixcharacter(len=:)outallocatableText after the conversion, %% resolved.
messagecharacter(len=:)outallocatableProblem, empty if none.

Call graph

exact_decimal ​

The exact decimal value of the finite v: |v| = digits * 10**e, digits without leading or trailing zeros (0 for zero). A double is m * 2**k, that is m * 5**(-k) * 10**k when k < 0: the product is computed exactly in base 10**9 limbs (at most 767 digits).

Attributes: pure

fortran
subroutine exact_decimal(v, digits, e)

Arguments

NameTypeIntentAttributesDescription
vreal(kind=R8P)inValue.
digitscharacter(len=:)outallocatableDecimal digits.
einteger(kind=I4P)outDecimal exponent.

Call graph

exp_digits ​

Mantissa of digits * 10**e in exponent form with prec decimals (printf %.<prec>e), and its exponent.

Attributes: pure

fortran
subroutine exp_digits(digits, e, prec, mantissa, x)

Arguments

NameTypeIntentAttributesDescription
digitscharacter(len=*)inDecimal integer, no sign.
einteger(kind=I4P)inExponent.
precinteger(kind=I4P)inDecimals of the mantissa.
mantissacharacter(len=:)outallocatableMantissa.
xinteger(kind=I4P)outExponent of the leading digit.

Call graph

limbs_multiply ​

limbs(1:n) *= f (base 109, least significant first; f below 231), n grown with the carry.

Attributes: pure

fortran
subroutine limbs_multiply(limbs, n, f)

Arguments

NameTypeIntentAttributesDescription
limbsinteger(kind=I8P)inoutDigits.
ninteger(kind=I4P)inoutLimbs in use.
finteger(kind=I8P)inFactor.

Call graph

Functions ​

int_str ​

Integer to minimal-width decimal string.

Attributes: pure

Returns: character(len=:)

fortran
function int_str(n) result(str)

Arguments

NameTypeIntentAttributesDescription
ninteger(kind=I8P)inInteger.

Call graph

decimal_str ​

Exact decimal string of the value n * 10**(-ndec), without trailing fractional zeros.

A negative ndec appends zeros: decimal_str(15_I8P, -2_I4P) is 1500.

Attributes: pure

Returns: character(len=:)

fortran
function decimal_str(n, ndec) result(str)

Arguments

NameTypeIntentAttributesDescription
ninteger(kind=I8P)inInteger mantissa.
ndecinteger(kind=I4P)inNumber of decimal digits of the mantissa.

Call graph

fixed ​

Value rounded to ndec <= 7 decimals, trailing zeros stripped; -0 prints as 0.

Magnitudes are clamped to 1e11: callers format pixel and unit-square coordinates only, where larger values lie far outside any clip region. v must not be NaN.

Attributes: pure

Returns: character(len=:)

fortran
function fixed(v, ndec) result(str)

Arguments

NameTypeIntentAttributesDescription
vreal(kind=R8P)inValue.
ndecinteger(kind=I4P)inNumber of decimals.

Call graph

real_str ​

Finite v with 15 significant digits, as d.ddde<exp> (trailing zeros stripped) or 0.

Readable back by any language to within 1e-15 relative: used for metadata consumed by the interactive viewer.

Attributes: pure

Returns: character(len=:)

fortran
function real_str(v) result(str)

Arguments

NameTypeIntentAttributesDescription
vreal(kind=R8P)inValue.

Call graph

latin1_utf8 ​

text read as ISO 8859-1 (gnuplot's set encoding iso_8859_1), in UTF-8: each byte from 128 a two-byte character.

Attributes: pure

Returns: character(len=:)

fortran
function latin1_utf8(text) result(utf8)

Arguments

NameTypeIntentAttributesDescription
textcharacter(len=*)inISO 8859-1 text.

Call graph

utf8_char ​

UTF-8 encoding of the code point code (at most U+10FFFF; beyond, U+FFFD).

Attributes: pure

Returns: character(len=:)

fortran
function utf8_char(code) result(text)

Arguments

NameTypeIntentAttributesDescription
codeinteger(kind=I4P)inCode point.

Call graph

xml_escape ​

Escape the XML special characters &, <, > and ", and new lines (&#10;: an attribute keeps them).

Attributes: pure

Returns: character(len=:)

fortran
function xml_escape(text) result(escaped)

Arguments

NameTypeIntentAttributesDescription
textcharacter(len=*)inRaw text.

Call graph

format_check ​

Empty if format is a supported tick format, gnuplot's: text with conversions %[flags][width][.precision]type, flags among -+ 0#', all applied to the same value: f e E g G (C's), h H (g with a x10 or *10 superscript exponent), t l (the mantissa to base 10; foresight's log axes are base 10), s (the mantissa to a power of 1000), T L (the power of 10), S (the power of 1000), c (its SI prefix letter), b B (the mantissa to a power of 1024 and its binary prefix), P (multiple of pi), x X o (the integer part in hex or octal); %% writes %. A format without a conversion is its text on every tick, as gnuplot (" " blank labels).

Attributes: pure

Returns: character(len=:)

fortran
function format_check(format) result(message)

Arguments

NameTypeIntentAttributesDescription
formatcharacter(len=*)inFormat.

Call graph

unsigned_digits ​

The integer part of v as C prints a 64-bit unsigned integer with %llx, %llX or %llo; beyond the 64-bit range, x86's conversion result 0x8000000000000000.

Attributes: pure

Returns: character(len=:)

fortran
function unsigned_digits(v, type) result(digits)

Arguments

NameTypeIntentAttributesDescription
vreal(kind=R8P)inValue.
typecharacter(len=1)inx, X or o.

Call graph

c_real ​

C printf of the real v with the conversion %<flags><width>.<prec><type>, type among f F e E g G, flags among -+ 0#, prec -1 if absent (6): glibc's output, from the exact binary value, rounded half to even.

Attributes: pure

Returns: character(len=:)

fortran
function c_real(flags, width, prec, type, v) result(text)

Arguments

NameTypeIntentAttributesDescription
flagscharacter(len=*)inFlags.
widthinteger(kind=I4P)inField width, 0 if none.
precinteger(kind=I4P)inPrecision, -1 if absent.
typecharacter(len=1)inConversion.
vreal(kind=R8P)inValue.

Call graph

c_integer ​

C printf of the integer i with %<flags><width>.<prec><type>, type among d i u o x X c, as gnuplot passes it: a value that fits 32 bits as a C int (so %x of -1 is ffffffff), else as a 64-bit integer.

Attributes: pure

Returns: character(len=:)

fortran
function c_integer(flags, width, prec, type, i) result(text)

Arguments

NameTypeIntentAttributesDescription
flagscharacter(len=*)inFlags.
widthinteger(kind=I4P)inField width, 0 if none.
precinteger(kind=I4P)inPrecision (minimum digits), -1 if absent.
typecharacter(len=1)inConversion.
iinteger(kind=I8P)inValue.

Call graph

c_string ​

C printf of the string s with %<flags><width>.<prec>s: the precision cuts, the width pads.

Attributes: pure

Returns: character(len=:)

fortran
function c_string(flags, width, prec, s) result(text)

Arguments

NameTypeIntentAttributesDescription
flagscharacter(len=*)inFlags.
widthinteger(kind=I4P)inField width, 0 if none.
precinteger(kind=I4P)inMost characters, -1 if absent.
scharacter(len=*)inValue.

Call graph

gnuplot_real ​

A real as gnuplot's print shows it: %.15g, with .0 appended to an integral value (100.0, -0.0), NaN.

Attributes: pure

Returns: character(len=:)

fortran
function gnuplot_real(v) result(text)

Arguments

NameTypeIntentAttributesDescription
vreal(kind=R8P)inValue.

Call graph

scale10 ​

x * 10**e, in steps of at most 10**22 (exact powers of ten) to never overflow an intermediate.

Attributes: pure

Returns: real(kind=R8P)

fortran
function scale10(x, e) result(y)

Arguments

NameTypeIntentAttributesDescription
xreal(kind=R8P)inValue.
einteger(kind=I4P)inDecimal exponent.

Call graph

round_digits ​

The decimal integer digits with its last drop digits removed, rounded half to even; drop <= 0 appends -drop zeros. No leading zeros, except for 0.

Attributes: pure

Returns: character(len=:)

fortran
function round_digits(digits, drop) result(rounded)

Arguments

NameTypeIntentAttributesDescription
digitscharacter(len=*)inDecimal integer.
dropinteger(kind=I4P)inDigits to remove.

Call graph

fixed_digits ​

digits * 10**e with prec decimals (printf %.<prec>f), rounded half to even.

Attributes: pure

Returns: character(len=:)

fortran
function fixed_digits(digits, e, prec) result(text)

Arguments

NameTypeIntentAttributesDescription
digitscharacter(len=*)inDecimal integer, no sign.
einteger(kind=I4P)inExponent.
precinteger(kind=I4P)inDecimals.

Call graph

exp_text ​

C exponent text: letter, sign, at least two digits.

Attributes: pure

Returns: character(len=:)

fortran
function exp_text(letter, x) result(text)

Arguments

NameTypeIntentAttributesDescription
lettercharacter(len=1)ine or E.
xinteger(kind=I4P)inExponent.

Call graph

strip_zeros ​

Remove trailing zeros of a fraction, and the point if nothing remains after it.

Attributes: pure

Returns: character(len=:)

fortran
function strip_zeros(text) result(stripped)

Arguments

NameTypeIntentAttributesDescription
textcharacter(len=*)inNumber without exponent.

Call graph

pad ​

sign and body padded to width as printf: left justified with -, zero filled after the sign with 0 (when zeros allows it), else right justified with blanks.

Attributes: pure

Returns: character(len=:)

fortran
function pad(sign, body, flags, width, zeros) result(text)

Arguments

NameTypeIntentAttributesDescription
signcharacter(len=*)inSign or prefix.
bodycharacter(len=*)inBody.
flagscharacter(len=*)inFlags.
widthinteger(kind=I4P)inField width.
zeroslogicalin0 flag allowed.

Call graph

radix_digits ​

Digits of u read as an unsigned 64-bit integer, bits per digit (3 octal, 4 hexadecimal).

Attributes: pure

Returns: character(len=:)

fortran
function radix_digits(u, bits, symbols) result(text)

Arguments

NameTypeIntentAttributesDescription
uinteger(kind=I8P)inValue (bits).
bitsinteger(kind=I4P)inBits per digit.
symbolscharacter(len=*)inDigit symbols.

Call graph

unsigned_decimal ​

Decimal digits of u read as an unsigned 64-bit integer.

Attributes: pure

Returns: character(len=:)

fortran
function unsigned_decimal(u) result(text)

Arguments

NameTypeIntentAttributesDescription
uinteger(kind=I8P)inValue (bits).

Call graph

upper_case ​

text in upper case (ASCII).

Attributes: pure

Returns: character(len=len)

fortran
function upper_case(text) result(upper)

Arguments

NameTypeIntentAttributesDescription
textcharacter(len=*)inText.

Call graph

dms_label ​

The label of value in degrees by a geographic format, gnuplot's gstrdms (set xtics geographic): %D whole degrees, %d degrees (as %f, with its width and precision), %M whole minutes, %m minutes, %S whole seconds, %s seconds, %E E or W, %N N or S (blank at 0) instead of the sign; the value is first brought within +-180, the minus sign on the first of degrees, minutes and seconds written. A format with none of them is a plain C format of the value. Text before. Text after. Problem.

Attributes: pure

Returns: character(len=:)

fortran
function dms_label(format, value) result(label)

Arguments

NameTypeIntentAttributesDescription
formatcharacter(len=*)inFormat.
valuereal(kind=R8P)inValue [degrees].

Call graph

Released under the GPL v3, BSD 2-Clause, BSD 3-Clause or MIT License.