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
- real_from_decimal
- decimal_of
- parse_decimal
- format_decimal
- format_value
- classic
- mant_exp
- next_conversion
- split_format
- exact_decimal
- exp_digits
- limbs_multiply
- int_str
- decimal_str
- fixed
- real_str
- latin1_utf8
- utf8_char
- xml_escape
- format_check
- unsigned_digits
- c_real
- c_integer
- c_string
- gnuplot_real
- scale10
- round_digits
- fixed_digits
- exp_text
- strip_zeros
- pad
- radix_digits
- unsigned_decimal
- upper_case
- dms_label
Variables
| Name | Type | Attributes | Description |
|---|---|---|---|
LIMB | integer(kind=I8P) | parameter | Base of the exact decimal digits. |
PI | real(kind=R8P) | parameter | C's M_PI (gnuplot's %P). |
FIXED_CLAMP | real(kind=R8P) | parameter | Magnitude 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.
subroutine real_from_decimal(text, value, ok)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
text | character(len=*) | in | Decimal text. | |
value | real(kind=R8P) | out | Value. | |
ok | logical | out | Well 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
subroutine decimal_of(v, n, e)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
v | real(kind=R8P) | in | Value. | |
n | integer(kind=I8P) | out | Mantissa. | |
e | integer(kind=I4P) | out | Exponent. |
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
subroutine parse_decimal(text, n, e, ok)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
text | character(len=*) | in | Number. | |
n | integer(kind=I8P) | out | Mantissa. | |
e | integer(kind=I4P) | out | Exponent. | |
ok | logical | out | Well 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
subroutine format_decimal(format, n, e, label, sup)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
format | character(len=*) | in | Format. | |
n | integer(kind=I8P) | in | Mantissa. | |
e | integer(kind=I4P) | in | Exponent. | |
label | character(len=:) | out | allocatable | Label. |
sup | character(len=:) | out | allocatable | Superscript, 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
subroutine format_value(format, n, e, log10_base, label, sup)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
format | character(len=*) | in | Format. | |
n | integer(kind=I8P) | in | Mantissa. | |
e | integer(kind=I4P) | in | Exponent. | |
log10_base | real(kind=R8P) | in | log10 of the base of %l and %L. | |
label | character(len=:) | out | allocatable | Label. |
sup | character(len=:) | out | allocatable | Superscript, 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
subroutine classic(flags, width, prec, type, n, e, piece, sup)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
flags | character(len=*) | in | Flags. | |
width | integer(kind=I4P) | in | Field width, 0 if none. | |
prec | integer(kind=I4P) | in | Precision, -1 if absent. | |
type | character(len=1) | in | Conversion. | |
n | integer(kind=I8P) | in | Mantissa. | |
e | integer(kind=I4P) | in | Exponent. | |
piece | character(len=:) | out | allocatable | Converted text. |
sup | character(len=:) | out | allocatable | Superscript 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
subroutine mant_exp(log10_base, x, scientific, prec, m, p)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
log10_base | real(kind=R8P) | in | log10 of the base. | |
x | real(kind=R8P) | in | Value. | |
scientific | logical | in | Power of 1000. | |
prec | integer(kind=I4P) | in | Decimals of the mantissa. | |
m | real(kind=R8P) | out | Mantissa, signed. | |
p | integer(kind=I4P) | out | Power. |
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
subroutine next_conversion(format, i, text, flags, width, prec, type, message)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
format | character(len=*) | in | Format. | |
i | integer(kind=I4P) | inout | Position. | |
text | character(len=:) | out | allocatable | Literal text. |
flags | character(len=:) | out | allocatable | Flags. |
width | integer(kind=I4P) | out | Field width, 0 if absent. | |
prec | integer(kind=I4P) | out | Precision, -1 if absent. | |
type | character(len=1) | out | Conversion type, blank at the end. | |
message | character(len=:) | out | allocatable | Problem, empty if none. |
Call graph
split_format
Split a tick format into its text around the one conversion and the conversion fields.
Attributes: pure
subroutine split_format(format, prefix, flags, width, prec, type, suffix, message)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
format | character(len=*) | in | Format. | |
prefix | character(len=:) | out | allocatable | Text before the conversion, %% resolved. |
flags | character(len=:) | out | allocatable | Flags. |
width | integer(kind=I4P) | out | Field width, 0 if absent. | |
prec | integer(kind=I4P) | out | Precision, -1 if absent. | |
type | character(len=1) | out | Conversion type. | |
suffix | character(len=:) | out | allocatable | Text after the conversion, %% resolved. |
message | character(len=:) | out | allocatable | Problem, 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
subroutine exact_decimal(v, digits, e)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
v | real(kind=R8P) | in | Value. | |
digits | character(len=:) | out | allocatable | Decimal digits. |
e | integer(kind=I4P) | out | Decimal exponent. |
Call graph
exp_digits
Mantissa of digits * 10**e in exponent form with prec decimals (printf %.<prec>e), and its exponent.
Attributes: pure
subroutine exp_digits(digits, e, prec, mantissa, x)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
digits | character(len=*) | in | Decimal integer, no sign. | |
e | integer(kind=I4P) | in | Exponent. | |
prec | integer(kind=I4P) | in | Decimals of the mantissa. | |
mantissa | character(len=:) | out | allocatable | Mantissa. |
x | integer(kind=I4P) | out | Exponent 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
subroutine limbs_multiply(limbs, n, f)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
limbs | integer(kind=I8P) | inout | Digits. | |
n | integer(kind=I4P) | inout | Limbs in use. | |
f | integer(kind=I8P) | in | Factor. |
Call graph
Functions
int_str
Integer to minimal-width decimal string.
Attributes: pure
Returns: character(len=:)
function int_str(n) result(str)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
n | integer(kind=I8P) | in | Integer. |
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=:)
function decimal_str(n, ndec) result(str)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
n | integer(kind=I8P) | in | Integer mantissa. | |
ndec | integer(kind=I4P) | in | Number 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=:)
function fixed(v, ndec) result(str)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
v | real(kind=R8P) | in | Value. | |
ndec | integer(kind=I4P) | in | Number 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=:)
function real_str(v) result(str)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
v | real(kind=R8P) | in | Value. |
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=:)
function latin1_utf8(text) result(utf8)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
text | character(len=*) | in | ISO 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=:)
function utf8_char(code) result(text)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
code | integer(kind=I4P) | in | Code point. |
Call graph
xml_escape
Escape the XML special characters &, <, > and ", and new lines ( : an attribute keeps them).
Attributes: pure
Returns: character(len=:)
function xml_escape(text) result(escaped)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
text | character(len=*) | in | Raw 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=:)
function format_check(format) result(message)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
format | character(len=*) | in | Format. |
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=:)
function unsigned_digits(v, type) result(digits)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
v | real(kind=R8P) | in | Value. | |
type | character(len=1) | in | x, 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=:)
function c_real(flags, width, prec, type, v) result(text)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
flags | character(len=*) | in | Flags. | |
width | integer(kind=I4P) | in | Field width, 0 if none. | |
prec | integer(kind=I4P) | in | Precision, -1 if absent. | |
type | character(len=1) | in | Conversion. | |
v | real(kind=R8P) | in | Value. |
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=:)
function c_integer(flags, width, prec, type, i) result(text)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
flags | character(len=*) | in | Flags. | |
width | integer(kind=I4P) | in | Field width, 0 if none. | |
prec | integer(kind=I4P) | in | Precision (minimum digits), -1 if absent. | |
type | character(len=1) | in | Conversion. | |
i | integer(kind=I8P) | in | Value. |
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=:)
function c_string(flags, width, prec, s) result(text)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
flags | character(len=*) | in | Flags. | |
width | integer(kind=I4P) | in | Field width, 0 if none. | |
prec | integer(kind=I4P) | in | Most characters, -1 if absent. | |
s | character(len=*) | in | Value. |
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=:)
function gnuplot_real(v) result(text)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
v | real(kind=R8P) | in | Value. |
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)
function scale10(x, e) result(y)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
x | real(kind=R8P) | in | Value. | |
e | integer(kind=I4P) | in | Decimal 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=:)
function round_digits(digits, drop) result(rounded)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
digits | character(len=*) | in | Decimal integer. | |
drop | integer(kind=I4P) | in | Digits to remove. |
Call graph
fixed_digits
digits * 10**e with prec decimals (printf %.<prec>f), rounded half to even.
Attributes: pure
Returns: character(len=:)
function fixed_digits(digits, e, prec) result(text)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
digits | character(len=*) | in | Decimal integer, no sign. | |
e | integer(kind=I4P) | in | Exponent. | |
prec | integer(kind=I4P) | in | Decimals. |
Call graph
exp_text
C exponent text: letter, sign, at least two digits.
Attributes: pure
Returns: character(len=:)
function exp_text(letter, x) result(text)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
letter | character(len=1) | in | e or E. | |
x | integer(kind=I4P) | in | Exponent. |
Call graph
strip_zeros
Remove trailing zeros of a fraction, and the point if nothing remains after it.
Attributes: pure
Returns: character(len=:)
function strip_zeros(text) result(stripped)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
text | character(len=*) | in | Number 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=:)
function pad(sign, body, flags, width, zeros) result(text)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
sign | character(len=*) | in | Sign or prefix. | |
body | character(len=*) | in | Body. | |
flags | character(len=*) | in | Flags. | |
width | integer(kind=I4P) | in | Field width. | |
zeros | logical | in | 0 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=:)
function radix_digits(u, bits, symbols) result(text)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
u | integer(kind=I8P) | in | Value (bits). | |
bits | integer(kind=I4P) | in | Bits per digit. | |
symbols | character(len=*) | in | Digit symbols. |
Call graph
unsigned_decimal
Decimal digits of u read as an unsigned 64-bit integer.
Attributes: pure
Returns: character(len=:)
function unsigned_decimal(u) result(text)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
u | integer(kind=I8P) | in | Value (bits). |
Call graph
upper_case
text in upper case (ASCII).
Attributes: pure
Returns: character(len=len)
function upper_case(text) result(upper)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
text | character(len=*) | in | Text. |
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=:)
function dms_label(format, value) result(label)Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
format | character(len=*) | in | Format. | |
value | real(kind=R8P) | in | Value [degrees]. |
Call graph