The fine print
A collection of places where intuition fails: comparisons with a tolerance, null instead of infinity and objects equal only to themselves
equal compares with a tolerance
Section titled “equal compares with a tolerance”0.1 + 0.2 in mlog, as everywhere, gives 0.30000000000000004. But op equal almost 0.3
answers one: it is a comparison with a tolerance — two numbers count as equal if the
difference is less than one millionth. A strict comparison of the same pair gives zero.
The same example shows the rest too: division by zero turned into null, null turned out to be
equal to zero by equal and not equal by strictEqual, and copper is equal only to copper.
| Condition | How it compares |
|---|---|
equal, notEqual |
with a tolerance of 0.000001 |
strictEqual |
exactly, and checks the type as well |
lessThan, greaterThan and the rest |
exactly, with no tolerance |
Hence an oddity: equal can say “equal” while lessThan says “less”, and both are right.
Division by zero gives null
Section titled “Division by zero gives null”There is no infinity and no “not a number” in mlog variables. The moment a computation gives such an answer, the variable becomes null:
op div x 5 0 # x = nullop div y 0 0 # y = nullop sqrt z -4 # z = nullThat is not our invention but how a variable is built in the game: it does not store an unusable number at all and turns it into null.
Null in comparisons
Section titled “Null in comparisons”| Comparison | The answer |
|---|---|
equal null 0 |
1 — in arithmetic null counts as zero |
strictEqual null 0 |
0 — the types differ |
op add x null 5 |
5 |
print null |
null |
Null is not zero, but it behaves like zero everywhere except in a strict comparison and in
printing. The only reliable way to tell “no value” from “the value zero” is strictEqual.
Objects are equal only to themselves
Section titled “Objects are equal only to themselves”Buildings, units and content are compared by identity: equal @copper @copper is one,
equal @copper @lead is zero. There is no “similar”.
Two things follow from that:
- two units of the same type are not equal. To compare types you ask
sensor kind @unit @typeand compare the types instead; - a building from
getlinkand the same building fromradarare one and the same object, and comparing them will confirm it.
A string is a value, but not a number
Section titled “A string is a value, but not a number”A string can be put in a variable, printed, compared for equality and read character by character
through read. What you cannot do with it is add it up
and compare it in order:
op add x "abc" 5 # not an addition: a string in arithmetic is a oneop lessThan y "abc" 5 # compares a one, not a stringIn numeric operations a string behaves like an object: present is one, absent is zero.
Unofficial fan project, not affiliated with Anuke. Mindustry sprites, fonts and translations © Anuke, used under GPL-3.0; Fira Code under OFL-1.1. Site code is GPL-3.0.