Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C# (http://www.go4expert.com/articles/c-sharp-tutorials/)
-   -   Custom Math Class (http://www.go4expert.com/articles/custom-math-class-t28457/)

pein87 28May2012 00:56

Custom Math Class
 
Custom Math Class that can check if a number is even or odd, calculate your rental payment, mean, percent, and percentage of a number. My custom class for some math problems, including a function to calculate payment on a house or rental agreement.

The Code



This class is used to calculate various different types math problems that are not covered in the regular math class.

Code: CSharp

using System;
/* My custom math algorithms for my pet projects */

namespace rOtano
{
    public class roMath
    {
        /* returns a ratio or percent of the two numbers */
        public double ratio(double numer, double denom, int percent = 0)
        {
            double ratio = 0;

            if (numer == 0 || denom == 0)
            {
                return 0;
            }
            else
            {
                switch (percent)
                {
                    case 1:
                        ratio = (numer / denom) * 100;
                        break;
                    case 0:
                        ratio = numer / denom;
                        break;
                    default:
                        ratio = numer / denom;
                        break;
                }
            }

            return ratio;
        }
        /* calculates the number that makes up the percentage of another number */
        public double percentageOf(double denom, double percent)
        {

            return (denom * percent) / 100;

        }
        /* caluculates the hypotenuse of a triangle */
        static double Hypotenuse(double A, double B)
        {

            return Math.Sqrt((A * A) + (B * B));

        }
        /* calculates the rental payment */
        static decimal RentalPayment(decimal Principle, decimal IntRate, decimal PayPerYear, decimal NumYears)
        {

            decimal numer, denom;

            double b, e;

            numer = IntRate * Principle / PayPerYear;

            e = (double)-(PayPerYear * NumYears);

            b = (double)(IntRate / PayPerYear) + 1;

            denom = 1 - (decimal)Math.Pow(b, e);

            return numer / denom;

        }
        /* function determines if the number is even or not and accepts only ints, not decimals */
        static int isEven(int Number)
        {

            return (Number % 2) != 0 ? 0 : 1;

        }
        /* function determines if the number is odd or not and accepts only ints, not decimals */
        static int isOdd(int Number)
        {

            return (Number % 2) != 0 ? 1 : 0;

        }

        public double mean(int[] numbList)
        {
            int i, listSize = numbList.Length;
            double mean = 0;

            if (listSize == 1)
            {
                return 0;
            }
            else
            {

                for (i = 0; i < listSize; i++)
                {
                    mean += numbList[i];
                }
            }

            return mean / listSize;
        }
    }
}



All times are GMT +5.5. The time now is 13:12.