Data Types
The data type of a variable determines the internal storage format of the data in the computer's memory. You can specify a data type for a variable when you declare it, and you can convert a variable's data type to another data type. All variables have a data type.
By default, if you don't specify a data type, the variable is given the list data type. The list data type can contain any number of values which can be of any data type.
For variables that are known to have a particular data type, it is more efficient to declare them with that type.
Data types apply to other things besides variables. For example, data types apply to the arguments of methods and their return values.
You can also declare vectors of any of the fundamental data types.
Fundamental Data Types
HyperScript has a set of fundamental data types corresponding to the most common ways of representing data in a computer:
- A Boolean type (boolean)
- Character types (such as str)
- Integer types (such as int)
- Floating-point types (such as float)
From these types you can construct other types:
- Lists of any types (list)
- Arrays of the same type (vector)
- Data structures (Structures)
The integer and floating-point types are provided in a variety of sizes so that you can specify the most appropriate storage, precision, and range available for computations. For most applications, you would simply use boolean for logical values, str for string (character) values, int for integer values, and float for floating-point values. The remaining fundamental types are variations for optimizations and special needs.
Boolean Type
A Boolean, boolean, can have one of the two values 1 (true) or 0 (false). A Boolean is used to express the result of logical operations. For example:
f ( int x, int y )
{
boolean b = ( x == y );
/* ... */
}
If x an y have the same value, b becomes 1; otherwise b becomes 0.
Character Types
A variable of type str can hold up to 64 ASCII characters. For example:
str s = "this is a string";
A variable of type char holds the integer value of the ASCII character. For example:
char c = "a";
is the integer representation of the character 'a'. The conversion of a character to an integer results in one of 256 values (because char is an 8-bit byte). HyperScript provides both a signed and unsigned character type for representing the values of char. The signed char can hold the values -128 to 127, while the unsigned uchar can hold the values 0 to 255.
Integer Types
Integer types come in two forms: signed, and unsigned, and four sizes: byte, short, int, and long. (An int does not have an unsigned form.) The unsigned integer types: ubyte, ushort, and ulong, are ideal for use as bit-array variables. Care must be taken when converting between these types since they each hold a different range of values.
Binary Types
Binary types are useful for storing data in bit patterns, and for bit-arrays. The binary data type is used to represent single byte hexadecimal values. For example:
binary b = 0xab;
The hex data type represents full size (size of int) hexadecimal numbers. For example:
hex x = 0x1234cdef;
Finally, the octal data type is used to hold octal values. For example:
octal o = 0o1234567;
Floating-Point Types
Floating-point types come in two sizes: float (single-precision), and double (double-precision). While choice of precision is problem specific, all floating-point calculations are carried out in double-precision regardless of operand type. Thus, single-precision variables receive rounded-off results from floating-point calculations.
Handle Type
The handle data type is used in file I/O operations. It is used to store the handle returned by the fopen method:
handle h = fopen ( "filename", "w" );
Calls to other I/O functions require the file handle as an argument.
Sizes
In general, sizes of fundamental types are machine dependent, and it is unwise to make any assumptions in this regard when developing a program. With this caveat, the following table presents a plausible set of fundamental types, their sizes, and the minimum and maximum values possible.
| Data Type | Size and Representation | Range | |
|---|---|---|---|
| Min | Max | ||
| int | 4 byte signed integer | -2147483648 | 2147483647 |
| long | 4 byte signed integer | -2147483648 | 2147483647 |
| ulong | 4 byte unsigned integer | 0 | 4294967295 |
| short | 2 byte signed integer | -32768 | 32767 |
| ushort | 2 byte unsigned integer | 0 | 65535 |
| byte | signed byte | -128 | 127 |
| ubyte | unsigned byte | 0 | 255 |
| char | signed char (byte) | -128 | 127 |
| uchar | unsigned char (byte) | 0 | 255 |
| binary | unsigned byte | 0x0 | 0xff |
| hex | 4 byte unsigned integer | 0x0 | 0xffffffff |
| octal | 4 byte unsigned integer | 0o0 | 0o37777777777 |
| boolean | unsigned byte | 0 | 1 |
| handle | pointer (4 bytes) | n/a | n/a |
| float | 4 byte floating point decimal | +/-3.40282347E+38 | +/-1.40239846E-45 |
| double | 8 byte floating point decimal | +/-1.7976931348623157E+308 | +/-4.940656458412465E-324 |
| str | variable length character string | "" | 64 characters |