Difference between revisions of "*"

From Free Pascal wiki
Jump to navigationJump to search
(→‎see also: power)
(exponentiation in system unit)
Line 33: Line 33:
 
== exponentiation ==
 
== exponentiation ==
 
Furthermore, in [[FPC]] the exponentiation operator consisting of two consecutive asterisks <syntaxhighlight lang="pascal" enclose="none">**</syntaxhighlight> exists.
 
Furthermore, in [[FPC]] the exponentiation operator consisting of two consecutive asterisks <syntaxhighlight lang="pascal" enclose="none">**</syntaxhighlight> exists.
However, it isn't defined for any type by the standard system unit.
+
However, it is only {{Doc|package=RTL|unit=system|identifier=.op-power-variant-ariant-ariant|text=defined for variants}} by the standard system unit, which requires a variant manager being installed.
Instead you have the chance to overload it on your own.
+
In order to actually use it with integers, one can define it on their own:
 
 
 
<syntaxhighlight lang="pascal" highlight="6-9,12">
 
<syntaxhighlight lang="pascal" highlight="6-9,12">
 
program exponentiation(input, output, stderr);
 
program exponentiation(input, output, stderr);
Line 64: Line 63:
  
 
In [[ASCII]], the character code decimal <syntaxhighlight lang="pascal" enclose="none">42</syntaxhighlight> (or [[Hexadecimal|hexadecimal]] <syntaxhighlight lang="pascal" enclose="none">2A</syntaxhighlight>) is defined to be <syntaxhighlight lang="pascal" enclose="none">*</syntaxhighlight>.
 
In [[ASCII]], the character code decimal <syntaxhighlight lang="pascal" enclose="none">42</syntaxhighlight> (or [[Hexadecimal|hexadecimal]] <syntaxhighlight lang="pascal" enclose="none">2A</syntaxhighlight>) is defined to be <syntaxhighlight lang="pascal" enclose="none">*</syntaxhighlight>.
 
== see also ==
 
* {{Doc|package=RTL|unit=system|identifier=.op-power-variant-ariant-ariant|text=<syntaxhighlight lang="pascal" enclose="none">system.power</syntaxhighlight>}}
 
  
 
{{Symbols}}
 
{{Symbols}}
 
[[Category:Code]]
 
[[Category:Code]]

Revision as of 20:01, 8 November 2018

English (en) suomi (fi) français (fr) русский (ru)

*

standard Pascal

The symbol *, pronounced “asterisk”, is used in Pascal to

  • indicate multiplication of numbers, or
  • form the intersection of sets.
program asteriskDemo(input, output, stderr);

type
	day = (monday, tuesday, wednesday,
		thursday, friday, saturday, sunday);

var
	i: longint;
	n: real;
	m: set of day;

begin
	// multiplication operator
	i := 6 * 7;      // i becomes 42
	n := 6.0 * 7.0;  // n becomes 42.0
	
	// intersection operator
	m := [saturday, sunday] * [sunday, monday];
	// m is now {sunday}
end.

exponentiation

Furthermore, in FPC the exponentiation operator consisting of two consecutive asterisks ** exists. However, it is only defined for variants by the standard system unit, which requires a variant manager being installed. In order to actually use it with integers, one can define it on their own:

program exponentiation(input, output, stderr);

// make operator overloading available
{$mode objfpc}

operator ** (const base: integer; const exponent: integer): integer;
begin
	result := trunc(exp(ln(base) * exponent));
end;

begin
	writeLn(2 ** 10); // will print 1024
end.

For readily available overloads, the math and matrix unit can be included.

other appearances

In Pascal's years of childhood computer systems did not necessarily knew the comment delimiting characters opening and closing curly brace { }. To make block comments available on such systems an alternative syntax, the bigramms (* and *) are allowed, too, but they can't be interchanged willynilly: (* has to match a *), and can not match a } even though it is closing a block comment, too.

Also, if C like operators were allowed by the compiler directive {$COperator on}, the short syntax for i := i * n reads i *= n. But by doing so, you leave the domain of Pascal. Your code technically, mathematically speaking becomes wrong.

In ASCII, the character code decimal 42 (or hexadecimal 2A) is defined to be *.


navigation bar: topic: Pascal symbols
single characters

+ (plus)  •  - (minus)  •  * (asterisk)  •  / (slash)
= (equal)  •  > (greater than)  •  < (less than)
. (period)  •  : (colon)  •  ; (semi colon)
^ (hat)  •  @ (at)
$ (dollar sign)  •  & (ampersand)  •  # (hash)
' (single quote)

character pairs

<> (not equal)  •  <= (less than or equal)  •  := (becomes)  •  >= (greater than or equal)

 •  >< (symmetric difference)  •  // (double slash)