Next: Miscellaneous output, Previous: Simple numeric output, Up: Other I/O [Contents][Index]
Floating-point output is always displayed using decimal base (even if
BASE is different). The words starting with e. output
the FP number in a format that the text interpreter accepts as input.
f. ( r – ) floating-ext “f-dot”
Display (the floating-point number) r without exponent, followed by a space.
fe. ( r – ) floating-ext “f-e-dot”
Display r using engineering notation (with exponent dividable by 3), followed by a space.
fs. ( r – ) floating-ext “f-s-dot”
Display r using scientific notation (with exponent), followed by a space.
fp. ( r – ) floating-ext “f-p-dot”
Display r using SI prefix notation (with exponent dividable by 3, converted into SI prefixes if available), followed by a space.
e. ( f: r – ) gforth “e-dot”
Print r with the least number of mantissa digits such that
the result, when converted with >float, produces r
(for finite r). Caveat: e. only works as specified
if represent (which calls the C library’s
ecvt_r()) produces the closest mantissa for the given
buffer length; that is the case on not-too-old versions of
glibc.
e.p ( r u – ) gforth-experimental “e-dot-p”
Print r with up to u mantissa digits, leaving away trailing mantissa digits. One additional mantissa digit is displayed if this can avoid showing an exponent.
e.exact ( r – ) gforth-experimental “e-dot-exact”
Print r at full length, with possibly more than 700 mantissa
digits. Caveat: e.exact only provides the exact output
if represent does (which calls the C library’s
ecvt_r()); that is the case on not-too-old versions of
glibc.
Examples of printing the number 1234.5678E23 in the different floating-point output formats are shown below.
1234.5678E23 fdup f. \ 123456780000000000000000000. fdup fe. \ 123.456780000000E24 fdup fs. \ 1.23456780000000E26 fdup fp. \ 123.456780000000Y fdup e. \ 1.2345678e26 ok fdup 5 e.p \ 1.2346e26 ok fdup e.exact \ 123456780000000004618977280e ok fdrop
Note that 1234.5678E23 cannot be represented exactly as IEEE
double-precision (binary64) FP number (Gforth’s float format), so
there is a rounding error when converting that string to a float, and
the output of e.exact shows the exact value of the resulting
number. All the other output words produce a result that rounds the
mantissa to fit in a certain number of digits (and possibly extends
that with trailing zeros).
The length of the output of f. is influenced by:
precision ( – u ) floating-ext
u is the number of significant digits currently used by
F. FE. and FS.
set-precision ( u – ) floating-ext
Set the number of significant digits currently used by
F. FE. and FS. to u.
You can control the output in more detail with:
f.rdp ( rf +nr +nd +np – ) gforth-0.6 “f-dot-rdp”
Print float rf formatted. The total width of the output is
nr. For fixed-point notation, the number of digits after the
decimal point is +nd and the minimum number of significant
digits is np. Set-precision has no effect on
f.rdp. Fixed-point notation is used if the number of
siginicant digits would be at least np and if the number of
digits before the decimal point would fit. If fixed-point notation
is not used, exponential notation is used, and if that does not
fit, asterisks are printed. We recommend using nr>=7 to avoid
the risk of numbers not fitting at all. We recommend
nr>=np+5 to avoid cases where f.rdp switches to
exponential notation because fixed-point notation would have too
few significant digits, yet exponential notation offers fewer
significant digits. We recommend nr>=nd+2, if you want to
have fixed-point notation for some numbers; the smaller the value
of np, the more cases are shown in fixed-point notation (cases
where few or no significant digits remain in fixed-point notation).
We recommend np>nr, if you want to have exponential
notation for all numbers.
To give you a better intuition of how they influence the output, here are some examples of parameter combinations; in each line the same number is printed, in each column the same parameter combination is used for printing:
12 13 0 7 3 4 7 3 0 7 3 1 7 5 1 7 7 1 7 0 2 4 2 1 |-1.234568E-6|-1.2E-6| -0.000|-1.2E-6|-1.2E-6|-1.2E-6|-1.2E-6|****| |-1.234568E-5|-1.2E-5| -0.000|-1.2E-5|-.00001|-1.2E-5|-1.2E-5|****| |-1.234568E-4|-1.2E-4| -0.000|-1.2E-4|-.00012|-1.2E-4|-1.2E-4|****| |-1.234568E-3|-1.2E-3| -0.001| -0.001|-.00123|-1.2E-3|-1.2E-3|****| |-1.234568E-2|-1.2E-2| -0.012| -0.012|-.01235|-1.2E-2|-1.2E-2|-.01| |-1.234568E-1|-1.2E-1| -0.123| -0.123|-.12346|-1.2E-1|-1.2E-1|-.12| |-1.2345679E0| -1.235| -1.235| -1.235|-1.23E0|-1.23E0|-1.23E0|-1E0| |-1.2345679E1|-12.346|-12.346|-12.346|-1.23E1|-1.23E1| -12.|-1E1| |-1.2345679E2|-1.23E2|-1.23E2|-1.23E2|-1.23E2|-1.23E2| -123.|-1E2| |-1.2345679E3|-1.23E3|-1.23E3|-1.23E3|-1.23E3|-1.23E3| -1235.|-1E3| |-1.2345679E4|-1.23E4|-1.23E4|-1.23E4|-1.23E4|-1.23E4|-12346.|-1E4| |-1.2345679E5|-1.23E5|-1.23E5|-1.23E5|-1.23E5|-1.23E5|-1.23E5|-1E5|
Next: Miscellaneous output, Previous: Simple numeric output, Up: Other I/O [Contents][Index]