Averages/Mean time of day: Difference between revisions
→{{header|Perl}}: replaced algorithm that only gave approximate answer
No edit summary |
SqrtNegInf (talk | contribs) (→{{header|Perl}}: replaced algorithm that only gave approximate answer) |
||
Line 1,327:
=={{header|Perl}}==
Using the core module <code>Math::Complex</code> to enable use of complex numbers. The <code>POSIX</code> CPAN module provides the <code>fmod</code> routine for non-integer modulus calculations.
<lang Perl>use Const::Fast;▼
{{trans|Perl 6}}
use Math::Complex;
use List::Util qw(sum);
use utf8;
use constant τ => 2 * 3.1415926535;
my ( $hms ) = @_;▼
# time-of-day to radians
my ( $h, $m, $s ) = split /:/, $hms;▼
sub tod2rad {
(3600*$h + 60*$m + $s) * τ / 86400;
}
# radians to time-of-day
sub rad2tod {
my $x = $_[0] * 86400 / τ;
sprintf '%02d:%02d:%02d', fm($x/3600,24), fm($x/60,60), fm($x,60);
}▼
# float modulus, normalized to positive values
sub fm {
$
$
}
sub phase { arg($_[0]) } # aka theta
sub cis { cos($_[0]) + i*sin($_[0]) }
sub mean_time { rad2tod phase sum map { cis tod2rad $_ } @_ }
▲}
print mean_time(@times) . " is the mean time of " . join(' ', @times) . "\n";</lang>
{{out}}
<pre>23:47:43 is the mean time of 23:00:17 23:40:20 00:12:45 00:17:19</pre>
▲$ echo 23:00:17, 23:40:20, 00:12:45, 00:17:19 | mean_time.pl
=={{header|Perl 6}}==
|