Hier ist, was ich kam mit:
public struct Odds
{
private int _a;
private int _b;
public int A
{
get
{
return _a;
}
}
public int B
{
get
{
return _b;
}
}
public Odds(int a, int b)
{
this._a = a;
this._b = b;
}
public Odds(double percent)
{
Odds odds = FromPercent(percent);
this._a = odds.A;
this._b = odds.B;
}
public Odds Invert()
{
return new Odds(_b, _a);
}
public double ToPercent()
{
return ToPercent(_a, _b);
}
public static double ToPercent(int a, int b)
{
return ((double)a/(b + a));
}
public static Odds FromPercent(double percent)
{
int multiplier = GetDecimalMultiplier(percent);
return new Odds(1, (multiplier - (int)(percent * multiplier))/(int)(percent * multiplier));
}
private static int GetDecimalMultiplier(double n)
{
if (n > 0.01)
{
return 100;
}
if (n > 0.001)
{
return 100000;
}
if (n > 0.0001)
{
return 100000000;
}
throw new Exception("Number too small!");
}
public override string ToString()
{
return A + "-" + B;
}
}
Odds Prozent:
public static double ToPercent(int a, int b)
{
return ((double)a/(b + a));
}
Prozent auf Odds (nicht perfekt sein kann)
public static Odds FromPercent(double percent)
{
int multiplier = GetDecimalMultiplier(percent);
return new Odds(1, (multiplier - (int)(percent * multiplier))/(int)(percent * multiplier));
}
private static int GetDecimalMultiplier(double n)
{
if (n > 0.01)
{
return 100;
}
if (n > 0.001)
{
return 100000;
}
if (n > 0.0001)
{
return 100000000;
}
throw new Exception("Number too small!");
}
Sie sicher Quoten zu Prozent nicht "zurück (doppelt) a/(b + a)"? Rhetorische Frage. Übrigens werden Phantom-Downvotes mich dazu bringen, diese Frage zu schließen. – Krythic
Definieren Sie die Quoten dann. Ich beschreibe das * 1 in X *, nicht * 1 vs. X (Teile) *. – sascha