Difference between revisions of "Int"

From Lazarus wiki
Jump to navigationJump to search
 
(4 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
{{Int}}
 
{{Int}}
  
Function '''Int''' returns the integer part of the argument. X is a [[Real|real]]-type expression. The result is the integer part of X, rounded toward zero.
+
The [[Function|<syntaxhighlight lang="pascal" inline>function</syntaxhighlight>]] '''{{Doc|package=RTL|unit=system|identifier=int|text=<syntaxhighlight lang="delphi" inline>Int</syntaxhighlight>}}''' returns the [[Integer|integer]] part of the argument.
 +
It is an [[UCSD Pascal]] extension supported by the [[FPC]] as an <syntaxhighlight lang="delphi" inline>InternProc</syntaxhighlight>.
  
Declaration
+
== Behavior ==
function Int(X: Real): Real;
+
<syntaxhighlight lang="delphi" inline>Int</syntaxhighlight> has effectively the following signature ({{gitlab|blob|FPC|release_3_2_0/rtl/inc/mathh.inc#L116|actual declaration}} uses the compiler intrinsic):
 +
<syntaxhighlight lang="pascal">function Int(X: Real): Real;</syntaxhighlight>
 +
<syntaxhighlight lang="delphi" inline>X</syntaxhighlight> must be a [[Real|real]]-type expression.
 +
The result is the integer part of X, rounded toward zero ''as a <syntaxhighlight lang="pascal" inline>real</syntaxhighlight> value''.
 +
It is ''semantically'' equivalent to <syntaxhighlight lang="pascal" inline style="whitespace: nowrap;">Trunc(X) * 1.0</syntaxhighlight> (<syntaxhighlight lang="pascal" inline>Trunc</syntaxhighlight> would cause an error if there did not exist an appropriate <syntaxhighlight lang="pascal" inline>integer</syntaxhighlight> value).
  
See also:
+
== Application ==
* [[Round]]
+
* <syntaxhighlight lang="delphi" inline>Int</syntaxhighlight> is used to do a [[Trunc|<syntaxhighlight lang="pascal" inline>Trunc</syntaxhighlight>]] without leaving the domain of <syntaxhighlight lang="pascal" inline>real</syntaxhighlight> numbers. This speeds up things and virtually allows a larger range of permissible values. For an example see {{gitlab|blob|FPC|release_3_2_0/rtl/objpas/math.pp#L2476-2496|the implementation of}} {{Doc|package=RTL|unit=math|identifier=fmod|text=<syntaxhighlight lang="delphi" inline>math.FMod</syntaxhighlight>}}.
* [[Trunc]]
 
  
[[Category:Pascal]]
+
== See also ==
 +
* [[Frac|<syntaxhighlight lang="delphi" inline>Frac</syntaxhighlight>]]&nbsp;– return fractional part as <syntaxhighlight lang="pascal" inline>real</syntaxhighlight>
 +
* [[Round|<syntaxhighlight lang="pascal" inline>Round</syntaxhighlight>]]&nbsp;– return rounded <syntaxhighlight lang="pascal" inline>integer</syntaxhighlight> value
 +
* [[Trunc|<syntaxhighlight lang="pascal" inline>Trunc</syntaxhighlight>]]&nbsp;– return truncated <syntaxhighlight lang="pascal" inline>integer</syntaxhighlight> value

Latest revision as of 22:47, 10 August 2022

English (en) русский (ru)

The function Int returns the integer part of the argument. It is an UCSD Pascal extension supported by the FPC as an InternProc.

Behavior

Int has effectively the following signature (actual declaration uses the compiler intrinsic):

function Int(X: Real): Real;

X must be a real-type expression. The result is the integer part of X, rounded toward zero as a real value. It is semantically equivalent to Trunc(X) * 1.0 (Trunc would cause an error if there did not exist an appropriate integer value).

Application

  • Int is used to do a Trunc without leaving the domain of real numbers. This speeds up things and virtually allows a larger range of permissible values. For an example see the implementation of math.FMod.

See also

  • Frac – return fractional part as real
  • Round – return rounded integer value
  • Trunc – return truncated integer value