6.20.3 Floating-point output

Floating-point output is always displayed using base 10.

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-e-dot”

Display r using SI prefix notation (with exponent dividable by 3, converted into SI prefixes if available), followed by a space.

Examples of printing the number 1234.5678E23 in the different floating-point output formats are shown below.

f. 123456780000000000000000000.
fe. 123.456780000000E24
fs. 1.23456780000000E26
fp. 123.456780000000Y

The length of the output is influenced by:

precision ( – u  ) floating-ext “precision”

u is the number of significant digits currently used by F. FE. and FS.

set-precision ( u –  ) floating-ext “set-precision”

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

You can generate a string instead of displaying the number with:

f>str-rdp ( rf +nr +nd +np – c-addr nr  ) gforth-0.6 “f>str-rdp”

Convert rf into a string at c-addr nr. The conversion rules and the meanings of nr +nd np are the same as for f.rdp. The result in in the pictured numeric output buffer and will be destroyed by anything destroying that buffer.

f>buf-rdp ( rf c-addr +nr +nd +np –  ) gforth-0.6 “f>buf-rdp”

Convert rf into a string at c-addr nr. The conversion rules and the meanings of nr nd np are the same as for f.rdp.

There is also a primitive used for implementing higher-level FP-to-string words:

represent ( r c-addr u – n f1 f2 ) floating “represent”

Convert the decimal significand (aka mantissa) of r into a string in buffer c-addr u; n is the exponent, f1 is true if r is negative, and f2 is true if r is valid (a finite number in Gforth).