Float syntax
Factor handbook » The language » Syntax » Literals » Number syntax

Prev:Ratio syntax
Next:Complex number syntax


Floating point literals are specified when a literal number contains a decimal point or exponent. Exponents are marked by an e or E:
10.5 -3.1456 7e13 1.0e-5 1.0E+5

Literal numbers without a decimal point or an exponent always parse as integers:
1 float? .
f

1. float? .
t

1e0 float? .
t

Literal floating point approximations of ratios can also be input by placing a decimal point in the denominator:
1/2. .
0.5

1/3. .
0.3333333333333333

1/0.5 .
2.0

1/2.5 .
0.4

1+1/2. .
1.5

1+1/2.5 .
1.4

The special float values have their own syntax:
Positive infinity1/0.
Negative infinity-1/0.
Not-a-number (positive)0/0.
Not-a-number (negative)-0/0.

A Not-a-number literal with an arbitrary payload can also be input:
NAN:


To see the 64 bit value of 0/0. on your platform, execute the following code :
USING: io math math.parser ; "NAN: " write 0/0. double>bits >hex print

Hexadecimal, octal and binary float literals are also supported. These consist of a hexadecimal, octal or binary literal with a decimal point and a mandatory base-two exponent expressed as a decimal number after p or P:
8.0 0x1.0p3 = .
t

-1024.0 -0x1.0P10 = .
t

10.125 0x1.44p3 = .
t

10.125 0b1.010001p3 = .
t

10.125 0o1.21p3 = .
t

The normalized hex form ±0x1.MMMMMMMMMMMMMp±EEEE allows any floating-point number to be specified precisely. The values of MMMMMMMMMMMMM and EEEE map directly to the mantissa and exponent fields of the binary IEEE 754 representation.

More information on floats can be found in Floats.