Skip to content

format

builtin · libnexusinterp/corefcn/pr-output-proprietary.cc

: format
: format options
: format (options)
: [format, formatspacing, uppercase] = format

Query or set how disp and the interpreter’s automatic echoing lay out numeric values.

The setting governs presentation only; it does not alter how numbers are stored or computed. Use a conversion function such as single, uint8, or int64 to change the stored type. Whenever an option changes the count of significant digits shown, that count is also visible through output_precision.

Out of the box, Nexus prints about 5 significant digits in a readable layout (the ‘short’, ‘lowercase’, and ‘loose’ options). Calling format with no option, or with ‘default’, returns to this baseline.

The floating-point layout options are:

default

Return to the baseline layout described above.

short

Fixed-point layout with about 5 significant figures; this is the default.

long

Fixed-point layout with about 16 significant figures.

Like ‘short’, this falls back to an exponential ‘e’ layout when a matrix cannot be shown acceptably in fixed point.

shorte
longe

Exponential layout, where each value is written as a mantissa times a power of ten. The mantissa carries 5 significant digits in the short variant; in the long variant, doubles get 16 significant digits and singles get 8. For instance, ‘shorte’ shows pi as 3.1416e+00. The trailing ‘e’ may instead be passed as a separate argument.

shortg
longg

Pick whichever of fixed-point or exponential layout suits each value’s magnitude best. For example, ‘shortg’ shows pi .^ [2; 4; 8; 16; 32] as

ans =

      9.8696
      97.409
      9488.5
  9.0032e+07
  8.1058e+15

The trailing ‘g’ may instead be passed as a separate argument.

shorteng
longeng

Like ‘shorte’ or ‘longe’, but in engineering style, with the exponent constrained to a multiple of 3. For example, ‘shorteng’ shows 10 * pi as 31.416e+00. The trailing ‘eng’ may instead be passed as a separate argument.

free
none

Print values without aligning matrix columns on the decimal point. This raw layout mirrors the C++ expression std::cout << variable, giving roughly 6 significant digits and dropping needless precision such as trailing zeros on integers. Complex values appear as a parenthesized pair, (0.60419, 0.60709), rather than as 0.60419 + 0.60709i.

The next option applies to every numeric type, floating point and integer alike.

"+"
"+" "chars"
plus
plus chars

Reduce each element to a single symbol: ‘+’ for a positive element, ‘-’ for a negative one, and a space for zero. This gives a quick picture of a large matrix’s sparsity pattern, though spy draws that pattern more clearly for very large matrices.

Supplying chars, a string of exactly three characters, overrides the symbols used for positive, negative, and zero entries in that order. For example, under "+" "+-." the matrix [1, 0, -1; -1, 0, 1] prints as

ans =

+.-
-.+
bank

Show values as currency amounts, in fixed point with two digits after the decimal point. Only the real part is printed, since an imaginary part is meaningless for money.

bit

Print each number’s bits, most significant bit first, regardless of storage order. For example, pi prints as:

0 10000000000 1001001000011111101101010100010001000010110100011000

with spaces inserted here to separate the sign bit, the 11-bit exponent, and the 52-bit mantissa, which together encode pi as an IEEE 754 double in normal form. Single-precision values work the same way.

native-bit

Print each number’s bits in the machine’s own byte order. On a big-endian host this matches format bit above; on a little-endian host the bytes come out reversed, although the bits inside each byte still read most significant first.

On x86-64, for example, pi appears as:

00011000 00101101 01000100 01010100 11111011 00100001 00001001 01000000

again with spaces added for readability; comparing it with the format bit output above shows how the bytes are grouped and reordered.

hex

As format bit, but with the bits collapsed four at a time into hexadecimal digits for compactness. Thus pi becomes:

400921fb54442d18
native-hex

As format native-bit, but with the bits collapsed four at a time into hexadecimal digits. Thus pi on x86-64 becomes:

182d4454fb210940
rat

Approximate each value by a ratio of small integers and print that fraction. For example, ‘rat’ shows pi as 355/113.

The next two options control the lettering used in scientific and hexadecimal output.

lowercase (default)

Write the exponent marker in scientific notation as a lowercase ‘e’ and the hex digits for 10-15 as lowercase ‘a-f’.

uppercase

Write the exponent marker as an uppercase ‘E’ and the hex digits for 10-15 as uppercase ‘A-F’.

The next two options control the vertical spacing in matrix output.

compact

Drop the blank lines around column-number headers and between matrices, fitting more data on the screen.

loose (default)

Keep the blank lines above and below column-number headers and between matrices for a more readable, less dense layout.

When several conflicting options are given at once, the last one takes effect, except that ‘default’ overrides everything else. If any option is invalid the current settings are left untouched.

Called with no inputs and one to three outputs, format instead reports the present numeric layout, the matrix spacing, and the lettering preference. Requesting outputs while also passing inputs is an error.