/* foreach does implicit type conversion on elements of a params argument or
Generic.List.
* This is not good.
* Examples of evil follow...
*/
using System;
// I love it when C# is strict with me...
using System.Collections.Generic;
// ...prettier than C++ templates, not slutty like ArrayList.
namespace DoubleUtilities
{
public static class MaxDouble
{
public static double Max(params double[] list)
{
if (list.Length == 0)
return 0.0D;
double m = list[0];
for (int i = 1; i < list.Length; i++)
{
// Taste the whip of love not given lightly...
//int d = list[i]; //Type error reported (double to int
conversion).
double d = list[i]; // The correct type declaration.
if (d > m)
m = d;
}
return m;
}
public static double MadMax(params double[] list)
{
if (list.Length == 0)
return 0.0D;
double m = list[0];
// Her FOG, her amphetamine, and her pearls...
// And she BREAKS just like a LITTLE GIRL
foreach (int d in list) //Type error unreported (double to int
conversion).
if (d > m)
m = d;
return m;
}
//__________________________________________________ ________________________
public static double Max(List<double> list)
{
if (list.Count == 0)
return 0.0D;
double m = list[0];
for (int i = 1; i < list.Count; i++)
{
// Taste the whip of love not given lightly...
//int d = list[i]; //Type error reported (double to int
conversion).
double d = list[i]; // The correct type declaration.
if (d > m)
m = d;
}
return m;
}
public static double MadMax(List<double> list)
{
if (list.Count == 0)
return 0.0D;
double m = list[0];
// Her FOG, her amphetamine, and her pearls...
// And she BREAKS just like a LITTLE GIRL
foreach (int d in list) //Type error unreported (double to int
conversion).
if (d > m)
m = d;
return m;
}
//__________________________________________________ _________________
public static void TestCases()
{
double expected_max = 8.345;
double[] array = { -3.6, 1.1, -2.2, 3.4, 6.6, expected_max, 7.2 };
TestMaxes(array, expected_max);
}
private static void TestMaxes( double[] parameters, double
expected_max )
{
/* Output
parameters={-3.6, 1.1, -2.2, 3.4, 6.6, 8.345, 7.2}
Max( parms double[] list): max=8.345, is_ok=True
Max( List<double> list): max=8.345, is_ok=True
MadMax( parms double[] list): max=8, is_ok=False
MadMax( List<double> list): max=8, is_ok=False
*/
string delimiter = String.Empty;
Console.Write("parameters={");
for (int i = 0; i < parameters.Length; i++)
{
Console.Write("{0}{1}",delimiter, parameters[i].ToString());
delimiter = ", ";
}
Console.WriteLine("}");
double max = Max(parameters);
bool is_ok = Math.Abs(max - expected_max) < 1E-12D; //paranoid
double comparison
Console.WriteLine("Max( parms double[] list): max={0},
is_ok={1}", max, is_ok);
List<double> list = new List<double>(parameters);
max = Max(list);
is_ok = Math.Abs(max - expected_max) < 1E-12D;
Console.WriteLine("Max( List<double> list): max={0}, is_ok={1}",
max, is_ok);
max = MadMax(parameters);
is_ok = Math.Abs(max - expected_max) < 1E-12D;
Console.WriteLine("MadMax( parms double[] list): max={0},
is_ok={1}", max, is_ok);
max = MadMax(list);
is_ok = Math.Abs(max - expected_max) < 1E-12D;
Console.WriteLine("MadMax( List<double> list): max={0},
is_ok={1}", max, is_ok);
}
}
}