CondorLang / Condor

A fast, simple, and intelligent new programming language
BSD 3-Clause "New" or "Revised" License
34 stars 11 forks source link

std::stof and std::stol Rounding Issues #17

Open chaseWillden opened 7 years ago

chaseWillden commented 7 years ago

When running the following code:

import "println" from "console"

int ten1 = 10;
float tenf = 10.1;
double tend = 10.1;
long tenl = 10.1;

println(ten1 + 10);
println(tenf + 10);
println(tend + 10);
println(tenl + 10);

The following output occurs:

20
20.100000000000001
10
20.100000000000001

A 1 exists at the end. I've isolated where it exactly occurs, but I haven't been able to find out why.

boranyldrm commented 7 years ago

Below C++ code about this situation.

#include <iostream>
#include <iomanip> 

using namespace std;

int main() {

    int ten = 10;
    float tenf = 10.1;
    double tend = 10.1;
    long tenl = 10.1;

    cout << ten + 10 << endl;
    cout << setprecision (15) << fixed << tenf + 10 << endl;
    cout << setprecision (15) << fixed << tend + 10 << endl;
    cout << setprecision (15) << fixed << tenl + 10 << endl;

    return 0;
}

Output:

20
20.100000381469727
20.100000000000001
20

This bug is not about your implementation, I think the only thing to do is format the printed values.

chaseWillden commented 7 years ago

@boranyldrm Interesting, thanks for looking into that. I found this comment on stackoverflow also. So I think instead of defaulting all the numbers to have a precision of 15 decimal places, there should be smarter math to ensure we don't go beyond a float's bounds. I think implementing a NTL number might be beneficial for a bigint or something like that.