Skip to content

Commit

Permalink
can now get the absolute value of a HugeInt/HugeFloat
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomHashTags committed Apr 23, 2023
1 parent 7befc9e commit fd06a1a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
6 changes: 6 additions & 0 deletions Sources/huge-numbers/HugeFloat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,12 @@ public extension HugeFloat {
return HugeFloat(integer: -value.integer, decimal: value.decimal, remainder: value.remainder)
}
}
/*
Misc
*/
func abs(_ float: HugeFloat) -> HugeFloat {
return HugeFloat(integer: abs(float.integer), decimal: float.decimal, remainder: float.remainder)
}
/*
Addition
*/
Expand Down
14 changes: 12 additions & 2 deletions Sources/huge-numbers/HugeInt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ public struct HugeInt : Hashable, Comparable, Codable {
return T.init(description)
}


/// - Warning: This is very resource intensive when using a big number.
public func get_all_factors() -> Set<HugeInt> {
let maximum:HugeInt = (self / 2).quotient
Expand All @@ -107,7 +106,15 @@ public struct HugeInt : Hashable, Comparable, Codable {
/// - Warning: This assumes this number is less than or equal to the given number; can be very resource intensive when using big numbers.
public func get_shared_factors(_ integer: HugeInt) -> Set<HugeInt>? {
let (self_array, other_array):(Set<HugeInt>, Set<HugeInt>) = (get_all_factors(), integer.get_factors(maximum: self))
let array:Set<HugeInt> = self_array.filter({ other_array.contains($0) })
let bigger_array:Set<HugeInt>, smaller_array:Set<HugeInt>
if self_array.count > other_array.count {
bigger_array = self_array
smaller_array = other_array
} else {
bigger_array = other_array
smaller_array = self_array
}
let array:Set<HugeInt> = bigger_array.filter({ smaller_array.contains($0) })
return array.isEmpty ? nil : array
}

Expand Down Expand Up @@ -290,6 +297,9 @@ public extension HugeInt {
/*
Misc
*/
func abs(_ integer: HugeInt) -> HugeInt {
return HugeInt(is_negative: false, integer.numbers)
}
internal extension HugeInt {
static func left_int_is_bigger(left: HugeInt, right: HugeInt) -> Bool {
return get_bigger_int(left: left, right: right).left_is_bigger
Expand Down
9 changes: 9 additions & 0 deletions Sources/huge-numbers/HugeRemainder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,15 @@ internal extension HugeRemainder {
let left_divisor:HugeInt = left.divisor, right_divisor:HugeInt = right.divisor
if left_divisor == right_divisor {
return (left_divisor, true, nil, nil)
/*} else if let max_shared_factor:HugeInt = left_divisor.get_shared_factors(right_divisor)?.max() { // TODO: fix? | makes performance significantly worse, but remainder is simplified
let left_divisor_is_max:Bool = left_divisor == max_shared_factor
if left_divisor_is_max {
let quotient:HugeInt = (right_divisor / left_divisor).quotient
return (right_divisor, false, quotient, HugeInt.one)
} else {
let quotient:HugeInt = (left_divisor / right_divisor).quotient
return (left_divisor, false, HugeInt.one, quotient)
}*/
} else {
return (left_divisor * right_divisor, false, right_divisor, left_divisor)
}
Expand Down
9 changes: 5 additions & 4 deletions Tests/huge-numbersTests/huge_numbersTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ final class huge_numbersTests: XCTestCase {

private func test_benchmarks() async throws {
guard #available(macOS 13.0, iOS 16.0, *) else { return }
let left1:HugeInt = HugeInt("928352873512820358268047"), right1:HugeInt = HugeInt("928352873512820358285")
try await benchmark_compare_is_faster(key1: "HugeInt1.add", {
//let result:HugeInt = left1 + right1
}, key2: "HugeInt2.add", code2: {
let left1:HugeRemainder = HugeRemainder(dividend: "5", divisor: "10"), right1:HugeRemainder = HugeRemainder(dividend: "2", divisor: "30")
try await benchmark_compare_is_faster(key1: "HugeRemainder.add1", {
let result:HugeRemainder = left1 + right1
}, key2: "HugeRemainder.add2", code2: {
//let result:HugeRemainder = HugeRemainder.add2(left: left1, right: right1)
})
}
}
Expand Down

0 comments on commit fd06a1a

Please sign in to comment.