Data Types

Table of Contents

  1. NAP Data-types
  2. Data-type of Constants
  3. Data-type Conversion Functions
  4. Data-type of result of operation

NAP Data-types

A NAO can have any of the following data-types:

Name Description
c8 8-bit character
i8 8-bit signed integer
i16 16-bit signed integer
i32 32-bit signed integer
u8 8-bit unsigned integer
u16 16-bit unsigned integer
u32 32-bit unsigned integer
f32 32-bit floating-point
f64 64-bit floating-point
ragged compressed
boxed slot numbers

The ragged type provides an efficient way of storing arrays with many missing values around the edges.

A boxed NAO contains slot numbers pointing to other NAOs. This allows one to construct arrays (normally vectors) of arrays. Boxed vectors are generated by the Link Operators "..." and ",". Boxed vectors are used

Boxed vectors can be unpacked using function open_box(x), which is described in Functions related to Special Data-types.

Data-type of Constants

A scalar constant can contain a data-type suffix, as in:

% [nap "3.7f32"] all
::NAP::53-53  f32  MissingValue: NaN  References: 0
Value:
3.7
% [nap "123u8"] all
::NAP::55-55  u8  MissingValue: (NULL)  References: 0
Value:
123
The following examples show that if there is no such suffix then floating-point values are treated as f64 while integer values are treated as i32:
% [nap "3.7"] all
::NAP::57-57  f64  MissingValue: NaN  References: 0
Value:
3.7
% [nap "123"] all
::NAP::58-58  i32  MissingValue: -2147483648  References: 0
Value:
123

Data-type Conversion Functions

There is a function with the name of each data-type except ragged and boxed. Each such function converts its argument to that data-type. For example:
% [nap "f64(123)"] all
::NAP::62-62  f64  MissingValue: NaN  References: 0
Value:
123
% [nap "c8(123)"] all; # ascii character 123
::NAP::66-66  c8  MissingValue: (NULL)  References: 0
Value:
{
% [nap "f32({123 -1.2 0})"] all; # vector with 3 elements
::NAP::70-70  f32  MissingValue: NaN  References: 0
Dimension 0   Size: 3      Name: (NULL)    Coordinate-variable: (NULL)
Value:
123 -1.2 0

The parentheses () in these three examples are not needed. Deleting them:

% [nap "f64 123"] all
::NAP::26-26  f64  MissingValue: NaN  References: 0
Value:
123
% [nap "c8 123"] all
::NAP::29-29  c8  MissingValue: (NULL)  References: 0
Value:
{
% [nap "f32{123 -1.2 0}"] all
::NAP::33-33  f32  MissingValue: NaN  References: 0
Dimension 0   Size: 3      Name: (NULL)    Coordinate-variable: (NULL)
Value:
123 -1.2 0

Data-type of result of operation

Many operations (defined by an operator or a function) produce a result whose data-type matches that of their operands/arguments (if these all have the same data-type). The following examples illustrate this for the subtract operator:

% [nap "3 - 1"] all
::NAP::57-57  i32  MissingValue: -2147483648  References: 0  Unit: (NULL)
Value:
2
% [nap "3f32 - 1f32"] all
::NAP::61-61  f32  MissingValue: NaN  References: 0  Unit: (NULL)
Value:
2
% [nap "3u8 - 1u8"] all
::NAP::65-65  u8  MissingValue: 255  References: 0  Unit: (NULL)
Value:
2

What happens if the operands differ in data-type? Let's try adding f32 and f64 values:

% [nap "234f32 + 3.5f64"] all
::NAP::40-40  f64  MissingValue: NaN  References: 0
Value:
237.5
The result is of type f64, so there is no loss of precision. Next let's try adding i32 and f32 values:
% [nap "234 + 3.5f32"] all
::NAP::54-54  f64  MissingValue: NaN  References: 0  Unit: (NULL)
Value:
237.5

Why is the result f64 rather than f32? This prevents possible loss of precision, as in:

% [nap "f32(123456789) + 5f32"] all -format %d
::NAP::48-48  f32  MissingValue: NaN  References: 0  Unit: (NULL)
Value:
123456800
This is due to the fact that an i32 value has 31 bits (9.3 digits) of precision, whereas an f32 value has only 24 bits (7.2 digits) of precision. So both operands must be converted to f64 before the addition takes place. This is shown by:
% [nap "123456789 + 5f32"] all -format %d
::NAP::43-43  f64  MissingValue: NaN  References: 0  Unit: (NULL)
Value:
123456794

Some operations produce a result whose data-type is independent of the types of the operands. In particular, relational and logical operators always produce an i8 result with value 1 for true and 0 for false. The following illustrates the ">" operator:

% [nap "9 > 8"] all
::NAP::43-43  i8  MissingValue: (NULL)  References: 0
Value:
1

Author: Harvey Davies       © 2002, CSIRO Australia.       Legal Notice and Disclaimer
CVS Version Details: $Id: data_type.html,v 1.5 2005/02/02 00:41:43 dav480 Exp $