Difference between revisions of "Trunc"

From Lazarus wiki
Jump to navigationJump to search
m (insert external link to documentation)
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
{{Trunc}}
 
{{Trunc}}
  
<div class="floatright">
+
The [[Standard Pascal]] function {{Doc|package=RTL|unit=system|identifier=trunc|text=<syntaxhighlight lang="pascal" inline>trunc</syntaxhighlight>}} returns the integer part of a [[Real|<syntaxhighlight lang="pascal" inline>real</syntaxhighlight>-type value]] rounded toward zero.
[[Image:trunc.png|100px]]
 
</div>  
 
  
The [[RTL]] [[System unit]] contains [[Function|function]] '''trunc''' which truncates a [[Real|real]]-type value to an [[Integer]]-type value.
+
[[Image:trunc.png|thumb|100px|right]]
It's input parameter is a real-type [[expression]].Trunc returns a [[Longint]] value that is the value of the input parameter rounded toward zero.
+
<syntaxhighlight lang="pascal" inline>trunc</syntaxhighlight> is short for “truncate”.
 +
The fraction part of the <syntaxhighlight lang="pascal" inline>real</syntaxhighlight> value is, so to speak, cut off.
 +
However, unlike the [[Int|<syntaxhighlight lang="pascal" inline>int</syntaxhighlight> function]] the result is of [[Integer|<syntaxhighlight lang="pascal" inline>integer</syntaxhighlight>]] type.
  
 +
[[FPC]] implements this [[Function|function]] as a compiler function.
  
== Declaration ==
+
== definition ==
 +
The mathematical definition reads as follows:
  
<syntaxhighlight lang="pascal">
+
<math>
function Trunc(X: Real): Longint;
+
\text{trunc}(x) := \begin{cases}
</syntaxhighlight>
+
\lfloor x \rfloor & \text{if } x \geq 0 \\
 +
\lceil x \rceil & \text{if } x < 0
 +
\end{cases}
 +
</math>
  
== Example Usage ==
+
The function <syntaxhighlight lang="pascal" inline>trunc</syntaxhighlight> is defined for all <syntaxhighlight lang="pascal" inline>real</syntaxhighlight> values in the open interval (<syntaxhighlight lang="pascal" inline>low(integer)-1</syntaxhighlight>,&nbsp;<syntaxhighlight lang="pascal" inline>high(integer)+1</syntaxhighlight>).
 +
If the supplied parameter is out of range, a program compiled with FPC will stop with the [[runtime error|run-time error]] 207 “Invalid floating point operation”.
 +
If the [[sysutils|<syntaxhighlight lang="pascal" inline>sysUtils</syntaxhighlight>]] is included, this RTE becomes the {{Doc|package=RTL|unit=sysutils|identifier=einvalidop|text=<syntaxhighlight lang="pascal" inline>eInvalidOp</syntaxhighlight> exception}}.
  
<syntaxhighlight lang="pascal">
+
== application ==
begin
+
<syntaxhighlight lang="pascal" inline>Trunc</syntaxhighlight> is used to retrieve an integer value.
  WriteLn( Trunc(8.7) );
+
It is primarily used where no loss in accuracy is expected if the fractional part (if any) is removed.
  WriteLn( Trunc(8.3) );
 
  WriteLn( Trunc(-8.7) );
 
  WriteLn( Trunc(-8.3) );
 
  
end;
+
For example:
</syntaxhighlight>
+
[[Pascal]] does not have a power operator or function built in.
 +
Instead, one has to define their own function doing this task using already existing function.
 +
One can utilize the rule <math>a^x = e^{x \times \ln a}</math> and the pre-existing <syntaxhighlight lang="pascal" inline>exp</syntaxhighlight> and <syntaxhighlight lang="pascal" inline>ln</syntaxhighlight> functions for that.
 +
However, if the operands are integers, it is guaranteed the result will be an integer to, yet <syntaxhighlight lang="pascal" inline>exp</syntaxhighlight> will return a <syntaxhighlight lang="pascal" inline>real</syntaxhighlight> value.
 +
The result can be truncated, without loss in precision.
 +
For source code, see [[*#exponentiation|asterisk § “exponentiation”]].
  
=== Output ===
+
== see also ==
8
+
* [[Round|<syntaxhighlight lang="pascal" inline>round</syntaxhighlight>]]
8
+
* {{Doc|package=RTL|unit=math|identifier=ceil|text=<syntaxhighlight lang="pascal" inline>math.ceil</syntaxhighlight>}} – round up  
-8
+
* {{Doc|package=RTL|unit=math|identifier=floor|text=<syntaxhighlight lang="pascal" inline>math.floor</syntaxhighlight>}} – round down  
-8
+
* [[Frac|<syntaxhighlight lang="pascal" inline>frac</syntaxhighlight>]] – returns the fractional part of a floating point value   
 
+
* [[Int|<syntaxhighlight lang="pascal" inline>int</syntaxhighlight>]] – returns the integer part of a floating point value
== See also ==
+
* [[Div|<syntaxhighlight lang="pascal" inline>div</syntaxhighlight>]] – integer division
 
 
* [[Round|<syntaxhighlight lang="pascal" enclose="none">round</syntaxhighlight>]]
 
* {{Doc|package=RTL|unit=math|identifier=ceil|text=<syntaxhighlight lang="pascal" enclose="none">math.ceil</syntaxhighlight>}} - round up  
 
* {{Doc|package=RTL|unit=math|identifier=floor|text=<syntaxhighlight lang="pascal" enclose="none">math.floor</syntaxhighlight>}} - round down  
 
* [[Frac|<syntaxhighlight lang="pascal" enclose="none">frac</syntaxhighlight>]] - returns the fractional part of a floating point value   
 
* [[Int|<syntaxhighlight lang="pascal" enclose="none">int</syntaxhighlight>]] - returns the integer part of a floating point value
 
* [[Div|<syntaxhighlight lang="pascal" enclose="none">div</syntaxhighlight>]] - integer division
 
 
* [[Comparison of approaches for rounding to an integer]]
 
* [[Comparison of approaches for rounding to an integer]]

Latest revision as of 11:23, 23 May 2020

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

The Standard Pascal function trunc returns the integer part of a real-type value rounded toward zero.

trunc.png

trunc is short for “truncate”. The fraction part of the real value is, so to speak, cut off. However, unlike the int function the result is of integer type.

FPC implements this function as a compiler function.

definition

The mathematical definition reads as follows:

[math]\displaystyle{ \text{trunc}(x) := \begin{cases} \lfloor x \rfloor & \text{if } x \geq 0 \\ \lceil x \rceil & \text{if } x \lt 0 \end{cases} }[/math]

The function trunc is defined for all real values in the open interval (low(integer)-1high(integer)+1). If the supplied parameter is out of range, a program compiled with FPC will stop with the run-time error 207 “Invalid floating point operation”. If the sysUtils is included, this RTE becomes the eInvalidOp exception.

application

Trunc is used to retrieve an integer value. It is primarily used where no loss in accuracy is expected if the fractional part (if any) is removed.

For example: Pascal does not have a power operator or function built in. Instead, one has to define their own function doing this task using already existing function. One can utilize the rule [math]\displaystyle{ a^x = e^{x \times \ln a} }[/math] and the pre-existing exp and ln functions for that. However, if the operands are integers, it is guaranteed the result will be an integer to, yet exp will return a real value. The result can be truncated, without loss in precision. For source code, see asterisk § “exponentiation”.

see also