[Issue 6725] core.time.dur should accept floating point

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Wed Jul 23 11:49:53 PDT 2014


https://issues.dlang.org/show_bug.cgi?id=6725

--- Comment #31 from Jonathan M Davis <jmdavisProg at gmx.com> ---
Okay. I was going to say that allowing stuff like seconds(.033) would encourage
a lack of precision even in cases where precision was required, and I really
didn't lie that idea, but it looks like the lack of precision really isn't all
that bad. The largest that the error gets is one hnsec:

import std.algorithm;
import std.datetime;
import std.stdio;
import std.string;

void main()
{
    long i = 0;
    immutable units = convert!("seconds", "hnsecs")(1);
    immutable mult = 1.0 / units;
    for(double d = 0; d < 1; d += mult, ++i)
    {
        auto result = cast(long)(d * units);
        assert(result == i || result == i - 1 || result == i + 1,
               format("%s %s %s", i, d, result));
    }
}

So, simply constructing a Duration from a floating point value doesn't
introduce much error. I still don't think that it's a good habit to get into,
but with Duration and SysTime using long internally, we avoid any major
problems.

However, I don't think that we should be making convert work with floating
point values like https://github.com/D-Programming-Language/druntime/pull/905
does, because that helps make the problem more general, and we should probably
avoid making Duration.total support it unless someone has a really good reason
for it, since it would make it easier to manipulate durations as floating point
values, and that's not something that I think that we should support.

--


More information about the Digitalmars-d-bugs mailing list