KSP-KOS / KOS

Fully programmable autopilot mod for KSP. Originally By Nivekk
Other
691 stars 229 forks source link

[kOS 0.19.3] Incorrect results when subtracting decimal values. #1600

Closed Patrykz94 closed 8 years ago

Patrykz94 commented 8 years ago

I noticed that when subtracting values that have a decimal point, the result is slightly off. It doesn't seem to be happening while adding, just when subtracting.

Here is a screenshot: http://imgur.com/d9gDjup

Dunbaratu commented 8 years ago

That's a well known problem with floating point numbers. There's literally nothing we can do about it and you'd find the same result from pretty much any language unless the language rounds numbers off by default when it prints them.

Here's an example from old fashioned C: the program:

#include <stdio.h>
int main(int arc, char** argv)
{
    double x = 7455.7;
    double y = 7455.5;

    printf("x - y = %25.18lf\n", (x - y));
}

The result:

x - y =      0.199999999999818101

(The qualifier "25.18" in the print statement means "print the value using 25 characters, 18 of which come after the decimal point." It's necessary to make it print out the real value in C because the default tries to round it off to about 8 places after the point if you don't change it, and that masks what's really going on because that rounding will turn 0.199999999999818101 into 0.20000000.)

Dunbaratu commented 8 years ago

Basically, if you really need the value printed to within a certain limited width, then explicitly round it in kerboscript with the round() function.

Patrykz94 commented 8 years ago

Ok, never noticed that before. Thanks for the explanation.