如何模拟除法函数? [英] How do I simulate the divide function?
本文介绍了如何模拟除法函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在不使用C#中的'/'的情况下模拟分割功能?
我能否想办法计算余数?
我开始创建一个函数:
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:
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;
}
推荐答案
请参阅以下链接: -
http://auk-port.blogspot.in/2011/07/division- without-using-division.html [ ^ ]
Refer following link:-
http://auk-port.blogspot.in/2011/07/division-without-using-division.html[^]
最明显的是从第一原则开始:除法是重复减法,有计数。
因此,x / y可以被认为是我可以从x减去y多少次?这就是你所实现的。
但有一种更快的方法,使用班次: http://stackoverflow.com/questions/6532598/division-using-right-shift-for-divider-which-not -power-of-2 [ ^ ]
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[^]
这篇关于如何模拟除法函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文