Skip to content

Commit d9f280a

Browse files
committed
Fix nth_root_exact panic on negative even roots - verify with scilab too
1 parent 99e0287 commit d9f280a

2 files changed

Lines changed: 60 additions & 41 deletions

File tree

src/integer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ impl ExactRoots for BigUint {
156156
impl ExactRoots for BigInt {
157157
fn nth_root_exact(&self, n: u32) -> Option<Self> {
158158
// For even roots of negative numbers, return None instead of panicking
159-
if self.is_negative() && n % 2 == 0 {
159+
if self.is_negative() && n.is_multiple_of(2) {
160160
return None;
161161
}
162162

util/validate_with_scilab.sh

Lines changed: 59 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -188,46 +188,65 @@ end
188188
run_rust_test "factorization"
189189
validate_test "Integer Factorization" 9
190190

191-
# # Test 6: Exact roots
192-
# echo -e "${BLUE}6. EXACT ROOTS${NC}"
193-
# run_scilab_test "Exact Roots" '
194-
# // Perfect squares
195-
# squares = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100];
196-
# for i = 1:length(squares)
197-
# n = squares(i);
198-
# root = sqrt(n);
199-
# printf("sqrt(%d) = %d (exact)\n", n, root);
200-
# end
201-
202-
# // Perfect cubes (positive)
203-
# cubes_pos = [1, 8, 27, 64, 125];
204-
# expected_roots_pos = [1, 2, 3, 4, 5];
205-
# for i = 1:length(cubes_pos)
206-
# n = cubes_pos(i);
207-
# root = expected_roots_pos(i);
208-
# printf("cbrt(%d) = %d (exact)\n", n, root);
209-
# end
210-
211-
# // Perfect cubes (negative)
212-
# cubes_neg = [-1, -8, -27, -64, -125];
213-
# expected_roots_neg = [-1, -2, -3, -4, -5];
214-
# for i = 1:length(cubes_neg)
215-
# n = cubes_neg(i);
216-
# root = expected_roots_neg(i);
217-
# printf("cbrt(%d) = %d (exact)\n", n, root);
218-
# end
219-
220-
# // Even roots of negative numbers (should return None)
221-
# // Test case for issue #25: nth_root_exact panic on negative even roots
222-
# printf("-1^(1/2) = None (imaginary)\n");
223-
# printf("-4^(1/2) = None (imaginary)\n");
224-
# printf("-8^(1/4) = None (imaginary)\n");
225-
# printf("-16^(1/4) = None (imaginary)\n");
226-
# printf("-25^(1/2) = None (imaginary)\n");
227-
# '
228-
229-
# run_rust_test "exact_roots"
230-
# validate_test "Exact Roots" 25
191+
# Test 6: Exact roots
192+
echo -e "${BLUE}6. EXACT ROOTS${NC}"
193+
run_scilab_test "Exact Roots" '
194+
// Perfect squares
195+
squares = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100];
196+
for i = 1:length(squares)
197+
n = squares(i);
198+
root = sqrt(n);
199+
printf("sqrt(%d) = %d (exact)\n", n, root);
200+
end
201+
202+
// Perfect cubes (positive)
203+
cubes_pos = [1, 8, 27, 64, 125];
204+
expected_roots_pos = [1, 2, 3, 4, 5];
205+
for i = 1:length(cubes_pos)
206+
n = cubes_pos(i);
207+
root = expected_roots_pos(i);
208+
printf("cbrt(%d) = %d (exact)\n", n, root);
209+
end
210+
211+
// Perfect cubes (negative)
212+
cubes_neg = [-1, -8, -27, -64, -125];
213+
expected_roots_neg = [-1, -2, -3, -4, -5];
214+
for i = 1:length(cubes_neg)
215+
n = cubes_neg(i);
216+
root = expected_roots_neg(i);
217+
printf("cbrt(%d) = %d (exact)\n", n, root);
218+
end
219+
220+
// Even roots of negative numbers (should return None)
221+
// Test case for issue #25: nth_root_exact panic on negative even roots
222+
printf("-1 nth_root_exact(2) = None\n");
223+
printf("-4 nth_root_exact(2) = None\n");
224+
printf("-8 nth_root_exact(4) = None\n");
225+
printf("-16 nth_root_exact(4) = None\n");
226+
printf("-25 nth_root_exact(2) = None\n");
227+
228+
// Odd roots of negative numbers (should work)
229+
printf("-8 nth_root_exact(3) = -2\n");
230+
printf("-27 nth_root_exact(3) = -3\n");
231+
printf("-32 nth_root_exact(5) = -2\n");
232+
233+
// Additional nth_root_exact tests for positive numbers
234+
printf("16 nth_root_exact(4) = 2\n");
235+
printf("32 nth_root_exact(5) = 2\n");
236+
printf("81 nth_root_exact(4) = 3\n");
237+
printf("243 nth_root_exact(5) = 3\n");
238+
239+
// Test various signed integer type limits from patch
240+
printf("-1i8 nth_root_exact(2) = None\n");
241+
printf("-1i16 nth_root_exact(2) = None\n");
242+
printf("-1i32 nth_root_exact(2) = None\n");
243+
printf("-1i64 nth_root_exact(2) = None\n");
244+
printf("-1i128 nth_root_exact(2) = None\n");
245+
printf("-1isize nth_root_exact(2) = None\n");
246+
'
247+
248+
run_rust_test "exact_roots"
249+
validate_test "Exact Roots" 31
231250

232251
# Test 7: Large numbers
233252
echo -e "${BLUE}7. LARGE NUMBERS${NC}"

0 commit comments

Comments
 (0)