Difference between revisions of "Manual:DIL Manual/integer"
Jump to navigation
Jump to search
(XML import of LLM wiki pages) |
(checked against the compiler and the interpreter; example compiles) |
||
| (One intermediate revision by one other user not shown) | |||
| Line 1: | Line 1: | ||
| + | = integer = | ||
| + | '''Type.''' A whole number, signed, 32 bits. | ||
| + | var | ||
| + | n : integer; | ||
| − | < | + | Integers count, measure and flag things, and they are DIL's truth values too: a condition is true when it is not 0. <code>TRUE</code> (1) and <code>FALSE</code> (0) are defined in <code>vme.h</code>; a comparison gives 1 or 0. |
| − | |||
| − | + | Numbers are written in decimal (<code>42</code>) or hexadecimal (<code>0x2A</code>). A negative number is a minus sign in front of one (<code>-5</code>). Most constants in <code>vme.h</code> and <code>values.h</code> (<code>UNIT_ST_PC</code>, <code>PULSE_SEC</code>, <code>SFB_CMD</code>...) are integers. | |
| − | |||
| − | |||
| − | |||
| − | + | == Operators == | |
| + | * Arithmetic: <code>+ - * / %</code>. Division cuts towards 0. | ||
| + | * Bits: <code>&</code> (and), <code>|</code> (or). <code>isset()</code>, <code>set()</code> and <code>unset()</code> do the same for flag fields. | ||
| + | * Logic: <code>and</code>, <code>or</code>, <code>not</code>. Both sides are always worked out. | ||
| + | * Comparison: <code>== != < > <= >=</code>. | ||
| − | + | == Notes == | |
| + | * Comparisons bind more loosely than <code>and</code> and <code>or</code>: <code>if (a == 1 and b == 2)</code> compiles, but means <code>(a == (1 and b)) == 2</code>. Put each comparison in brackets: <code>if ((a == 1) and (b == 2))</code>. | ||
| + | * Dividing by 0 (<code>/</code> or <code>%</code>) gives no number but a failed value. A failed value is false in every comparison, and assigning it changes nothing: the variable keeps its old value. | ||
| + | * A field of a null unit fails the same way: <code>u.level > 5</code> and <code>u.level <= 5</code> are both false when <code>u</code> is null. | ||
| + | * Many integer fields are stored in fewer bits (8 or 16); a value assigned to them that doesn't fit wraps around. Each field's page says. | ||
| + | * Integer variables keep their value through waits and are saved with the unit. | ||
| + | * <code>itoa()</code> makes a string of an integer, <code>atoi()</code> an integer of a string; <code>"a" + 5</code> is a compile error. | ||
| − | + | == Example == | |
| + | <pre> | ||
| + | dilbegin integer percent(part : integer, whole : integer); | ||
| + | code | ||
| + | { | ||
| + | if (whole == 0) | ||
| + | return (0); | ||
| + | return ((part * 100) / whole); | ||
| + | } | ||
| + | dilend | ||
| − | + | dilbegin do_health(arg : string); | |
| + | external | ||
| + | integer percent(part : integer, whole : integer); | ||
| + | var | ||
| + | pct : integer; | ||
| + | code | ||
| + | { | ||
| + | pct := percent(self.hp, self.max_hp); | ||
| + | if (pct < 25) | ||
| + | sendtext("You are badly hurt (" + itoa(pct) + "%).<br/>", self); | ||
| + | else | ||
| + | sendtext("You are at " + itoa(pct) + "% of your hit points.<br/>", self); | ||
| + | quit; | ||
| + | } | ||
| + | dilend | ||
| + | </pre> | ||
| + | A function that works out a percentage without dividing by 0, and a command that uses it. | ||
| − | + | == See also == | |
| − | + | * [[Manual:DIL Manual/Operators|Operators]] | |
| − | + | * [[Manual:DIL Manual/itoa()|itoa()]] | |
| − | + | * [[Manual:DIL Manual/atoi()|atoi()]] | |
| − | + | * [[Manual:DIL Manual/isset()|isset()]] | |
| − | + | * [[Manual:DIL Manual/rnd()|rnd()]] | |
| − | + | * [[Manual:DIL Manual/intlist|intlist]] | |
| − | + | * [[Manual:DIL Manual/Data Types|Data Types]] | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
Latest revision as of 23:27, 14 November 2026
integer
Type. A whole number, signed, 32 bits.
var n : integer;
Integers count, measure and flag things, and they are DIL's truth values too: a condition is true when it is not 0. TRUE (1) and FALSE (0) are defined in vme.h; a comparison gives 1 or 0.
Numbers are written in decimal (42) or hexadecimal (0x2A). A negative number is a minus sign in front of one (-5). Most constants in vme.h and values.h (UNIT_ST_PC, PULSE_SEC, SFB_CMD...) are integers.
Operators
- Arithmetic:
+ - * / %. Division cuts towards 0. - Bits:
&(and),|(or).isset(),set()andunset()do the same for flag fields. - Logic:
and,or,not. Both sides are always worked out. - Comparison:
== != < > <= >=.
Notes
- Comparisons bind more loosely than
andandor:if (a == 1 and b == 2)compiles, but means(a == (1 and b)) == 2. Put each comparison in brackets:if ((a == 1) and (b == 2)). - Dividing by 0 (
/or%) gives no number but a failed value. A failed value is false in every comparison, and assigning it changes nothing: the variable keeps its old value. - A field of a null unit fails the same way:
u.level > 5andu.level <= 5are both false whenuis null. - Many integer fields are stored in fewer bits (8 or 16); a value assigned to them that doesn't fit wraps around. Each field's page says.
- Integer variables keep their value through waits and are saved with the unit.
itoa()makes a string of an integer,atoi()an integer of a string;"a" + 5is a compile error.
Example
dilbegin integer percent(part : integer, whole : integer);
code
{
if (whole == 0)
return (0);
return ((part * 100) / whole);
}
dilend
dilbegin do_health(arg : string);
external
integer percent(part : integer, whole : integer);
var
pct : integer;
code
{
pct := percent(self.hp, self.max_hp);
if (pct < 25)
sendtext("You are badly hurt (" + itoa(pct) + "%).<br/>", self);
else
sendtext("You are at " + itoa(pct) + "% of your hit points.<br/>", self);
quit;
}
dilend
A function that works out a percentage without dividing by 0, and a command that uses it.