Skip to content

Commit

Permalink
fixed a corner-case logic issue when dividing HugeInts
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomHashTags committed May 2, 2023
1 parent e36ba13 commit 063d383
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Sources/huge-numbers/HugeInt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ internal extension HugeInt {
bruh.removeLast()
}
}
if bruh.last == 0 {
while bruh.last == 0 {
if quotient_index < result_count {
quotient_numbers[quotient_index] = 0
quotient_index += 1
Expand Down
8 changes: 7 additions & 1 deletion Tests/huge-numbersTests/huge_numbersTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,10 @@ extension huge_numbersTests {
XCTAssert(quotient == HugeInt("17") && remainder == nil, "test_int_division;quotient=\(quotient);remainder=\(String(describing: remainder))")

(quotient, remainder) = HugeInt("80665") / HugeInt("2")
XCTAssert(quotient == HugeInt("40332"), "test_int_division;quotient=\(quotient);remainder=\(remainder)")
XCTAssert(quotient == HugeInt("40332") && remainder == HugeRemainder(dividend: "1", divisor: "2"), "test_int_division;quotient=\(quotient);remainder=\(String(describing: remainder))")

(quotient, remainder) = HugeInt("1000") / HugeInt("2")
XCTAssert(quotient == HugeInt("500") && remainder == nil, "test_int_division;quotient=\(quotient);remainder=\(String(describing: remainder))")
}
private func test_int_factorial() {
var integer:HugeInt = HugeInt("5")
Expand Down Expand Up @@ -637,6 +639,10 @@ extension huge_numbersTests {
result = HugeFloat("9r6/10") / HugeFloat("2r4/10")
expected_result = HugeFloat("4")
XCTAssert(result == expected_result, "test_float_division;result=\(result);expected_result=\(expected_result)")

result = HugeFloat("1000") / HugeFloat("2")
expected_result = HugeFloat("500")
XCTAssert(result == expected_result, "test_float_division;result=\(result);expected_result=\(expected_result)")
}
}

Expand Down

0 comments on commit 063d383

Please sign in to comment.