Skip to content

Commit 7c1b1ed

Browse files
committed
Fix AbsoluteMin.getMinValue to compare by absolute magnitude, not raw value
1 parent 8a20fa9 commit 7c1b1ed

2 files changed

Lines changed: 48 additions & 40 deletions

File tree

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
1-
package com.thealgorithms.maths;
2-
3-
import java.util.Arrays;
4-
1+
package com.thealgorithms.maths;
2+
53
public final class AbsoluteMin {
6-
private AbsoluteMin() {
7-
}
8-
9-
/**
10-
* Compares the numbers given as arguments to get the absolute min value.
11-
*
12-
* @param numbers The numbers to compare
13-
* @return The absolute min value
14-
*/
15-
public static int getMinValue(int... numbers) {
16-
if (numbers.length == 0) {
17-
throw new IllegalArgumentException("Numbers array cannot be empty");
4+
private AbsoluteMin() {
5+
}
6+
7+
/**
8+
* Compares the numbers given as arguments to get the absolute min value.
9+
*
10+
* @param numbers The numbers to compare
11+
* @return The absolute min value
12+
*/
13+
public static int getMinValue(int... numbers) {
14+
if (numbers.length == 0) {
15+
throw new IllegalArgumentException("Numbers array cannot be empty");
16+
}
17+
18+
int minValue = numbers[0];
19+
for (int number : numbers) {
20+
long absoluteNumber = Math.abs((long) number);
21+
long absoluteMinValue = Math.abs((long) minValue);
22+
if (absoluteNumber < absoluteMinValue
23+
|| (absoluteNumber == absoluteMinValue && number < minValue)) {
24+
// For equal absolute values, consistently choose the numerically smaller value.
25+
minValue = number;
26+
}
1827
}
19-
20-
var absMinWrapper = new Object() { int value = numbers[0]; };
21-
22-
Arrays.stream(numbers).skip(1).filter(number -> Math.abs(number) <= Math.abs(absMinWrapper.value)).forEach(number -> absMinWrapper.value = Math.min(absMinWrapper.value, number));
23-
24-
return absMinWrapper.value;
28+
return minValue;
2529
}
2630
}

src/test/java/com/thealgorithms/maths/AbsoluteMinTest.java

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
1-
package com.thealgorithms.maths;
2-
3-
import static org.junit.jupiter.api.Assertions.assertEquals;
4-
import static org.junit.jupiter.api.Assertions.assertThrows;
5-
6-
import org.junit.jupiter.api.Test;
7-
8-
public class AbsoluteMinTest {
9-
1+
package com.thealgorithms.maths;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
public class AbsoluteMinTest {
9+
1010
@Test
1111
void testGetMinValue() {
12+
assertEquals(2, AbsoluteMin.getMinValue(-5, 2));
1213
assertEquals(0, AbsoluteMin.getMinValue(4, 0, 16));
1314
assertEquals(-2, AbsoluteMin.getMinValue(3, -10, -2));
14-
}
15-
16-
@Test
17-
void testGetMinValueWithNoArguments() {
18-
Exception exception = assertThrows(IllegalArgumentException.class, AbsoluteMin::getMinValue);
19-
assertEquals("Numbers array cannot be empty", exception.getMessage());
20-
}
21-
22-
@Test
15+
assertEquals(-2, AbsoluteMin.getMinValue(-5, -2));
16+
assertEquals(2, AbsoluteMin.getMinValue(5, 2));
17+
}
18+
19+
@Test
20+
void testGetMinValueWithNoArguments() {
21+
Exception exception = assertThrows(IllegalArgumentException.class, AbsoluteMin::getMinValue);
22+
assertEquals("Numbers array cannot be empty", exception.getMessage());
23+
}
24+
25+
@Test
2326
void testGetMinValueWithSameAbsoluteValues() {
27+
assertEquals(-3, AbsoluteMin.getMinValue(-3, 3));
2428
assertEquals(-5, AbsoluteMin.getMinValue(-5, 5));
2529
assertEquals(-5, AbsoluteMin.getMinValue(5, -5));
2630
}

0 commit comments

Comments
 (0)