WebDrake / Rational

std.rational Phobos candidate module
2 stars 0 forks source link

toRational should consider relative epsilon, not just absolute #12

Open WebDrake opened 11 years ago

WebDrake commented 11 years ago

toRational converts floating-point types to rationals with a tolerance epsilon which by default is 1e-8. However, this is an absolute rather than relative value, and currently the code does not even support the case where the floating-point input is less than the tolerance value, simply returning zero:

    // Handle this as a special case to make the rest of the code less
    // complicated:
    if (abs(floatNum) < epsilon)
    {
        Rational!Int ret;
        ret.num = 0;
        ret.den = 1;
        return ret;
    }

Suggested solution: toRational should accept both a relative and absolute tolerance, as with std.math.approxEquals.