Rexx Programming/How to Rexx/number

In rexx, variables are typeless and are treated as variable length strings. When a number is assigned to a variable, the string that represents the number is stored in the variable. In Rexx, numbers are simply a string of one or more digits with an optional decimalpoint.

Notations

edit

Numbers can be represented using a variety of common notations:

Integer Representation

edit

An integer is a positive or negative whole number without a fractional part. In Rexx, we write integers much as we would write them in daily life, as a sequence of digits with an optional positive or negative sign out front.

/* Here are some positive integers. */
a = 5135
b = 51
c = +6
/* Here are some negative integers. */
d = -5
e = -573
/* The answer on the screen will be 46 */
say d+b

Decimal Representation

edit

Rexx numbers may also have fractional parts expressed using the decimal point.

pi = 3.14159
centimeters_per_foot = 30.48
absolute_zero = -273.15

Floating Point Representation

edit

Exponential floating point numbers may be represented in either scientific notation or engineering notation. The default floating point representation is scientific notation.

parse numeric my_numeric_settings
say my_numeric_settings
9 0 SCIENTIFIC

Bases

edit

By default, numbers are expressed in decimal (base 10). In rexx, it is possible to express numbers in other bases.

Rexx has built-in functions to convert decimals from binary to hexadecimal, hexadecimal to decimal, decimal to hexadecimal, and hexadecimal to binary.

dec# = 4093
bin# = 1011
hex# = 7ac0
say d2x(dec#)       /* decimal to hex */
say x2b(hex#)       /* hex to binary */
say x2d(hex#)       /* hex to decimal */
say b2x(bin#)       /* binary to hex */
say x2d(b2x(bin#))  /* binary to decimal */