diff --git a/.project b/.project index 3cd55b5c..edf7bba4 100644 --- a/.project +++ b/.project @@ -14,4 +14,15 @@ org.eclipse.jdt.core.javanature + + + 1730486517114 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/src/com/jwetherell/algorithms/mathematics/Division.java b/src/com/jwetherell/algorithms/mathematics/Division.java index c133f741..03e155c3 100644 --- a/src/com/jwetherell/algorithms/mathematics/Division.java +++ b/src/com/jwetherell/algorithms/mathematics/Division.java @@ -3,7 +3,12 @@ public class Division { public static final long division(int a, int b) { + if (b == 0){ + throw now IllegalArgumentException ("Denominator is 0."); + } + long result = ((long) a) / ((long) b); + return result; } @@ -11,13 +16,18 @@ public static final long divisionUsingLoop(int a, int b) { int absA = Math.abs(a); int absB = Math.abs(b); + if (b == 0){ + throw now IllegalArgumentException ("Denominator is 0."); + } + long temp = absA; long result = 0; while (temp >= 0) { temp -= absB; if (temp >= 0) result++; - } + } + return (a > 0 && b > 0 || a < 0 && b < 0) ? result : -result; } @@ -25,13 +35,17 @@ public static final long divisionUsingRecursion(int a, int b) { int absA = Math.abs(a); int absB = Math.abs(b); + if (b == 0){ + throw now IllegalArgumentException ("Denominator is 0."); + } + long result = 1; int diff = absA - absB; if (diff > 0 && diff <= 1) { return result; - } else if (diff < 0) { + } else if (diff < 0) { return 0; - } + } result += divisionUsingRecursion(diff, absB); return (a > 0 && b > 0 || a < 0 && b < 0) ? result : -result; @@ -41,6 +55,10 @@ public static final long divisionUsingMultiplication(int a, int b) { int absA = Math.abs(a); int absB = Math.abs(b); + if(b == 0){ + throw now IllegalArgumentException ("Denominator is 0."); + } + int temp = absB; int counter = 0; while (temp <= absA) { @@ -59,6 +77,10 @@ public static final long divisionUsingShift(int a, int b) { int absB = Math.abs(b); int tempA, tempB, counter; + if(b == 0){ + throw now IllegalArgumentException ("Denominator is 0."); + } + long result = 0L; while (absA >= absB) { tempA = absA >> 1; // Right shift "a" @@ -78,6 +100,11 @@ public static final long divisionUsingShift(int a, int b) { public static final long divisionUsingLogs(int a, int b) { long absA = Math.abs(a); long absB = Math.abs(b); + + if(b == 0){ + throw now IllegalArgumentException ("Denominator is 0."); + } + double logBase10A = Math.log10(absA); double logBase10B = Math.log10(absB); double powOf10 = Math.pow(10, (logBase10A - logBase10B));