Floating Point
Floating-point operations are provided by the Floating-Point Coprocessor and use the F.* namespace.
Memory itself is untyped. Floating-point instructions interpret their operands as floating-point values and write their results back to ordinary memory.
Floating-point literals are described in Syntax.
Floating-point memory and stack operations are documented in Memory & Stack:
F.MOVE <dst> <src>
F.SWAP <a> <b>
F.PUSH <value>
F.POP <dst>
Floating-point comparisons are documented in Branching.
Floating-point random numbers are documented in Random Numbers.
Representation
NCL floating-point values use IEEE 754 binary32 representation.
This provides approximately seven decimal digits of precision.
Floating-point arithmetic is therefore not exact. Values produced by calculations may differ slightly from their mathematically exact result.
For example, calculations that theoretically produce the same value may not compare exactly equal.
Use the epsilon-aware floating-point branches when comparing calculated values:
F.BEQ :equal $000 $001 f0.0001
Constants
The Floating-Point Coprocessor provides several built-in constants:
| Constant | Value | Description |
|---|---|---|
#F.PI |
3.14159265 |
π |
#F.TAU |
6.28318531 |
τ, equal to 2π |
#F.E |
2.71828183 |
Euler's number |
#F.R2 |
1.41421356 |
Square root of 2 |
These are ordinary source-level constants and may be used anywhere a floating-point value is accepted:
F.MUL $000 #F.PI f2
Arithmetic
F.ADD — Addition
F.ADD <dst> <lhs> <rhs>
Adds two floating-point values.
Operands
| Position | Name | Type | Description |
|---|---|---|---|
| 1 | dst |
Location | Destination location. |
| 2 | lhs |
Floating-point value | Left operand. |
| 3 | rhs |
Floating-point value | Right operand. |
Modified locations
| Location | Description |
|---|---|
dst |
Sum. |
Examples
F.ADD $000 f1.5 f2.25
-- > $000 = 3.750000
F.SUB — Subtraction
F.SUB <dst> <lhs> <rhs>
Subtracts one floating-point value from another.
Operands
| Position | Name | Type | Description |
|---|---|---|---|
| 1 | dst |
Location | Destination location. |
| 2 | lhs |
Floating-point value | Left operand. |
| 3 | rhs |
Floating-point value | Right operand. |
Modified locations
| Location | Description |
|---|---|
dst |
Difference. |
Examples
F.SUB $000 f10 f3.5
-- > $000 = 6.500000
F.MUL — Multiplication
F.MUL <dst> <lhs> <rhs>
Multiplies two floating-point values.
Operands
| Position | Name | Type | Description |
|---|---|---|---|
| 1 | dst |
Location | Destination location. |
| 2 | lhs |
Floating-point value | Left operand. |
| 3 | rhs |
Floating-point value | Right operand. |
Modified locations
| Location | Description |
|---|---|
dst |
Product. |
Examples
F.MUL $000 f2.5 f4
-- > $000 = 10.000000
F.DIV — Division
F.DIV <dst> <lhs> <rhs>
Divides one floating-point value by another.
Operands
| Position | Name | Type | Description |
|---|---|---|---|
| 1 | dst |
Location | Destination location. |
| 2 | lhs |
Floating-point value | Dividend. |
| 3 | rhs |
Floating-point value | Divisor. |
Modified locations
| Location | Description |
|---|---|
dst |
Quotient. |
Examples
F.DIV $000 f10 f4
-- > $000 = 2.500000
F.MOD — Modulo
F.MOD <dst> <lhs> <rhs>
Returns the floating-point remainder of a division.
Operands
| Position | Name | Type | Description |
|---|---|---|---|
| 1 | dst |
Location | Destination location. |
| 2 | lhs |
Floating-point value | Dividend. |
| 3 | rhs |
Floating-point value | Divisor. |
Modified locations
| Location | Description |
|---|---|
dst |
Remainder. |
Examples
F.MOD $000 f10 f3
-- > $000 = 1.000000
F.POW — Power
F.POW <dst> <base> <exponent>
Raises a floating-point value to a power.
Operands
| Position | Name | Type | Description |
|---|---|---|---|
| 1 | dst |
Location | Destination location. |
| 2 | base |
Floating-point value | Base value. |
| 3 | exponent |
Floating-point value | Exponent. |
Modified locations
| Location | Description |
|---|---|
dst |
Result. |
Examples
Square a value:
F.POW $000 f3 f2
-- > $000 = 9.000000
Calculate a fractional power:
F.POW $000 f16 f0.5
-- > $000 = 4.000000
Notes
An exponent of exactly f0.5 uses the same behavior as F.SQRT.
This means a negative base with an exponent of exactly f0.5 returns the magnitude of the imaginary square root and reports MATH_ERROR.
Other fractional powers of a negative base use the underlying floating-point power operation and may produce nan.
F.SQRT — Square Root
F.SQRT <dst> <value>
Calculates the square root of a floating-point value.
Operands
| Position | Name | Type | Description |
|---|---|---|---|
| 1 | dst |
Location | Destination location. |
| 2 | value |
Floating-point value | Value to evaluate. |
Modified locations
| Location | Description |
|---|---|
dst |
Square root. |
Examples
F.SQRT $000 f25
-- > $000 = 5.000000
For a negative value:
F.SQRT $000 f-25
-- > $000 = 5.000000
-- > er = "MATH_ERROR"
Notes
For a negative input, F.SQRT calculates the square root of its magnitude and sets er to MATH_ERROR.
The returned value is therefore the magnitude of the imaginary square root.
F.NEG — Negate
F.NEG <dst> <value>
Negates a floating-point value.
Operands
| Position | Name | Type | Description |
|---|---|---|---|
| 1 | dst |
Location | Destination location. |
| 2 | value |
Floating-point value | Value to negate. |
Modified locations
| Location | Description |
|---|---|
dst |
Negated value. |
Examples
F.NEG $000 f3.5
-- > $000 = -3.500000
F.ABS — Absolute Value
F.ABS <dst> <value>
Returns the absolute value of a floating-point number.
Operands
| Position | Name | Type | Description |
|---|---|---|---|
| 1 | dst |
Location | Destination location. |
| 2 | value |
Floating-point value | Source value. |
Modified locations
| Location | Description |
|---|---|
dst |
Absolute value. |
Examples
F.ABS $000 f-3.5
-- > $000 = 3.500000
F.MIN — Minimum
F.MIN <dst> <lhs> <rhs>
Stores the smaller of two floating-point values.
Operands
| Position | Name | Type | Description |
|---|---|---|---|
| 1 | dst |
Location | Destination location. |
| 2 | lhs |
Floating-point value | First value. |
| 3 | rhs |
Floating-point value | Second value. |
Modified locations
| Location | Description |
|---|---|
dst |
Smaller value. |
Examples
F.MIN $000 f10.5 f4.25
-- > $000 = 4.250000
F.MAX — Maximum
F.MAX <dst> <lhs> <rhs>
Stores the larger of two floating-point values.
Operands
| Position | Name | Type | Description |
|---|---|---|---|
| 1 | dst |
Location | Destination location. |
| 2 | lhs |
Floating-point value | First value. |
| 3 | rhs |
Floating-point value | Second value. |
Modified locations
| Location | Description |
|---|---|
dst |
Larger value. |
Examples
F.MAX $000 f10.5 f4.25
-- > $000 = 10.500000
F.CLAMP — Clamp
F.CLAMP <dst> <value> <min> <max>
Constrains a floating-point value to a range.
Operands
| Position | Name | Type | Description |
|---|---|---|---|
| 1 | dst |
Location | Destination location. |
| 2 | value |
Floating-point value | Value to constrain. |
| 3 | min |
Floating-point value | Minimum value. |
| 4 | max |
Floating-point value | Maximum value. |
Modified locations
| Location | Description |
|---|---|
dst |
Clamped value. |
Examples
F.CLAMP $000 f12.5 f0 f10
-- > $000 = 10.000000
A value already inside the range is unchanged:
F.CLAMP $000 f7.5 f0 f10
-- > $000 = 7.500000
Notes
The comparisons are performed in order.
If min is greater than max, no error is produced; the result follows the normal comparison sequence.
F.MAP — Map Range
F.MAP <dst> <value> <in_min> <in_max> <out_min> <out_max>
Maps a floating-point value from one range to another.
Operands
| Position | Name | Type | Description |
|---|---|---|---|
| 1 | dst |
Location | Destination location. |
| 2 | value |
Floating-point value | Value to map. |
| 3 | in_min |
Floating-point value | Input range minimum. |
| 4 | in_max |
Floating-point value | Input range maximum. |
| 5 | out_min |
Floating-point value | Output range minimum. |
| 6 | out_max |
Floating-point value | Output range maximum. |
Modified locations
| Location | Description |
|---|---|
dst |
Mapped value. |
Examples
Map the midpoint of one range to another:
F.MAP $000 f0.5 f0 f1 f0 f100
-- > $000 = 50.000000
Values outside the input range are extrapolated:
F.MAP $000 f1.5 f0 f1 f0 f100
-- > $000 = 150.000000
Notes
F.MAP does not clamp the input or output.
If in_min and in_max are equal, the result is out_min and er is set to MATH_ERROR.
Rounding
F.FLOOR — Floor
F.FLOOR <dst> <value>
Rounds a floating-point value downward to the nearest integer value.
Operands
| Position | Name | Type | Description |
|---|---|---|---|
| 1 | dst |
Location | Destination location. |
| 2 | value |
Floating-point value | Value to round. |
Modified locations
| Location | Description |
|---|---|
dst |
Rounded floating-point value. |
Examples
F.FLOOR $000 f3.8
-- > $000 = 3.000000
F.FLOOR $000 f-3.2
-- > $000 = -4.000000
F.CEIL — Ceiling
F.CEIL <dst> <value>
Rounds a floating-point value upward to the nearest integer value.
Operands
| Position | Name | Type | Description |
|---|---|---|---|
| 1 | dst |
Location | Destination location. |
| 2 | value |
Floating-point value | Value to round. |
Modified locations
| Location | Description |
|---|---|
dst |
Rounded floating-point value. |
Examples
F.CEIL $000 f3.2
-- > $000 = 4.000000
F.CEIL $000 f-3.8
-- > $000 = -3.000000
F.ROUND — Round to Modulus
F.ROUND <dst> <value> <modulus>
Rounds a floating-point value to the nearest multiple of a modulus.
The operation is:
floor(value / modulus + 0.5) * modulus
Operands
| Position | Name | Type | Description |
|---|---|---|---|
| 1 | dst |
Location | Destination location. |
| 2 | value |
Floating-point value | Value to round. |
| 3 | modulus |
Floating-point value | Rounding interval. |
Modified locations
| Location | Description |
|---|---|
dst |
Rounded value. |
Examples
Round to the nearest whole value:
F.ROUND $000 f3.6 f1
-- > $000 = 4.000000
Round to the nearest tenth:
F.ROUND $000 f3.14159 f0.1
-- > $000 = 3.100000
Round to the nearest multiple of five:
F.ROUND $000 f17 f5
-- > $000 = 15.000000
Notes
The sign of modulus is preserved and affects the direction of exact ties.
A modulus of zero produces 0.0 and sets er to MATH_ERROR.
Logarithms & Exponents
F.LOG — Logarithm
F.LOG <dst> <value> [base]
Calculates the logarithm of a floating-point value.
If base is omitted, the natural logarithm is returned.
When a base is supplied, the result is calculated as:
ln(value) / ln(base)
Operands
| Position | Name | Type | Description |
|---|---|---|---|
| 1 | dst |
Location | Destination location. |
| 2 | value |
Floating-point value | Value to evaluate. |
| 3 | base |
Floating-point value | Optional logarithm base. |
Modified locations
| Location | Description |
|---|---|
dst |
Logarithm. |
Examples
Calculate a natural logarithm:
F.LOG $000 #F.E
-- > $000 = 1.000000
Calculate a base-10 logarithm:
F.LOG $000 f1000 f10
-- > $000 = 3.000000
F.EXP — Exponential
F.EXP <dst> <value>
Raises Euler's number to a floating-point power.
The operation is equivalent to:
e ^ value
Operands
| Position | Name | Type | Description |
|---|---|---|---|
| 1 | dst |
Location | Destination location. |
| 2 | value |
Floating-point value | Exponent. |
Modified locations
| Location | Description |
|---|---|
dst |
Exponential result. |
Examples
F.EXP $000 f1
-- > $000 = 2.718282
F.EXP $000 f0
-- > $000 = 1.000000
Trigonometry
Trigonometric functions use radians.
F.SIN — Sine
F.SIN <dst> <angle>
Calculates the sine of an angle.
Operands
| Position | Name | Type | Description |
|---|---|---|---|
| 1 | dst |
Location | Destination location. |
| 2 | angle |
Floating-point value | Angle in radians. |
Modified locations
| Location | Description |
|---|---|
dst |
Sine. |
Examples
F.SIN $000 #F.PI
-- > $000 ≈ 0
F.COS — Cosine
F.COS <dst> <angle>
Calculates the cosine of an angle.
Operands
| Position | Name | Type | Description |
|---|---|---|---|
| 1 | dst |
Location | Destination location. |
| 2 | angle |
Floating-point value | Angle in radians. |
Modified locations
| Location | Description |
|---|---|
dst |
Cosine. |
Examples
F.COS $000 #F.PI
-- > $000 ≈ -1
F.TAN — Tangent
F.TAN <dst> <angle>
Calculates the tangent of an angle.
Operands
| Position | Name | Type | Description |
|---|---|---|---|
| 1 | dst |
Location | Destination location. |
| 2 | angle |
Floating-point value | Angle in radians. |
Modified locations
| Location | Description |
|---|---|
dst |
Tangent. |
Examples
F.TAN $000 f0
-- > $000 = 0.000000
F.ASIN — Arcsine
F.ASIN <dst> <value>
Calculates the inverse sine of a floating-point value.
The result is an angle in radians.
Operands
| Position | Name | Type | Description |
|---|---|---|---|
| 1 | dst |
Location | Destination location. |
| 2 | value |
Floating-point value | Source value. |
Modified locations
| Location | Description |
|---|---|
dst |
Angle in radians. |
Examples
F.ASIN $000 f1
-- > $000 ≈ 1.570796
F.ACOS — Arccosine
F.ACOS <dst> <value>
Calculates the inverse cosine of a floating-point value.
The result is an angle in radians.
Operands
| Position | Name | Type | Description |
|---|---|---|---|
| 1 | dst |
Location | Destination location. |
| 2 | value |
Floating-point value | Source value. |
Modified locations
| Location | Description |
|---|---|
dst |
Angle in radians. |
Examples
F.ACOS $000 f1
-- > $000 = 0.000000
F.ATAN — Arctangent
F.ATAN <dst> <value>
Calculates the inverse tangent of a floating-point value.
The result is an angle in radians.
Operands
| Position | Name | Type | Description |
|---|---|---|---|
| 1 | dst |
Location | Destination location. |
| 2 | value |
Floating-point value | Source value. |
Modified locations
| Location | Description |
|---|---|
dst |
Angle in radians. |
Examples
F.ATAN $000 f1
-- > $000 ≈ 0.785398
F.ATAN2 — Two-Argument Arctangent
F.ATAN2 <dst> <y> <x>
Calculates the angle of the point (x, y) from the positive x-axis.
The result is an angle in radians.
Operands
| Position | Name | Type | Description |
|---|---|---|---|
| 1 | dst |
Location | Destination location. |
| 2 | y |
Floating-point value | Y component. |
| 3 | x |
Floating-point value | X component. |
Modified locations
| Location | Description |
|---|---|
dst |
Angle in radians. |
Examples
F.ATAN2 $000 f1 f1
-- > $000 ≈ 0.785398
Hyperbolic Functions
F.SINH — Hyperbolic Sine
F.SINH <dst> <value>
Calculates the hyperbolic sine of a floating-point value.
Operands
| Position | Name | Type | Description |
|---|---|---|---|
| 1 | dst |
Location | Destination location. |
| 2 | value |
Floating-point value | Source value. |
Modified locations
| Location | Description |
|---|---|
dst |
Hyperbolic sine. |
Examples
F.SINH $000 f0
-- > $000 = 0.000000
F.COSH — Hyperbolic Cosine
F.COSH <dst> <value>
Calculates the hyperbolic cosine of a floating-point value.
Operands
| Position | Name | Type | Description |
|---|---|---|---|
| 1 | dst |
Location | Destination location. |
| 2 | value |
Floating-point value | Source value. |
Modified locations
| Location | Description |
|---|---|
dst |
Hyperbolic cosine. |
Examples
F.COSH $000 f0
-- > $000 = 1.000000
F.TANH — Hyperbolic Tangent
F.TANH <dst> <value>
Calculates the hyperbolic tangent of a floating-point value.
Operands
| Position | Name | Type | Description |
|---|---|---|---|
| 1 | dst |
Location | Destination location. |
| 2 | value |
Floating-point value | Source value. |
Modified locations
| Location | Description |
|---|---|
dst |
Hyperbolic tangent. |
Examples
F.TANH $000 f0
-- > $000 = 0.000000
Angle Conversion
F.RAD — Degrees to Radians
F.RAD <dst> <degrees>
Converts an angle from degrees to radians.
Operands
| Position | Name | Type | Description |
|---|---|---|---|
| 1 | dst |
Location | Destination location. |
| 2 | degrees |
Floating-point value | Angle in degrees. |
Modified locations
| Location | Description |
|---|---|
dst |
Angle in radians. |
Examples
F.RAD $000 f180
-- > $000 ≈ 3.141593
F.DEG — Radians to Degrees
F.DEG <dst> <radians>
Converts an angle from radians to degrees.
Operands
| Position | Name | Type | Description |
|---|---|---|---|
| 1 | dst |
Location | Destination location. |
| 2 | radians |
Floating-point value | Angle in radians. |
Modified locations
| Location | Description |
|---|---|
dst |
Angle in degrees. |
Examples
F.DEG $000 #F.PI
-- > $000 ≈ 180.000000