Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Plug-in Framework

, 14 Sep 2010 CPOL
Basic framework for building desktop plug-in applications.
PluginFramework.zip
PluginFramework
Demo
bin
Debug
Plugins
Release
Plugins
Forms
Properties
Settings.settings
DemoFormPlugin
Properties
Settings.settings
DemoUserControlPlugin
Properties
Settings.settings
MBG.Extensions
Collections
Controls
Core
Data
Math
Properties
Xml
MBG.IO
Properties
PluginFramework.Attributes
Properties
PluginFramework.Controls
Properties
Resources
Icons
SettingsIcon.ico
TreeIcon.ico
Images
SettingsImage.png
TreeImage.png
PluginFramework.Interface
Properties
PluginFramework.Utilities
Properties
using System;

namespace MBG.Extensions.Math
{
    //TODO: Test
    public static class Int32Extensions
    {
        public static bool IsMultipleOf(this int i, int numberToCompare)
        {
            return i % numberToCompare == 0;
        }

        /// <summary>
        /// <para>Divide by given divisor and round UP to the next whole number.</para>
        /// <para>For example, 10001 / 9000 = 1.111222 and will be rounded UP to 2</para>
        /// </summary>
        /// <param name="dividend">The dividend</param>
        /// <param name="divisor">The divisor</param>
        /// <returns>Quotient</returns>
        public static int DivideRoundUp(int dividend, int divisor)
        {
            if (divisor == 0)
            {
                throw new DivideByZeroException();
            }
            if (divisor == -1 && dividend == int.MinValue)
            {
                throw new ArithmeticException(string.Concat("divisor = -1 and dividend = ", int.MinValue));
            }

            int roundedTowardsZeroQuotient = dividend / divisor;
            bool dividedEvenly = (dividend % divisor) == 0;
            if (dividedEvenly)
            {
                return roundedTowardsZeroQuotient;
            }

            // At this point we know that divisor was not zero  
            // (because we would have thrown) and we know that  
            // dividend was not zero (because there would have been no remainder) 
            // Therefore both are non-zero.  Either they are of the same sign,  
            // or opposite signs. If they're of opposite sign then we rounded  
            // UP towards zero so we're done. If they're of the same sign then  
            // we rounded DOWN towards zero, so we need to add one.
            bool wasRoundedDown = ((divisor > 0) == (dividend > 0));
            if (wasRoundedDown)
            {
                return roundedTowardsZeroQuotient + 1;
            }
            else
            {
                return roundedTowardsZeroQuotient;
            }
        }

        public static int LowestCommonMultiple(int lhs, int rhs)
        {
            int max = lhs > rhs ? rhs : lhs;

            int lcm = -1;
            for (int i = 2; i <= max; i++)
            {
                if (lhs.IsMultipleOf(i) && rhs.IsMultipleOf(i))
                {
                    lcm = i;
                    break;
                }
            }

            return lcm;
        }

        public static int GreatestCommonDivisor(int lhs, int rhs)
        {
            int i = 0;
            while (true)
            {
                i = lhs % rhs;
                if (i == 0)
                {
                    return rhs;
                }

                lhs = rhs;
                rhs = i;
            }
        }

        public static string ToFraction(this int i)
        {
            return ((double)i).ToFraction();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

gordon_matt
Software Developer VortexSoft
Vietnam Vietnam
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 14 Sep 2010
Article Copyright 2010 by gordon_matt
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid