Averages/Mean time of day: Difference between revisions

Updated C#
(→‎{{header|Vlang}}: Rename "Vlang" in "V (Vlang)")
(Updated C#)
Line 407:
using System.Linq;
using System.Text;
using static System.Math;
 
namespace RosettaCode;
 
class Program
{
private const int SecondsPerDay = 60 * 60 * 24;
class Program
 
static void Main(string[] args)
{
List<double>var digitimes = new List<doubleTimeSpan>();
static void Main(string[] args)
{
Func<TimeSpan, double> TimeToDegrees = (time) =>
360 * time.Hours / 24.0 +
360 * time.Minutes / (24 * 60.0) +
360 * time.Seconds / (24 * 3600.0);
Func<List<double>, double> MeanAngle = (angles) =>
{
double y_part = 0.0d, x_part = 0.0d;
int numItems = angles.Count;
 
for Console.WriteLine(int"Enter itimes, =end 0;with ino <input: numItems"); i++)
while (true) {
string input x_part += MathConsole.CosReadLine(angles[i] * Math.PI / 180);
} whileif (!string.IsNullOrWhiteSpace(input)) break;
y_part += Math.Sin(angles[i] * Math.PI / 180);
if (TimeSpan.TryParse(input, out var digitime)) }{
360 * timedigitimes.Hours / 24.0 +Add(digitime);
} else {
Console.WriteLine("Seems this is wrong input: ignoring time");
}
{}
if(digitimes.Count() > 0)
Console.WriteLine($"The mean time is : {0}", TimeFromDegrees(360 + MeanAngleMeanTime(digitimes))}");
}
 
public static TimeSpan MeanTime(IEnumerable<TimeSpan> ts) => FromDegrees(MeanAngle(ts.Select(ToDegrees)));
return Math.Atan2(y_part / numItems, x_part / numItems) * 180 / Math.PI;
public static double ToDegrees(TimeSpan ts) => ts.TotalSeconds * 360d / SecondsPerDay;
};
public static TimeSpan FromDegrees(double degrees) => TimeSpan.FromSeconds((int)(degrees * SecondsPerDay / 360));
Func<double, TimeSpan> TimeFromDegrees = (angle) =>
new TimeSpan(
(int)(24 * 60 * 60 * angle / 360) / 3600,
((int)(24 * 60 * 60 * angle / 360) % 3600 - (int)(24 * 60 * 60 * angle / 360) % 60) / 60,
(int)(24 * 60 * 60 * angle / 360) % 60);
List<double> digitimes = new List<double>();
TimeSpan digitime;
string input;
 
public static double MeanAngle(IEnumerable<double> angles)
Console.WriteLine("Enter times, end with no input: ");
do{
var x = angles.Average(a {=> Cos(a * PI / 180));
var y = angles.Average(a => Sin(a * PI input/ = Console.ReadLine(180));
return (Atan2(y, x) * 180 / PI + if360) (!(string.IsNullOrWhiteSpace(input)))% 360;
{
if (TimeSpan.TryParse(input, out digitime))
digitimes.Add(TimeToDegrees(digitime));
else
Console.WriteLine("Seems this is wrong input: ignoring time");
}
} while (!string.IsNullOrWhiteSpace(input));
 
if(digitimes.Count() > 0)
Console.WriteLine("The mean time is : {0}", TimeFromDegrees(360 + MeanAngle(digitimes)));
}
}
}
196

edits