mirror of
https://github.com/uutils/num-prime.git
synced 2026-06-10 16:12:35 -07:00
Fix nth_root_exact panic on negative even roots - verify with scilab too
This commit is contained in:
+1
-1
@@ -156,7 +156,7 @@ impl ExactRoots for BigUint {
|
|||||||
impl ExactRoots for BigInt {
|
impl ExactRoots for BigInt {
|
||||||
fn nth_root_exact(&self, n: u32) -> Option<Self> {
|
fn nth_root_exact(&self, n: u32) -> Option<Self> {
|
||||||
// For even roots of negative numbers, return None instead of panicking
|
// For even roots of negative numbers, return None instead of panicking
|
||||||
if self.is_negative() && n % 2 == 0 {
|
if self.is_negative() && n.is_multiple_of(2) {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -188,46 +188,65 @@ end
|
|||||||
run_rust_test "factorization"
|
run_rust_test "factorization"
|
||||||
validate_test "Integer Factorization" 9
|
validate_test "Integer Factorization" 9
|
||||||
|
|
||||||
# # Test 6: Exact roots
|
# Test 6: Exact roots
|
||||||
# echo -e "${BLUE}6. EXACT ROOTS${NC}"
|
echo -e "${BLUE}6. EXACT ROOTS${NC}"
|
||||||
# run_scilab_test "Exact Roots" '
|
run_scilab_test "Exact Roots" '
|
||||||
# // Perfect squares
|
// Perfect squares
|
||||||
# squares = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100];
|
squares = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100];
|
||||||
# for i = 1:length(squares)
|
for i = 1:length(squares)
|
||||||
# n = squares(i);
|
n = squares(i);
|
||||||
# root = sqrt(n);
|
root = sqrt(n);
|
||||||
# printf("sqrt(%d) = %d (exact)\n", n, root);
|
printf("sqrt(%d) = %d (exact)\n", n, root);
|
||||||
# end
|
end
|
||||||
|
|
||||||
# // Perfect cubes (positive)
|
// Perfect cubes (positive)
|
||||||
# cubes_pos = [1, 8, 27, 64, 125];
|
cubes_pos = [1, 8, 27, 64, 125];
|
||||||
# expected_roots_pos = [1, 2, 3, 4, 5];
|
expected_roots_pos = [1, 2, 3, 4, 5];
|
||||||
# for i = 1:length(cubes_pos)
|
for i = 1:length(cubes_pos)
|
||||||
# n = cubes_pos(i);
|
n = cubes_pos(i);
|
||||||
# root = expected_roots_pos(i);
|
root = expected_roots_pos(i);
|
||||||
# printf("cbrt(%d) = %d (exact)\n", n, root);
|
printf("cbrt(%d) = %d (exact)\n", n, root);
|
||||||
# end
|
end
|
||||||
|
|
||||||
# // Perfect cubes (negative)
|
// Perfect cubes (negative)
|
||||||
# cubes_neg = [-1, -8, -27, -64, -125];
|
cubes_neg = [-1, -8, -27, -64, -125];
|
||||||
# expected_roots_neg = [-1, -2, -3, -4, -5];
|
expected_roots_neg = [-1, -2, -3, -4, -5];
|
||||||
# for i = 1:length(cubes_neg)
|
for i = 1:length(cubes_neg)
|
||||||
# n = cubes_neg(i);
|
n = cubes_neg(i);
|
||||||
# root = expected_roots_neg(i);
|
root = expected_roots_neg(i);
|
||||||
# printf("cbrt(%d) = %d (exact)\n", n, root);
|
printf("cbrt(%d) = %d (exact)\n", n, root);
|
||||||
# end
|
end
|
||||||
|
|
||||||
# // Even roots of negative numbers (should return None)
|
// Even roots of negative numbers (should return None)
|
||||||
# // Test case for issue #25: nth_root_exact panic on negative even roots
|
// Test case for issue #25: nth_root_exact panic on negative even roots
|
||||||
# printf("-1^(1/2) = None (imaginary)\n");
|
printf("-1 nth_root_exact(2) = None\n");
|
||||||
# printf("-4^(1/2) = None (imaginary)\n");
|
printf("-4 nth_root_exact(2) = None\n");
|
||||||
# printf("-8^(1/4) = None (imaginary)\n");
|
printf("-8 nth_root_exact(4) = None\n");
|
||||||
# printf("-16^(1/4) = None (imaginary)\n");
|
printf("-16 nth_root_exact(4) = None\n");
|
||||||
# printf("-25^(1/2) = None (imaginary)\n");
|
printf("-25 nth_root_exact(2) = None\n");
|
||||||
# '
|
|
||||||
|
|
||||||
# run_rust_test "exact_roots"
|
// Odd roots of negative numbers (should work)
|
||||||
# validate_test "Exact Roots" 25
|
printf("-8 nth_root_exact(3) = -2\n");
|
||||||
|
printf("-27 nth_root_exact(3) = -3\n");
|
||||||
|
printf("-32 nth_root_exact(5) = -2\n");
|
||||||
|
|
||||||
|
// Additional nth_root_exact tests for positive numbers
|
||||||
|
printf("16 nth_root_exact(4) = 2\n");
|
||||||
|
printf("32 nth_root_exact(5) = 2\n");
|
||||||
|
printf("81 nth_root_exact(4) = 3\n");
|
||||||
|
printf("243 nth_root_exact(5) = 3\n");
|
||||||
|
|
||||||
|
// Test various signed integer type limits from patch
|
||||||
|
printf("-1i8 nth_root_exact(2) = None\n");
|
||||||
|
printf("-1i16 nth_root_exact(2) = None\n");
|
||||||
|
printf("-1i32 nth_root_exact(2) = None\n");
|
||||||
|
printf("-1i64 nth_root_exact(2) = None\n");
|
||||||
|
printf("-1i128 nth_root_exact(2) = None\n");
|
||||||
|
printf("-1isize nth_root_exact(2) = None\n");
|
||||||
|
'
|
||||||
|
|
||||||
|
run_rust_test "exact_roots"
|
||||||
|
validate_test "Exact Roots" 31
|
||||||
|
|
||||||
# Test 7: Large numbers
|
# Test 7: Large numbers
|
||||||
echo -e "${BLUE}7. LARGE NUMBERS${NC}"
|
echo -e "${BLUE}7. LARGE NUMBERS${NC}"
|
||||||
|
|||||||
Reference in New Issue
Block a user