core.time Duration how to get units in double/float format?
Jonathan M Davis via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Jan 19 11:42:51 PST 2016
On Tuesday, January 19, 2016 14:07:50 Borislav Kosharov via Digitalmars-d-learn wrote:
> On Monday, 18 January 2016 at 12:46:31 UTC, Jonathan M Davis
> wrote:
> > In general, using floating point values with time is an
> > incredibly bad idea. It can certainly make sense when printing
> > stuff out, but using it in calculations is just asking for
> > trouble given all of the unnecessary imprecision that it adds.
> > So, Duration does not directly support floating point values at
> > all, and that's very much on purpose. I'd strongly argue that
> > the fact that TickDuration does was a mistake.
> >
> > So, if you're doing floating point calculations with time, I'd
> > strongly urge you to rethink your code. And if you're just
> > trying to print out the a duration as a floating point value,
> > because it's nice to view that way, then that's fine, but
> > you'll need to do the conversion yourself. And it's not that
> > hard. It just isn't handed to you directly, because aside from
> > printing, code really shouldn't be using floating point values
> > for time. e.g.
> >
> > string toFloatingSeconds(Duration d)
> > {
> > import std.conv;
> > enum hnsecsPerSecond = convert!("seconds", "hnsecs")(1);
> > auto s = d.split!("seconds", "hnsecs")();
> > return to!string(s.seconds + cast(real)(s.hnsecs) /
> > hnsecsPerSecond);
> > }
> >
> > And yes, that's a bit more of a pain than using to!("seconds",
> > float) with TickDuration, but it's also the sort of thing that
> > most code really shouldn't be doing. So, adding that
> > functionality to Duration would just encourage folks to write
> > buggy code.
> >
> > - Jonathan M Davis
>
> I want to use float time in a game where I call the update method
> passing the delta time as float seconds. It's more easy to
> multiply the dt with a speed constant meaning how much the object
> will move after 1 second. Such float delta time is used in many
> game engines and it's somewhat standart way of doing it. Also the
> method you wrote returns a string and I need a float to multiply
> it with a number.
> Thanks for the reply anyway
Using floating point as a delta for time is inherently buggy. Depending on
what you're doing, you may get away with it, but it means that error is
going to creep into your timing. And the bugs introduced by using floating
point like that can be quite subtle. I would strongly urge anyone using
floating point values with timing like that to rework their code to use
integral values. Aside from TickDuration, all of core.time and std.datetime
quite carefully avoids floating point values - even functions like
convClockFreq (which converts one clock frequency to another).
So, if you want to use floating point values with time stuff, that's
obviously your prerogative, but Duration does not directly support it and
isn't ever going to directly support it precisely because it's error-prone.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list