Difference between revisions of "Odd"

From Free Pascal wiki
Jump to navigationJump to search
(fix)
(rewrite, remove erroneous classification as an operator)
 
Line 1: Line 1:
 
{{Odd}}
 
{{Odd}}
Return to [[Reserved words]]
 
  
The system function {{HL|odd}} returns the value {{HL|true}} if and only if the argument passed to it is odd. The {{HL|odd}} function must be passed an integer value, it cannot accept reals.
+
The [[Standard Functions|standard function]] {{Doc|package=RTL|unit=system|identifier=odd|text='''<syntaxhighlight lang="pascal" inline>odd</syntaxhighlight>'''}} returns [[true|<syntaxhighlight lang="pascal" inline>true</syntaxhighlight>]] if and only if the passed [[Integer|<syntaxhighlight lang="pascal" inline>integer</syntaxhighlight>]] parameter is odd, that means it is not divisible by&nbsp;<syntaxhighlight lang="pascal" inline>2</syntaxhighlight>.
 +
<syntaxhighlight lang="pascal" inline>Odd(x)</syntaxhighlight> is by definition equivalent to the [[expression]]
 +
<syntaxhighlight lang="pascal">abs(x) mod 2 = 1</syntaxhighlight>
 +
<small>
 +
The <syntaxhighlight lang="pascal" inline>abs</syntaxhighlight> serves the purpose of any potential for confusion regarding the sign of the [[Mod|<syntaxhighlight lang="pascal" inline>mod</syntaxhighlight> operator]] in conjunction with a ''negative'' operand.
 +
It is ''unnecessary'' in a ''fully-ISO‑compliant'' compiler (for [[FPC]] this means [[modeswitch|<syntaxhighlight lang="pascal" inline style="whitespace: nowrap;">{$modeSwitch ISOMod+}</syntaxhighlight>]]).
 +
</small>
  
Usage:
+
== application ==
: <syntaxhighlight lang="pascal" inline> B := odd(i);</syntaxhighlight>  
+
Beside using <syntaxhighlight lang="pascal" inline>odd</syntaxhighlight> directly, in combination with [[Ord|<syntaxhighlight lang="pascal" inline>ord</syntaxhighlight>]] it can be useful to simplify calculations.
or
+
The following [[Program|<syntaxhighlight lang="pascal" inline>program</syntaxhighlight>]] calculates <math>1 \cdot 3^1 + 2 \cdot 3^0 + 3 \cdot 3^1 + 4 \cdot 3^0 </math> (=&nbsp;<math>3 + 2 + 9 + 4</math>) without the need of [[Branch|branches]]:
: {{HL|if odd(i) then ...}}
+
<syntaxhighlight lang="pascal" highlight="8-9">
Where
+
{$mode extendedPascal}
: {{HL|B}} is a [[Boolean|{{HL|boolean}}]] variable and
+
program oddDemo(output);
: {{HL|i}} is an [[Integer|{{HL|integer}}]] value.
+
var
 +
i, sum: integer value 0;
 +
begin
 +
for i := 1 to 4 do
 +
begin
 +
{ alternating scale factor 3^0, 3^1 based on Boolean }
 +
sum := sum + i * 3 pow ord(odd(i));
 +
end;
 +
writeLn(sum);
 +
end.
 +
</syntaxhighlight>
 +
<small>
 +
NB:
 +
Despite [[Mode extendedpascal|<syntaxhighlight lang="pascal" inline style="whitespace: nowrap;">{$mode extendedPascal}</syntaxhighlight>]] the shown <syntaxhighlight lang="pascal" inline>integer</syntaxhighlight> power operator <syntaxhighlight lang="pascal" inline>pow</syntaxhighlight> and initial-value specification are not yet supported by FPC as of version 3.2.0.
 +
</small>
  
The value supplied to {{HL|odd}} can be any integer: longint, longword, int64 or qword.
+
== notes ==
 
+
* There is no standard built‑in function “<syntaxhighlight lang="pascal" inline>even</syntaxhighlight>”. Obviously the expression [[Not|<syntaxhighlight lang="pascal" inline style="whitespace: nowrap;">not odd(x)</syntaxhighlight>]] determines whether an <syntaxhighlight lang="pascal" inline>integer</syntaxhighlight> is even.
{{Logical operators}}
+
* For constant expressions, <syntaxhighlight lang="pascal" inline>odd</syntaxhighlight> can be evaluated at compile-time, thus it can appear in the [[Const|<syntaxhighlight lang="pascal" inline>const</syntaxhighlight> section]].
* [[https://www.freepascal.org/docs-html/3.0.0/rtl/system/odd.html On-Linr documentation for the "odd" function]]
+
* On most architectures <syntaxhighlight lang="pascal" inline>odd</syntaxhighlight> can be implemented as a simple <syntaxhighlight lang="asm" style="whitespace: nowrap;" inline>and x, 1</syntaxhighlight>, that is by masking the least-significant bit in a value. For ''these'' platforms, using [[Type Helper|type helpers]] from the [[sysutils|<syntaxhighlight lang="pascal" inline>sysUtils</syntaxhighlight> unit]], <syntaxhighlight lang="pascal" inline>x.testBit(0)</syntaxhighlight> has the same return value.
 
 
[[Category:Operators]]
 

Latest revision as of 05:38, 20 January 2022

Deutsch (de) English (en) polski (pl)

The standard function odd returns true if and only if the passed integer parameter is odd, that means it is not divisible by 2. Odd(x) is by definition equivalent to the expression

abs(x) mod 2 = 1

The abs serves the purpose of any potential for confusion regarding the sign of the mod operator in conjunction with a negative operand. It is unnecessary in a fully-ISO‑compliant compiler (for FPC this means {$modeSwitch ISOMod+}).

application

Beside using odd directly, in combination with ord it can be useful to simplify calculations. The following program calculates [math]\displaystyle{ 1 \cdot 3^1 + 2 \cdot 3^0 + 3 \cdot 3^1 + 4 \cdot 3^0 }[/math] (= [math]\displaystyle{ 3 + 2 + 9 + 4 }[/math]) without the need of branches:

{$mode extendedPascal}
program oddDemo(output);
var
	i, sum: integer value 0;
begin
	for i := 1 to 4 do
	begin
		{ alternating scale factor 3^0, 3^1 based on Boolean }
		sum := sum + i * 3 pow ord(odd(i));
	end;
	writeLn(sum);
end.

NB: Despite {$mode extendedPascal} the shown integer power operator pow and initial-value specification are not yet supported by FPC as of version 3.2.0.

notes

  • There is no standard built‑in function “even”. Obviously the expression not odd(x) determines whether an integer is even.
  • For constant expressions, odd can be evaluated at compile-time, thus it can appear in the const section.
  • On most architectures odd can be implemented as a simple and x, 1, that is by masking the least-significant bit in a value. For these platforms, using type helpers from the sysUtils unit, x.testBit(0) has the same return value.