Skip to content

Wrong floating-point operations #292

@cardillan

Description

@cardillan

Mindcode currently performs optimizations which may change the outcome of floating-point operations performed by the program.

Example:

const c = 1e20;
param p = 1e20;
param x = 1;

println(p + x - p);
println(c + x - c);

compiles to

set p 1E20
set x 1
op add *tmp0 p x
op sub *tmp1 *tmp0 p
print "Unoptimized: "
print *tmp1
print "\nOptimized: "
print x
print "\n"

and outputs

Unoptimized: 0
Optimized: 1

Due to limited precision of mlog variables, the value of 1e20 + 1 is 1e20, and after subtracting 1e20, the result is 0. However, when the value is constant, Mindcode removes both addition and subtraction, as they're opposite operations in an ideal (algebraic) sense. This leaves the original value of 1 intact.

The problem is caused by the Data Flow Optimization (in constant folding), the Expression Optimization and perhaps some other.

The issue affects programs which perform numerical computations, computations with non-integer values or with integer values values exceeding 253. This should be quite unusual for a typical mlog program, so most programs probably won't be affected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingoptimizerRelated to the code optimizer

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions