Click here to Skip to main content
15,891,629 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
How can I simulate the Divide function without using '/' in C#?
Can I please have a way to calculate the remainder??

I started creating a function:
C#
public static double Divide(double left, double right)
{
    double count = 0;
    double num = right;
    while (num != 0)
    {
        num -= left;

        if (num < left)
        {
            count += left % num;
        }
        else
        {
            count += 1;
        }
    }

    return count;
}
Posted
Updated 5-Feb-14 22:15pm
v4

 
Share this answer
 
The most obvious was is to start from first principles: A divide is a repeated subtraction, with counting.
So "x / y" can be thought of as "how many times can I subtract y from x?" and that's what you have implemented.

But there is a faster method, using shifts: http://stackoverflow.com/questions/6532598/division-using-right-shift-for-divider-which-not-power-of-2[^]
 
Share this answer
 
Comments
The Kcrownikown 6-Feb-14 5:29am    
How can I get the remainder?

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900