Skip to content

Commit

Permalink
can now += and -= for HugeFloat, HugeDecimal, and `HugeRemain…
Browse files Browse the repository at this point in the history
…der`
  • Loading branch information
RandomHashTags committed Apr 23, 2023
1 parent 90b4369 commit 019355f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
10 changes: 9 additions & 1 deletion Sources/huge-numbers/HugeDecimal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public extension HugeDecimal {
Addition
*/
public extension HugeDecimal {
static func + (left: HugeDecimal, right: HugeDecimal) -> (result: HugeDecimal, quotient: HugeInt?) {
static func + (left: HugeDecimal, right: HugeDecimal) -> (result: HugeDecimal, quotient: HugeInt?) { // TODO: support addition of repeating numbers
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
Expand All @@ -136,6 +136,10 @@ public extension HugeDecimal {
}
return (HugeDecimal(value: result), quotient)
}

static func += (left: inout HugeDecimal, right: HugeDecimal) { // TODO: support addition of repeating numbers
left.value += right.value
}
}
/*
Subtraction
Expand All @@ -144,6 +148,10 @@ public extension HugeDecimal {
static func - (left: HugeDecimal, right: HugeDecimal) -> (result: HugeDecimal, quotient: HugeInt?) {
return left + -right
}

static func -= (left: inout HugeDecimal, right: HugeDecimal) { // TODO: support subtraction of repeating numbers
left.value -= right.value
}
}
/*
Multiplication
Expand Down
18 changes: 18 additions & 0 deletions Sources/huge-numbers/HugeFloat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,15 @@ public extension HugeFloat {
static func + (left: HugeFloat, right: any BinaryInteger) -> HugeFloat {
return left + HugeFloat(right)
}

static func += (left: inout HugeFloat, right: HugeFloat) {
left.integer += right.integer
if left.decimal != nil {
left.decimal! += right.decimal ?? HugeDecimal.zero
} else if left.remainder != nil {
left.remainder! += right.remainder ?? HugeRemainder.zero
}
}
}
/*
Subtraction
Expand All @@ -259,6 +268,15 @@ public extension HugeFloat {
static func - (left: HugeFloat, right: HugeFloat) -> HugeFloat {
return left + -right
}

static func -= (left: inout HugeFloat, right: HugeFloat) {
left.integer -= right.integer
if left.decimal != nil {
left.decimal! -= right.decimal ?? HugeDecimal.zero
} else if left.remainder != nil {
left.remainder! -= right.remainder ?? HugeRemainder.zero
}
}
}
/*
Multiplication
Expand Down
38 changes: 38 additions & 0 deletions Sources/huge-numbers/HugeRemainder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,25 @@ public extension HugeRemainder {
static func + (left: HugeRemainder, right: HugeInt) -> HugeRemainder {
return left + HugeRemainder(dividend: right, divisor: HugeInt.one)
}

static func += (left: inout HugeRemainder, right: HugeRemainder) {
if left == HugeRemainder.zero {
left.dividend = right.dividend
left.divisor = right.divisor
} else if right == HugeRemainder.zero {
return
} else {
let (common_denominator, are_equal, left_multiplier, right_multiplier):(HugeInt, Bool, HugeInt?, HugeInt?) = get_common_denominator(left: left, right: right)
if are_equal {
left.dividend += right.dividend
} else {
let left_dividend:HugeInt = left.dividend, right_dividend:HugeInt = right.dividend
let left_result:HugeInt = left_dividend * left_multiplier!, right_result:HugeInt = right_dividend * right_multiplier!
left.dividend = left_result + right_result
left.divisor = common_denominator
}
}
}
}
/*
Subtraction
Expand All @@ -221,6 +240,25 @@ public extension HugeRemainder {
static func - (left: HugeRemainder, right: HugeInt) -> HugeRemainder {
return left - HugeRemainder(dividend: right, divisor: HugeInt.one)
}

static func -= (left: inout HugeRemainder, right: HugeRemainder) {
if left == HugeRemainder.zero {
left.dividend = right.dividend
left.divisor = right.divisor
} else if right == HugeRemainder.zero {
return
} else {
let (common_denominator, are_equal, left_multiplier, right_multiplier):(HugeInt, Bool, HugeInt?, HugeInt?) = get_common_denominator(left: left, right: right)
if are_equal {
left.dividend -= right.dividend
} else {
let left_dividend:HugeInt = left.dividend, right_dividend:HugeInt = right.dividend
let left_result:HugeInt = left_dividend * left_multiplier!, right_result:HugeInt = right_dividend * right_multiplier!
left.dividend = left_result - right_result
left.divisor = common_denominator
}
}
}
}
/*
Multiplication
Expand Down

0 comments on commit 019355f

Please sign in to comment.