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

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Mon Jul 21 11:24:14 PDT 2014


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

--- Comment #9 from Jonathan M Davis <jmdavisProg at gmx.com> ---
(In reply to Steven Schveighoffer from comment #8)
> Note, the indicated mechanism to make a proper duration has improved
> somewhat:
> 
> 1.second + 500.msecs + 100.usecs;
> 
> I think this still looks awful.

In most cases, the most that you'd need would be two pieces, not three. And I
think that that's a small price to pay for avoiding floating point errors.

> As a compromise, one could make a text-parser based mechanism, similar to
> octal:
> 
> dur!"1.5001s"();
> 
> That translates into the above.
> Not sure how well this would turn out ;)
> 
> A true "literal" syntax would make things easier to deal with.

So, basically, you're suggesting that we create function or template which
takes a string which represents a floating point value at compile time and
generates a Duration from that, erroring out if it doesn't convert properly to
hnsecs? That would be pretty easy to do (though I wouldn't reuse dur for that,
since it already has a string template parameter - maybe durLit), and it would
avoid the problem with floating point error. Any floating point string with no
more than 7 decimal places (since that's what hnsecs will give you) and which
wasn't too large for a long to hold once converted would convert just fine
without floating point errors. That would certainly be better than allowing
floating point values at runtime.

--


More information about the Digitalmars-d-bugs mailing list