Numeric Data Types (GNU Octave (version 11.1.0))

## 4 Numeric Data Types ¶

A numeric constant may be a scalar, a vector, or a matrix, and it may contain complex values.

The simplest form of a numeric constant, a scalar, is a single number. Note that by default numbers are represented within Octave by IEEE 754 double precision (binary64) floating-point format (complex constants are stored as pairs of binary64 values). It is, however, possible to represent true integers as described in Integer Data Types.

If the numeric constant is a real integer, it can be defined in decimal, hexadecimal, or binary notation. An important difference, however, is that decimal constants will be stored as binary64 values while using hexadecimal or binary notation will result in a true integer with a storage class just large enough to hold the specified number. Hexadecimal notation starts with ‘`0x`’ or ‘`0X`’, binary notation starts with ‘`0b`’ or ‘`0B`’, otherwise decimal notation is assumed. As a consequence, ‘`0b`’ is not a hexadecimal number, in fact, it is not a valid number at all.

For better readability, digits may be partitioned by the underscore separator ‘`_`’, which is ignored by the Octave interpreter. Here are some examples of real-valued integer constants, which all represent the same value:

    42            # decimal notation, binary64
    0x2A          # hexadecimal notation
    0b101010      # binary notation
    0b10_1010     # underscore notation
    round (42.1)  # also binary64

In decimal notation, the numeric constant may be denoted as decimal fraction or even in scientific (exponential) notation. Note that this is not possible for hexadecimal or binary notation. Again, in the following example all numeric constants represent the same value:

    .105
    1.05e-1
    .00105e+2

Unlike most programming languages, complex numeric constants are denoted as the sum of real and imaginary parts. The imaginary part is denoted by a real-valued numeric constant followed immediately by a complex value indicator (‘`i`’, ‘`j`’, ‘`I`’, or ‘`J`’ which represents `sqrt (-1)`). No spaces are allowed between the numeric constant and the complex value indicator. All complex values are stored as pairs of binary64 values and the use of hexadecimal or binary notation does not result in a true integer. Some examples of complex numeric constants that all represent the same value:

    3 + 42i
    3 + 42j
    3 + 42I
    3 + 42J
    3.0 + 42.0i
    3.0 + 0x2Ai
    3.0 + 0b10_1010i
    0.3e1 + 420e-1i

`y =` double `(x)` ¶

  Convert x to double precision type.

  See also: single.

`z =` complex `(x)` ¶
`z =` complex `(re, im)` ¶

  Return a complex value from real arguments.

  With 1 real argument x, return the complex result `x + 0i`.

  With 2 real arguments, return the complex result `re + imi`. `complex` can often be more convenient than expressions such as `a + b*i`. For example:

      complex ([1, 2], [3, 4])
        ⇒  [ 1 + 3i   2 + 4i ]

  See also: real, imag, iscomplex, abs, arg.

Octave automatically "narrows" complex types to real types when the imaginary part is zero. The interpreter performs this narrowing whenever it gets the opportunity, in order to save memory, and to emulate MATLAB behavior. For example, if a variable initially has the complex value `4 + 2i` and subsequently the value `2i` is subtracted from it, then it becomes the real type `4` and not the complex type `4 + 0i`. Testing whether the resultant type is complex using the expression `iscomplex (4+2i - 2i)` returns false.

To ensure that a value is of complex type even with a zero imaginary part, use `complex (4, 0)` instead of `4 + 0i`. But even then, the interpreter will still narrow the type to real at the next change done to it (such as adding 1 to it), unless its imaginary part truly becomes nonzero.