Jump to content

Averages/Mean time of day: Difference between revisions

m
Automated syntax highlighting fixup (second round - minor fixes)
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 1:
{{task|Date and time}}
 
;Task
{{task heading}}
 
A particular activity of bats occurs at these times of the day:
Line 21:
=={{header|11l}}==
{{trans|Python}}
<syntaxhighlight lang="11l">F mean_angle(angles)
V x = sum(angles.map(a -> cos(radians(a)))) / angles.len
V y = sum(angles.map(a -> sin(radians(a)))) / angles.len
Line 49:
{{libheader|Action! Tool Kit}}
{{libheader|Action! Real Math}}
<syntaxhighlight lang=Action"action!">INCLUDE "H6:REALMATH.ACT"
 
DEFINE PTR="CARD"
Line 146:
 
=={{header|Ada}}==
<syntaxhighlight lang=Ada"ada">with Ada.Calendar.Formatting;
with Ada.Command_Line;
with Ada.Numerics.Elementary_Functions;
Line 233:
=={{header|AutoHotkey}}==
{{works with|AutoHotkey 1.1}}
<syntaxhighlight lang=AutoHotkey"autohotkey">MsgBox, % "The mean time is: " MeanTime(["23:00:17", "23:40:20", "00:12:45", "00:17:19"])
 
MeanTime(t, x=0, y=0) {
Line 258:
 
=={{header|AWK}}==
<syntaxhighlight lang=AWK"awk">#!/usr/bin/awk -f
{
c = atan2(0,-1)/(12*60*60);
Line 277:
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang="bbcbasic"> nTimes% = 4
DATA 23:00:17, 23:40:20, 00:12:45, 00:17:19
Line 322:
 
=={{header|C}}==
<syntaxhighlight lang=C"c">#include<stdlib.h>
#include<math.h>
#include<stdio.h>
Line 403:
 
=={{header|C sharp|C#}}==
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 471:
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">#include <iomanip>
#include <iostream>
#include <vector>
Line 542:
=={{header|Common Lisp}}==
{{trans|Echo Lisp}}
<syntaxhighlight lang="lisp">;; * Loading the split-sequence library
(eval-when (:compile-toplevel :load-toplevel :execute)
(ql:quickload '("split-sequence")))
Line 590:
=={{header|D}}==
{{trans|Python}}
<syntaxhighlight lang="d">import std.stdio, std.range, std.algorithm, std.complex, std.math,
std.format, std.conv;
 
Line 635:
{{libheader| System.Math}}
{{Trans|Go}}
<syntaxhighlight lang=Delphi"delphi">
program Averages_Mean_time_of_day;
 
Line 693:
end.</syntaxhighlight>
=={{header|EchoLisp}}==
<syntaxhighlight lang="scheme">
;; string hh:mm:ss to radians
(define (time->radian time)
Line 719:
 
=={{header|Erlang}}==
<syntaxhighlight lang=Erlang"erlang">
-module( mean_time_of_day ).
-export( [from_times/1, task/0] ).
Line 759:
=={{header|Euphoria}}==
{{works with|OpenEuphoria}}
<syntaxhighlight lang=Euphoria"euphoria">
include std/console.e
include std/math.e
Line 836:
 
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">open System
open System.Numerics
 
Line 862:
 
=={{header|Factor}}==
<syntaxhighlight lang="factor">USING: arrays formatting kernel math math.combinators
math.functions math.libm math.parser math.trig qw sequences
splitting ;
Line 895:
=={{header|Fortran}}==
{{works with|gfortran 5.1.0}}
<syntaxhighlight lang="fortran">
program mean_time_of_day
implicit none
Line 973:
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Const pi As Double = 3.1415926535897932
Line 1,034:
 
=={{header|Go}}==
<syntaxhighlight lang="go">package main
 
import (
Line 1,091:
=={{header|Groovy}}==
Solution:
<syntaxhighlight lang="groovy">import static java.lang.Math.*
 
final format = 'HH:mm:ss', clock = PI / 12, millisPerHr = 3600*1000
Line 1,104:
 
Test:
<syntaxhighlight lang="groovy">println (meanTime("23:00:17", "23:40:20", "00:12:45", "00:17:19"))</syntaxhighlight>
 
{{out}}
Line 1,110:
 
=={{header|Haskell}}==
<syntaxhighlight lang="haskell">import Data.Complex (cis, phase)
import Data.List.Split (splitOn)
import Text.Printf (printf)
Line 1,143:
=={{header|Icon}} and {{header|Unicon}}==
 
<syntaxhighlight lang="text">procedure main(A)
every put(B := [], ct2a(!A))
write(ca2t(meanAngle(B)))
Line 1,178:
=={{header|J}}==
use <code>avgAngleR</code> from [[Averages/Mean angle#J]]
<syntaxhighlight lang=J"j">require 'types/datetime'
parseTimes=: ([: _&".;._2 ,&':');._2
secsFromTime=: 24 60 60 #. ] NB. convert from time to seconds
Line 1,184:
meanTime=: 'hh:mm:ss' fmtTime [: secsFromTime [: avgAngleR&.rft parseTimes</syntaxhighlight>
{{out|Example Use}}
<syntaxhighlight lang=J"j"> meanTime '23:00:17 23:40:20 00:12:45 00:17:19 '
23:47:43</syntaxhighlight>
 
=={{header|Java}}==
{{trans|Kotlin}}
<syntaxhighlight lang="java">public class MeanTimeOfDay {
static double meanAngle(double[] angles) {
Line 1,245:
=={{header|Javascript}}==
{{works with|Node.js}}
<syntaxhighlight lang=Javascript"javascript">var args = process.argv.slice(2);
 
function time_to_seconds( hms ) {
Line 1,295:
 
The "mean time" of two times that differ by 12 hours (e.g. ["00:00:00", "12:00:00"]) is not very well-defined, and is accordingly computed as null here.
<syntaxhighlight lang="jq"># input: array of "h:m:s"
def mean_time_of_day:
def pi: 4 * (1|atan);
Line 1,327:
end ;</syntaxhighlight>
'''Examples'''
<syntaxhighlight lang="jq">["0:0:0", "12:0:0" ],
["0:0:0", "24:0:0" ],
["1:0:0", "1:0:0" ],
Line 1,336:
| mean_time_of_day</syntaxhighlight>
{{out}}
<syntaxhighlight lang="sh">$ jq -r -n -f Mean_time_of_day.jq
null
00:00:00
Line 1,348:
{{works with|Julia|0.6}}
{{trans|MATLAB}}
<syntaxhighlight lang="julia">using Statistics
 
function meantime(times::Array, dlm::String=":")
Line 1,379:
=={{header|Kotlin}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="scala">// version 1.0.6
 
fun meanAngle(angles: DoubleArray): Double {
Line 1,422:
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
global pi
pi = acs(-1)
Line 1,481:
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">
local times = {"23:00:17","23:40:20","00:12:45","00:17:19"}
 
Line 1,524:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">meanTime[list_] :=
StringJoin@
Riffle[ToString /@
Line 1,536:
 
=={{header|MATLAB}} / {{header|Octave}}==
<syntaxhighlight lang=MATLAB"matlab">function t = mean_time_of_day(t)
c = pi/(12*60*60);
for k=1:length(t)
Line 1,552:
=={{header|Nim}}==
{{works with|Nim|0.20.0+}}
<syntaxhighlight lang="nim">import math, complex, strutils, sequtils
proc meanAngle(deg: openArray[float]): float =
Line 1,577:
=={{header|Oberon-2}}==
{{works with|oo2c}}
<syntaxhighlight lang="oberon2">
MODULE AvgTimeOfDay;
IMPORT
Line 1,655:
 
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">let pi_twice = 2.0 *. 3.14159_26535_89793_23846_2643
let day = float (24 * 60 * 60)
 
Line 1,696:
 
=={{header|ooRexx}}==
<syntaxhighlight lang="oorexx">/* REXX ---------------------------------------------------------------
* 25.06.2014 Walter Pachl
*--------------------------------------------------------------------*/
Line 1,736:
 
=={{header|PARI/GP}}==
<syntaxhighlight lang="parigp">meanAngle(v)=atan(sum(i=1,#v,sin(v[i]))/sum(i=1,#v,cos(v[i])))%(2*Pi)
meanTime(v)=my(x=meanAngle(2*Pi*apply(u->u[1]/24+u[2]/1440+u[3]/86400, v))*12/Pi); [x\1, 60*(x-=x\1)\1, round(60*(60*x-60*x\1))]
meanTime([[23,0,17], [23,40,20], [0,12,45], [0,17,19]])</syntaxhighlight>
Line 1,746:
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.
{{trans|Raku}}
<syntaxhighlight lang=Perl"perl">use strict;
use warnings;
use POSIX 'fmod';
Line 1,784:
===v5.36===
As previous, but using features from an up-to-date release of Perl, e.g. strict/warn/subroutine signatures without the <code>use</code> boilerplate.
<syntaxhighlight lang="perl">use v5.36;
use POSIX 'fmod';
use Math::Complex;
Line 1,805:
 
=={{header|Phix}}==
<!--<syntaxhighlight lang=Phix"phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">MeanAngle</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">angles</span><span style="color: #0000FF;">)</span>
Line 1,847:
 
=={{header|PHP}}==
<syntaxhighlight lang=PHP"php">
<?php
function time2ang($tim) {
Line 1,893:
 
=={{header|PicoLisp}}==
<syntaxhighlight lang=PicoLisp"picolisp">(load "@lib/math.l")
 
(de meanTime (Lst)
Line 1,904:
(tim$ (% (+ Tim 86400) 86400) T) ) )</syntaxhighlight>
{{out|Test}}
<syntaxhighlight lang=PicoLisp"picolisp">: (meanTime '("23:00:17" "23:40:20" "00:12:45" "00:17:19"))
-> "23:47:43"</syntaxhighlight>
 
=={{header|PL/I}}==
<syntaxhighlight lang="pli">*process source attributes xref;
avt: Proc options(main);
/*--------------------------------------------------------------------
Line 1,958:
 
=={{header|PowerShell}}==
<syntaxhighlight lang=PowerShell"powershell">
function Get-MeanTimeOfDay
{
Line 2,026:
}
</syntaxhighlight>
<syntaxhighlight lang=PowerShell"powershell">
[timespan]$meanTimeOfDay = "23:00:17","23:40:20","00:12:45","00:17:19" | Get-MeanTimeOfDay
"Mean time is {0}" -f (Get-Date $meanTimeOfDay.ToString()).ToString("hh:mm:ss tt")
Line 2,036:
 
=={{header|Python}}==
<syntaxhighlight lang="python">from cmath import rect, phase
from math import radians, degrees
 
Line 2,064:
 
=={{header|Racket}}==
<syntaxhighlight lang="racket">
#lang racket
(define (mean-angle/radians as)
Line 2,093:
{{works with|Rakudo|2015.12}}
 
<syntaxhighlight lang=perl6"raku" line>sub tod2rad($_) { [+](.comb(/\d+/) Z* 3600,60,1) * tau / 86400 }
sub rad2tod ($r) {
Line 2,114:
 
=={{header|REXX}}==
<syntaxhighlight lang="rexx">/* REXX ---------------------------------------------------------------
* 25.06.2014 Walter Pachl
* taken from ooRexx using my very aged sin/cos/artan functions
Line 2,220:
Using the methods at [[http://rosettacode.org/wiki/Averages/Mean_angle#Ruby|Averages/Mean angle]]
 
<syntaxhighlight lang="ruby">def time2deg(t)
raise "invalid time" unless m = t.match(/^(\d\d):(\d\d):(\d\d)$/)
hh,mm,ss = m[1..3].map {|e| e.to_i}
Line 2,243:
 
=={{header|Run BASIC}}==
<syntaxhighlight lang="runbasic">global pi
pi = acs(-1)
Line 2,288:
End Function</syntaxhighlight>
=={{header|Rust}}==
<syntaxhighlight lang="rust">
use std::f64::consts::PI;
 
Line 2,358:
 
=={{header|Scala}}==
{{libheader|java.time.LocalTime}}<syntaxhighlight lang=Scala"scala">import java.time.LocalTime
import scala.compat.Platform
 
Line 2,394:
To be self-contained, this starts with the functions from [[Averages/Mean angle]]
 
<syntaxhighlight lang="scheme">
(import (scheme base)
(scheme inexact)
Line 2,457:
{{trans|Ruby}}
Using the '''mean_angle()''' function from: [http://rosettacode.org/wiki/Averages/Mean_angle#Sidef "Averages/Mean angle"]
<syntaxhighlight lang="ruby">func time2deg(t) {
(var m = t.match(/^(\d\d):(\d\d):(\d\d)$/)) || die "invalid time"
var (hh,mm,ss) = m.cap.map{.to_i}...
Line 2,479:
=={{header|SQL}}/{{header|PostgreSQL}}==
{{trans|Python}}
<syntaxhighlight lang=SQL"sql">
--Setup table for testing
CREATE TABLE time_table(times time);
Line 2,501:
=={{header|Swift}}==
 
<syntaxhighlight lang="swift">import Foundation
 
@inlinable public func d2r<T: FloatingPoint>(_ f: T) -> T { f * .pi / 180 }
Line 2,564:
 
=={{header|Tcl}}==
<syntaxhighlight lang="tcl">proc meanTime {times} {
set secsPerRad [expr {60 * 60 * 12 / atan2(0,-1)}]
set sumSin [set sumCos 0.0]
Line 2,587:
=={{header|VBA}}==
Uses Excel and [[Averages/Mean_angle#VBA|mean angle]].
<syntaxhighlight lang="vb">Public Sub mean_time()
Dim angles() As Double
s = [{"23:00:17","23:40:20","00:12:45","00:17:19"}]
Line 2,599:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<syntaxhighlight lang="vbnet">Module Module1
 
Function TimeToDegrees(time As TimeSpan) As Double
Line 2,656:
=={{header|Vlang}}==
{{trans|Wren}}
<syntaxhighlight lang="vlang">import math
 
const inputs = ["23:00:17", "23:40:20", "00:12:45", "00:17:19"]
Line 2,703:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascript">import "/fmt" for Fmt
 
var timeToDegs = Fn.new { |time|
Line 2,744:
 
=={{header|XPL0}}==
<syntaxhighlight lang=XPL0"xpl0">include c:\cxpl\codes;
 
proc NumOut(N); \Display 2-digit N with leading zero
Line 2,788:
=={{header|Yabasic}}==
{{trans|Phix}}
<syntaxhighlight lang=Yabasic"yabasic">sub atan2(y, x)
return 2 * atan((sqrt(x **2 + y ** 2) - x) / y)
end sub
Line 2,839:
to task "Averages/Mean angle" and some on-the-fly
time-to-angle and back conversions.
<syntaxhighlight lang="zkl">var D=Time.Date;
fcn meanT(t1,t2,etc){
ts:=vm.arglist.apply(fcn(hms){
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.