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

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Mon Jul 21 10:40:01 PDT 2014


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

Steven Schveighoffer <schveiguy at yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |schveiguy at yahoo.com

--- Comment #8 from Steven Schveighoffer <schveiguy at yahoo.com> ---
I feel like I should weigh in on this.

While I agree it's not good to deal with floating point at a low level
(Duration should never contain a floating point value), *creating* a duration
based on a floating point is OK as long as the conversion is sound. Note that
the most common usage of this would be for Duration literals.

IIRC, you can guarantee the conversion is sound by using an epsilon. In other
words, if you pass in 1.5, it's really 1 second, 500 milliseconds, not
1.4999999... seconds.

I agree with Vladimir's point, even if there is some error in floating point
representation, sleeping e.g. for 1.499 seconds vs. 1.5 isn't going to make a
large difference. But I think we can largely avoid the floating point error.

Where things become dicey is when you are accumulating lots of floating point
values, or if you are working with very very small values.

Note, the indicated mechanism to make a proper duration has improved somewhat:

1.second + 500.msecs + 100.usecs;

I think this still looks awful.

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.

--


More information about the Digitalmars-d-bugs mailing list