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.

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
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, as C printf would to that decimal, rounding half to even. sup is the exponent of the %h form.

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

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

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

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

xml_escape ​

Escape the XML special characters &, <, > and ".

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: text with one conversion %[flags][width][.precision]type, flags among -+ 0, type f, e, E, g, G or h (gnuplot: g with a x10 superscript exponent), and %% for %.

Attributes: pure

Returns: character(len=:)

fortran
function format_check(format) result(message)

Arguments

NameTypeIntentAttributesDescription
formatcharacter(len=*)inFormat.

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