core.time Duration how to get units in double/float format?

Borislav Kosharov via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jan 19 06:07:50 PST 2016


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


More information about the Digitalmars-d-learn mailing list