Difference between revisions of "Manual:DIL Manual/integer"

From DikuMUD Wiki
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;
  
−
<span id="int"></span>
+
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.
−
'''Integer:'''
 
  
−
  Non-fraction numbers can be used throughout your DIL programs. They are given
+
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.
−
normally, or in normal C style hexadecimal, preceded with '0x'. Integers are
 
−
signed 32-bit integers.  Variables of type string are saved with DIL programs,
 
−
if attached to a saved unit.
 
  
−
'''Example:'''
+
== Operators ==
 +
* Arithmetic: <code>+ - * / %</code>. Division cuts towards 0.
 +
* Bits: <code>&amp;</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>== != &lt; &gt; &lt;= &gt;=</code>.
  
−
  0x10
+
== 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 &gt; 5</code> and <code>u.level &lt;= 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:'''
+
== Example ==
 +
<pre>
 +
dilbegin integer percent(part : integer, whole : integer);
 +
code
 +
{
 +
  if (whole == 0)
 +
      return (0);
 +
  return ((part * 100) / whole);
 +
}
 +
dilend
  
−
   2
+
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.
  
−
  Integers are used within expressions as both number value and boolean(true/false)
+
== See also ==
−
values. You may use comparison between integers through the comparison operators:
+
* [[Manual:DIL Manual/Operators|Operators]]
−
'==' (equality),
+
* [[Manual:DIL Manual/itoa()|itoa()]]
−
'<' (less than),
+
* [[Manual:DIL Manual/atoi()|atoi()]]
−
'>' (greater than),
+
* [[Manual:DIL Manual/isset()|isset()]]
−
'<=' (less or equal),
+
* [[Manual:DIL Manual/rnd()|rnd()]]
−
'>=' (greater or equal)
+
* [[Manual:DIL Manual/intlist|intlist]]
−
'!=' (not equal).
+
* [[Manual:DIL Manual/Data Types|Data Types]]
−
 
 
−
 
 
−
'''Example:'''
 
−
 
 
−
  if (42<12) ...
 
−
 
 
−
  Returning the boolean value (true/false) depending on the comparison between
 
−
integers. The result may be stored in an integer variable, and tested later, or
 
−
used directly in an 'if' or 'while', etc.  You may also operate on boolean
 
−
expression themselves, using LOGICAL operators 'and','not','or', which allows
 
−
you to combine the expressions.
 
−
 
 
−
'''Example:'''
 
−
 
 
−
  if ( not ( (self.hp<42) or (self.level>10) ) ) ..
 
−
 
 
−
Integer expressions may also use a number of other operators:
 
−
'+' (addition)
 
−
'-' (subtraction/negation),
 
−
'*' (multiplication),
 
−
'/' (division),
 
−
'|' (bitwise or, for flags),
 
−
'&' (bitwise and, for flags)
 
−
 
 
−
Precedence rules for using these operators are still somewhat messed up.  You'd
 
−
better use parenthesis where ever possible.
 
−
 
 
−
---~---~---~---~---~---~---~---~---
 

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() and unset() do the same for flag fields.
  • Logic: and, or, not. Both sides are always worked out.
  • Comparison: == != < > <= >=.

Notes

  • Comparisons bind more loosely than and and or: 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 > 5 and u.level <= 5 are both false when u 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.
  • itoa() makes a string of an integer, atoi() an integer of a string; "a" + 5 is 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.

See also