cast (object schema -- convertedValue)
Inputs
object: Cond or numeric object to convert.schema: Numeric schema selector.
Outputs
convertedValue: Converted numeric object.
cast converts a Cond or numeric object to the selected numeric schema.
The schema input is used only as a numeric schema selector. Its value is not read.
Behavior
- Cond converts to numeric through
TRUE = 1andFALSE = 0, then follows the selected numeric schema. - Whole-to-Whole conversion extends, truncates, or preserves and reinterprets the bit pattern according to source width, target width, and source signedness.
- Whole-number to Real conversion converts numerically to the selected real schema.
- Real to Whole conversion converts numerically to the selected Whole schema and truncates toward zero.
- Real to Real conversion widens or narrows precision as needed.
- Known input objects are cast at compile time.
- Unknown inputs produce unknown values in the selected schema.
Example
{} 0 {} [
"-- schema selector value ignored --" printCompilerMessage
7 0n8 cast printStack _:;
7 255n8 cast printStack _:;
targetNat: 255n8 dynamic;
7 targetNat cast printStack _:;
TRUE 0i16 cast printStack _:;
TRUE -1i16 cast printStack _:;
FALSE 0i16 cast printStack _:;
FALSE -1i16 cast printStack _:;
"-- whole to whole width and signedness --" printCompilerMessage
-1 0n8 cast printStack _:;
255n8 0i8 cast printStack _:;
-1i8 0i16 cast printStack _:;
255n8 0n16 cast printStack _:;
"-- real to whole truncates toward zero --" printCompilerMessage
7.9r32 0 cast printStack _:;
-7.9r32 0 cast printStack _:;
"-- unknown value --" printCompilerMessage
value: 0 dynamic;
value 123.0r32 cast printStack _:;
0
] "main" exportFunction
Expected Output During Compilation
-- schema selector value ignored --
7n8
7n8
7n8
1i16
1i16
0i16
0i16
-- whole to whole width and signedness --
255n8
-1i8
-1i16
255n16
-- real to whole truncates toward zero --
7
-7
-- unknown value --
Real32
See also
^builtin(base exponent -- power)Raises one Real object to another of the same schema and keeps that real schema in the result.ceilbuiltin(object -- ceilingValue)Rounds a Real object upward and keeps the same real schema.cosbuiltin(angle -- cosine)Computes the cosine of a Real object and keeps the same real schema.floorbuiltin(object -- floorValue)Rounds a Real object downward and keeps the same real schema.logbuiltin(argument -- naturalLogarithm)Computes the natural logarithm of a Real object and keeps the same real schema.log10builtin(argument -- decimalLogarithm)Computes the base-10 logarithm of a Real object and keeps the same real schema.negbuiltin(object -- negatedValue)Negates a signed Int or Real object and keeps the same schema.sinbuiltin(angle -- sine)Computes the sine of a Real object and keeps the same real schema.sqrtbuiltin(radicand -- squareRoot)Computes the square root of a Real object and keeps the same real schema.