Skip to content

Incorrect evaluation of numeric mlog constants #295

@cardillan

Description

@cardillan

Mindustry uses a custom routine for parsing/evaluating numeric values. This routine parses some numbers differently from the standard Java methods. When evaluating expressions at compile time, the Java methods are used, which may lead to small numerical differences from the mlog method. These issues surfaced as the Mindcode's processor emulator now matches the game's behavior exactly. Example:

#set target = 8;

param a = 3e50;
const b = 3e50;

test("Param: ", a);
test("Const: ", b);

inline void test(title, n)
    var exp = floor(log10(n));
    var base = n * 10 ** -exp;
    var cmp = abs(base - round(base));
    println(title, cmp * 10 ** 16);
end;

produces this output:

Param: 4.440892098500626
Const: 0

The difference is that mlog parses 3e50 as 3.0000000000000002E50, while Java still loads the number as 3e50. This is different (and much much less severe) from the precision loss when parsing numbers in scientific notation in Mindustry 7. The difference is small and probably inconsequential. However, it is desirable for compile-time evaluation to match mlog evaluation exactly and it needs to be fixed. When converting a numerical literal to a double, the exact routine used by the target Mindsutry version will be used by the compiler.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingcompilerRelated to the code generator

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions