I thought that, when James Anderson announced the invention of nullity (i.e. Φ, or 0/0), we had finally gotten past the whole "impossibility" of dividing by zero. I don't know about you, but after installing the latest service pack including the "nullity patch," my programs happily hum along when they divide by zero. So why then did Mike C.'s predecessor bother with a DivisionWithZero method ... and why did he do it like this...
/// <summary> /// This function divides two values and notices if either of values is zero and creates /// possibility to run division without having to get trouble with zero values. It also /// remembers to keep the sign digit right. /// </summary> /// <param name="dividend">Dividend number</param> /// <param name="divider">Divider number</param> /// <returns></returns> static public Double DivisionWithZero(Double dividend, Double divider) { Double value = 0; Double separateZero = 0; if (dividend > divider) { separateZero = dividend - divider + 10; } else if (dividend < divider) { separateZero = divider - dividend + 10; } else { separateZero = 10; } if ((dividend + separateZero) == 0 || (divider + separateZero) == 0) { separateZero++; if ((dividend + separateZero) == 0 || (divider + separateZero) == 0) { separateZero++; } } value = (separateZero + dividend) / (separateZero + divider); if ((dividend < 0 && divider < 0 && value < 0) || (dividend > 0 && divider < 0 && value > 0) || (dividend < 0 && divider > 0 && value > 0)) { value = (-1) * value; } return value; }
[Advertisement]
BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!