Skip to content

Commit

Permalink
can now subtract and multiply HugeDecimals
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomHashTags committed Apr 17, 2023
1 parent 592f554 commit d10d117
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ This library was created to remove the decimal precision limitation on floats, w
- factorial
- factors & shared factors
- fraction simplification
- square root
- square root (n)
- infinite precision

## Current limitations
Expand Down
40 changes: 22 additions & 18 deletions Sources/huge-numbers/HugeDecimal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,15 @@ public extension HugeDecimal {
let left_value:HugeInt = left.value, right_value:HugeInt = right.value
let decimal_length:Int = max(left_value.length, right_value.length)
var result:HugeInt = left_value + right_value, result_length:Int = result.length
var quotient:HugeInt? = nil
if result_length > decimal_length {
let difference:Int = result_length-decimal_length
let quotient:HugeInt = HugeInt(is_negative: false, result.numbers[decimal_length..<result_length])
let difference:Int = result_length - decimal_length
quotient = HugeInt(is_negative: false, result.numbers[decimal_length..<result_length])
for _ in 0..<difference {
result.numbers.removeLast()
}
return (HugeDecimal(value: result), quotient)
} else {
return (HugeDecimal(value: result), nil)
}
return (HugeDecimal(value: result), quotient)
}
}
/*
Expand All @@ -150,19 +149,24 @@ public extension HugeDecimal {
Multiplication
*/
public extension HugeDecimal {
static func * (left: HugeDecimal, right: HugeDecimal) -> (quotient: HugeInt?, result: HugeDecimal) { // TODO: finish
var left_integer:HugeInt = left.value, right_integer:HugeInt = right.value
var decimal_places:Int = 0
while left_integer.numbers.first == 0 {
left_integer.numbers.removeFirst()
decimal_places += 1
}
while right_integer.numbers.first == 0 {
right_integer.numbers.removeFirst()
decimal_places += 1
static func * (left: HugeDecimal, right: HugeInt) -> (quotient: HugeInt?, result: HugeDecimal) {
let result_string:String = HugeDecimal.multiply(left: left.value, right: right, decimal_places: left.value.length)
let result:HugeFloat = HugeFloat(result_string)
return (result.integer == HugeInt.zero ? nil : result.integer, result.decimal ?? HugeDecimal.zero)
}
static func * (left: HugeDecimal, right: HugeDecimal) -> (quotient: HugeInt?, result: HugeDecimal) {
let result_string:String = HugeDecimal.multiply(left: left.value, right: right.value, decimal_places: left.value.length + right.value.length)
let result:HugeFloat = HugeFloat(result_string)
return (result.integer == HugeInt.zero ? nil : result.integer, result.decimal ?? HugeDecimal.zero)
}
}
internal extension HugeDecimal {
static func multiply(left: HugeInt, right: HugeInt, decimal_places: Int) -> String {
var result_string:String = (left * right).description
result_string.insert(".", at: result_string.index(result_string.endIndex, offsetBy: -decimal_places))
if result_string[result_string.startIndex] == "." {
result_string.insert("0", at: result_string.startIndex)
}
print("HugeDecimal;*;decimal_places=" + decimal_places.description + ";left_integer=" + left_integer.description + ";right_integer=" + right_integer.description)
var quotient:HugeInt? = nil
return (quotient, left)
return result_string
}
}
20 changes: 18 additions & 2 deletions Tests/huge-numbersTests/huge_numbersTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -549,16 +549,32 @@ extension huge_numbersTests {
XCTAssert(result.description.elementsEqual(expected_result.description), "test_decimal;result=\(result);expected_result=\(expected_result)")

test_decimal_addition()
test_decimal_subtraction()
test_decimal_multiplication()
}
private func test_decimal_addition() {
var decimal:HugeDecimal = HugeDecimal("999")
var (result, quotient):(HugeDecimal, HugeInt?) = decimal + HugeDecimal("1")
var (expected_result, expected_quotient):(HugeDecimal, HugeInt?) = (HugeDecimal("000", remove_leading_zeros: false), HugeInt.one)
XCTAssert(result == expected_result && quotient == expected_quotient, "test_decimal_addition;result=\(result);expected_result=\(expected_result);quotient=\(quotient);expected_quotient=\(expected_quotient)")
XCTAssert(result == expected_result && quotient == expected_quotient, "test_decimal_addition;result=\(result);expected_result=\(expected_result);quotient=\(String(describing: quotient));expected_quotient=\(String(describing: expected_quotient))")

(result, quotient) = HugeDecimal("998") + HugeDecimal("1")
(expected_result, expected_quotient) = (decimal, nil)
XCTAssert(result == expected_result && quotient == expected_quotient, "test_decimal_addition;result=\(result);expected_result=\(expected_result);quotient=\(quotient);expected_quotient=\(expected_quotient)")
XCTAssert(result == expected_result && quotient == expected_quotient, "test_decimal_addition;result=\(result);expected_result=\(expected_result);quotient=\(String(describing: quotient));expected_quotient=\(String(describing: expected_quotient))")
}
private func test_decimal_subtraction() {
var (result, quotient):(HugeDecimal, HugeInt?) = HugeDecimal("999") - HugeDecimal("1")
var (expected_result, expected_quotient):(HugeDecimal, HugeInt?) = (HugeDecimal("998"), nil)
XCTAssert(result == expected_result && quotient == expected_quotient, "test_decimal_subtraction;result=\(result);expected_result=\(expected_result);quotient=\(String(describing: quotient));expected_quotient=\(String(describing: expected_quotient))")
}
private func test_decimal_multiplication() {
var (quotient, result):(HugeInt?, HugeDecimal) = HugeDecimal("999") * HugeDecimal("2")
var (expected_quotient, expected_result):(HugeInt?, HugeDecimal) = (nil, HugeDecimal("1998"))
XCTAssert(result == expected_result && quotient == expected_quotient, "test_decimal_multiplication;result=\(result);expected_result=\(expected_result);quotient=\(String(describing: quotient));expected_quotient=\(String(describing: expected_quotient))")

(quotient, result) = HugeDecimal("999") * HugeInt("2")
(expected_quotient, expected_result) = (HugeInt.one, HugeDecimal("998"))
XCTAssert(result == expected_result && quotient == expected_quotient, "test_decimal_multiplication;result=\(result);expected_result=\(expected_result);quotient=\(String(describing: quotient));expected_quotient=\(String(describing: expected_quotient))")
}
}

Expand Down

0 comments on commit d10d117

Please sign in to comment.