Re: [RFC] intdiv()

From: Date: Tue, 15 Jul 2014 12:11:34 +0000
Subject: Re: [RFC] intdiv()
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-75532@lists.php.net to get a copy of this message
On 15 Jul 2014, at 03:31, Bishop Bettini <bishop@php.net> wrote:
> I need some education.  Can you elaborate on the specific situations where integer division
> would be used without other functions from bcmath or gmp?
> 
> I see in the RFC you mention seconds to hh:mm and index to rows/cols, but can you give some
> actual "before" and "after" samples?  Like this is how it would be done today
> without intdiv, and here's how it would be done after?

Sure. Say I have a number of seconds which I wish to split into years, days, hours, minutes and
seconds, for example:

$s = 1000000;
$seconds = $s % 60;
$minutes = intdiv($s, 60) % 60;
$hours = intdiv($s, 3600) % 24;
$days = intdiv($s, 3600 * 24) % 365;
$years = intdiv($s, 3600 * 24 * 365);

Currently, you’d have to cheat and use floating-point division:

$x = 1000000;
$seconds = $s % 60;
$minutes = (int)($s / 60) % 60;
$hours = (int)($s / 3600) % 24;
$days = (int)($s / (3600 * 24)) % 365;
$years = (int)($s / (3600 * 24 * 365));

The second one is a bit of a hack, really, but it would probably work most of the time since
realistically nobody is using >53-bit time values at the moment (though people are using
>32-bit values, so that 64-bit RFC can’t come soon enough given Y2K38).

However, intdiv() is perhaps not the best way to implement it or the best syntax.

--
Andrea Faulds
http://ajf.me/






Thread (49 messages)

« previous php.internals (#75532) next »