diff --git a/src/integer.rs b/src/integer.rs index 633f6ce..54efcbd 100644 --- a/src/integer.rs +++ b/src/integer.rs @@ -156,7 +156,7 @@ impl ExactRoots for BigUint { impl ExactRoots for BigInt { fn nth_root_exact(&self, n: u32) -> Option { // 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; } diff --git a/util/validate_with_scilab.sh b/util/validate_with_scilab.sh index a289d1f..bc28c33 100755 --- a/util/validate_with_scilab.sh +++ b/util/validate_with_scilab.sh @@ -188,46 +188,65 @@ end run_rust_test "factorization" validate_test "Integer Factorization" 9 -# # Test 6: Exact roots -# echo -e "${BLUE}6. EXACT ROOTS${NC}" -# run_scilab_test "Exact Roots" ' -# // Perfect squares -# squares = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]; -# for i = 1:length(squares) -# n = squares(i); -# root = sqrt(n); -# printf("sqrt(%d) = %d (exact)\n", n, root); -# end +# Test 6: Exact roots +echo -e "${BLUE}6. EXACT ROOTS${NC}" +run_scilab_test "Exact Roots" ' +// Perfect squares +squares = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]; +for i = 1:length(squares) + n = squares(i); + root = sqrt(n); + printf("sqrt(%d) = %d (exact)\n", n, root); +end -# // Perfect cubes (positive) -# cubes_pos = [1, 8, 27, 64, 125]; -# expected_roots_pos = [1, 2, 3, 4, 5]; -# for i = 1:length(cubes_pos) -# n = cubes_pos(i); -# root = expected_roots_pos(i); -# printf("cbrt(%d) = %d (exact)\n", n, root); -# end +// Perfect cubes (positive) +cubes_pos = [1, 8, 27, 64, 125]; +expected_roots_pos = [1, 2, 3, 4, 5]; +for i = 1:length(cubes_pos) + n = cubes_pos(i); + root = expected_roots_pos(i); + printf("cbrt(%d) = %d (exact)\n", n, root); +end -# // Perfect cubes (negative) -# cubes_neg = [-1, -8, -27, -64, -125]; -# expected_roots_neg = [-1, -2, -3, -4, -5]; -# for i = 1:length(cubes_neg) -# n = cubes_neg(i); -# root = expected_roots_neg(i); -# printf("cbrt(%d) = %d (exact)\n", n, root); -# end +// Perfect cubes (negative) +cubes_neg = [-1, -8, -27, -64, -125]; +expected_roots_neg = [-1, -2, -3, -4, -5]; +for i = 1:length(cubes_neg) + n = cubes_neg(i); + root = expected_roots_neg(i); + printf("cbrt(%d) = %d (exact)\n", n, root); +end -# // Even roots of negative numbers (should return None) -# // Test case for issue #25: nth_root_exact panic on negative even roots -# printf("-1^(1/2) = None (imaginary)\n"); -# printf("-4^(1/2) = None (imaginary)\n"); -# printf("-8^(1/4) = None (imaginary)\n"); -# printf("-16^(1/4) = None (imaginary)\n"); -# printf("-25^(1/2) = None (imaginary)\n"); -# ' +// Even roots of negative numbers (should return None) +// Test case for issue #25: nth_root_exact panic on negative even roots +printf("-1 nth_root_exact(2) = None\n"); +printf("-4 nth_root_exact(2) = None\n"); +printf("-8 nth_root_exact(4) = None\n"); +printf("-16 nth_root_exact(4) = None\n"); +printf("-25 nth_root_exact(2) = None\n"); -# run_rust_test "exact_roots" -# validate_test "Exact Roots" 25 +// Odd roots of negative numbers (should work) +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 echo -e "${BLUE}7. LARGE NUMBERS${NC}"