Trunc
function Trunc(E: Extended): LongInt;
Example
procedure ScriptEvent(var Value: variant);
begin
Value := Trunc(-12.75); // -12
end;
Usage
Trunc discards an Extended value's fraction toward zero and narrows the result to a 32-bit LongInt.
Parameters
| Name | Type | Description |
|---|---|---|
E | Extended | Floating value whose fractional component is discarded toward zero. |
Returns
For a truncated value within Low(LongInt)..High(LongInt), the expected 32-bit integer. Otherwise, the low 32 bits of the Velox Int64 result are reinterpreted as signed LongInt.
Velox Int64 result | Velox LongInt result |
|---|---|
2147483648 | -2147483648 |
4294967295 | -1 |
4294967296 | 0 |
-2147483649 | 2147483647 |
Additional Technical Info
Trunc discards the fractional part of an Extended value by rounding toward zero, obtains Delphi's Int64 result, then stores only a PascalScript 32-bit LongInt.
The unchecked 32-bit narrowing is a critical difference from Delphi's public Int64 contract. Values outside the LongInt range silently wrap modulo 2^32 if they are still convertible to Int64. The example is fictional and source-reviewed only.
Implementation
- The modified PascalScript compiler declares
Truncin the Numeric group. - Runtime
DefProcselector 19 reads the argument as real. - Delphi
System.Truncproduces anInt64by rounding toward zero. Stack.SetIntaccepts aLongInt;PascalScript.inchas disabled range/overflow checks, so narrowing wraps.
Edge cases and errors
Trunc(-12.75)is-12, not floor-13.- NaN, infinity or a value outside Delphi's
Int64conversion range can raiseEInvalidOp/a PascalScript math error before narrowing. Extendedis Double on default Win64 and wider on Win32; near-boundary binary representation can differ.- This is not a checked numeric conversion. Validate against the 32-bit LongInt range before calling if wraparound is not acceptable.
Related entries
Intalso removes the fraction toward zero but returns floatingExtendedand does not narrow to LongInt.Fracreturns the complementary signed fractional part.Roundrounds to nearest before applying the same unchecked 32-bit narrowing.
External references
Created 2026-07-15