Mathematics are in Mad Library targeted for now on fractions, proportionalities and percentage.

2.1 Fractions

Fractions are frequently missing in code when you need to use some mathematical or physical equations and you need to be precise. Mad Library offers you Fraction class which is documented and prepared for normal use. Operators are set for basic mathematical operations and and conversions.

Fraction f = new Fraction(10, 20);
f += new Fraction(1, 2);
f /= new Fraction(1, 2);
f *= new Fraction(15, 20);
double res = f;

2.2 Direct proportionality

Sometimes you need to use direct proportionality in your code. Direct proportionality is simpler than inverse and it seems like you don’t need to use some external codes for it in your project. But Mad Library is sure that you sometimes needed use some equations or functions that are really simple but later you found out that you spent lot of time when you were working on it. So down in this part of document is written example of that, what Mad Library offers you in this problematic.

DirectProportionality proportionality = new DirectProportionality(3);
double x = proportionality.GetX(36);
double y = proportionality.GetY(x);
double coefficient = MadLibrary.Math.DirectProportionality.GetCoefficient(x, y);
Console.WriteLine(proportionality.Coefficient == coefficient);

2.3 Inverse proportionality

As it’s written higher in this document, in programming are sometimes occurring some easy problems about which you now how to easily solve them, but you must spent lot of time when you’re working on it. Inverse proportionality is bit more difficult than direct, but for computer it’s almost same. Mad Library offers you solution for this kind of proportionality.

InverseProportionality prop = new InverseProportionality(10);
double x = prop.GetX(10);
double y = prop.GetY(x);
double coefficient = MadLibrary.Math.InverseProportionality.GetCoefficient(x, y);
Console.WriteLine(coefficient == prop.Coefficient);

2.4 Percentage

Calculation of percentage is also simple and it’s also missing in .NET 4.5 library.

Percentage percentage = new Percentage(500, 50);
double bas = percentage.GetBase(50);
double val = percentage.GetValue(bas);
Console.WriteLine(percentage == MadLibrary.Math.Percentage.GetPercentage(bas, val));

Last edited Jun 16, 2012 at 3:32 PM by FrewCen, version 3


No comments yet.