Skip to content

Commit

Permalink
Added more tests and added minimum requirements for OSes.
Browse files Browse the repository at this point in the history
  • Loading branch information
mgriebling committed Oct 12, 2024
1 parent 43f967f commit 4414e0e
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 24 deletions.
Binary file not shown.
8 changes: 4 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import PackageDescription

let package = Package(
name: "BigDecimal",
// platforms: [
// .macOS("13.3"), .iOS("16.4"), .macCatalyst("13.3"), .tvOS("16.4"),
// .watchOS("9.4")
// ],
platforms: [
.macOS("13.3"), .iOS("16.4"), .macCatalyst("13.3"), .tvOS("16.4"),
.watchOS("9.4")
],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ Its functionality falls in the following categories:
latter having a `signaling` option.

## Dependencies
BigDecimal requires Swift from macOS 15.0+, iOS 18.0+, macCatalyst 15.0+,
tvOS 18.0+, or watchOS 11.0+. It also requires that the `Int` type be a 64-bit
BigDecimal requires Swift from macOS 15.0+, iOS 13.3+, macCatalyst 13.3+,
tvOS 16.4+, or watchOS 9.4+. It also requires that the `Int` type be a 64-bit
type.

The BigDecimal package depends on the BigInt and swift-numerics packages.
It also uses the built-in UInt128 which is part of the new OS releases.
The BigDecimal package depends on the BigInt, UInt128, and swift-numerics packages.

```
dependencies: [
.package(url: "https://github.com/mgriebling/BigInt.git", from: "2.2.0")
.package(url: "https://github.com/apple/swift-numerics", from: "1.0.0")
.package(url: "https://github.com/apple/swift-numerics", from: "1.0.0"),
.package(url: "https://github.com/mgriebling/UInt128.git", from: "3.1.5")
]
```

Expand All @@ -54,7 +54,7 @@ In your project's Package.swift file add a dependency like

```
dependencies: [
.package(url: "https://github.com/mgriebling/BigDecimal.git", from: "2.2.0"),
.package(url: "https://github.com/mgriebling/BigDecimal.git", from: "3.0.2"),
]
```

Expand Down
4 changes: 1 addition & 3 deletions Sources/BigDecimal/DecimalMath/DecimalMath-Simple.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
// Created by Mike Griebling on 10.07.2023.
//

// import RealModule - temporarily removed due to playground incompatibility

/// `Real` number protocol compliance using a simplified interface with
/// a fixed precision given by a global rounding context: `BigDecimal.mc`.
extension BigDecimal { // : Real {
extension BigDecimal {

@inlinable
public static func atan2(y: BigDecimal, x: BigDecimal) -> BigDecimal {
Expand Down
27 changes: 27 additions & 0 deletions Tests/BigDecimalTests/TestCompare.swift
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,31 @@ class TestCompare: XCTestCase {
XCTAssertTrue(-x > -y)
XCTAssertTrue(-x >= -y)
}

func test8() throws {
let x = BigDecimal("1")
let y = BigDecimal("2")
XCTAssertFalse(x.isEqual(to: y))
XCTAssertTrue(x.isLess(than: y))
XCTAssertTrue(x.isLessThanOrEqualTo(y))

XCTAssertFalse((-x).isEqual(to: y))
XCTAssertTrue((-x).isLess(than: y))
XCTAssertTrue((-x).isLessThanOrEqualTo(y))

XCTAssertFalse(x.isEqual(to: -y))
XCTAssertFalse(x.isLess(than: -y))
XCTAssertFalse(x.isLessThanOrEqualTo(-y))

XCTAssertFalse((-x).isEqual(to: -y))
XCTAssertFalse((-x).isLess(than: -y))
XCTAssertFalse((-x).isLessThanOrEqualTo(-y))

XCTAssertFalse(x.isNaN)
XCTAssertFalse(x.isZero)
XCTAssertTrue(x.isPositive)
XCTAssertTrue(x.isFinite)
XCTAssertTrue(x.isNormal)
XCTAssertTrue(x.isCanonical)
}
}
37 changes: 37 additions & 0 deletions Tests/BigDecimalTests/TestHyperbolic.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// TestHyperbolic.swift
// BigDecimal
//
// Created by Mike Griebling on 11.10.2024.
//

import XCTest
@testable import BigDecimal

final class TestHyperbolic: XCTestCase {

override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
}

override func tearDownWithError() throws {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}

func testExample() throws {
XCTAssertEqual(BigDecimal.sinh(.one).asString(), "1.175201193643801456882381850595601")
XCTAssertEqual(BigDecimal.cosh(.one).asString(), "1.543080634815243778477905620757062")
XCTAssertEqual(BigDecimal.tanh(.one).asString(), "0.7615941559557648881194582826047937")
XCTAssertEqual(BigDecimal.asinh(BigDecimal.sinh(.one)).asString(), "1.000000000000000000000000000000000")
XCTAssertEqual(BigDecimal.acosh(BigDecimal.cosh(.one)).asString(), "1.000000000000000000000000000000000")
XCTAssertEqual(BigDecimal.atanh(BigDecimal.tanh(.one)).asString(), "1.000000000000000000000000000000000")
}

func testPerformanceExample() throws {
// This is an example of a performance test case.
self.measure {
// Put the code you want to measure the time of here.
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import XCTest
import BigDecimal

final class TestLogarit: XCTestCase {
final class TestLogarithm: XCTestCase {

override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
Expand All @@ -23,6 +23,11 @@ final class TestLogarit: XCTestCase {
XCTAssertEqual(result1.round(.decimal32).asString(), "1.000434")
let result2 = BigDecimal.log(BigDecimal(10.01))
XCTAssertEqual(result2.round(.decimal32).asString(), "2.303585")

let result3 = BigDecimal.log(onePlus: BigDecimal(10.01))
XCTAssertEqual(result3.round(.decimal64).asString(), "2.398803950734589")
let result4 = BigDecimal.log2(BigDecimal(10.01))
XCTAssertEqual(result4.round(.decimal64).asString(), "3.323370069061269")
}

func testPerformanceExample() throws {
Expand Down
10 changes: 8 additions & 2 deletions Tests/BigDecimalTests/TestOperations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,15 @@ class TestOperations: XCTestCase {
let sqrt10000 = BigDecimal.sqrt(10000, decimal128)
XCTAssertEqual(sqrt10000, BigDecimal(100))

let cbrt = BigDecimal.root(2, 3, decimal138)
let cbrt = BigDecimal.root(2, 3, decimal128)
XCTAssertEqual(cbrt.round(decimal128).description,
"1.259921049894873164767210607278228")

let cubed = BigDecimal.pow(cbrt, 3, decimal128)
XCTAssertEqual(cubed.description, "2.000000000000000000000000000000000")
XCTAssertEqual(cubed.description, "1.999999999999999999999999999999998")

let bernie = BigDecimal.bernoulli(20, decimal128)
XCTAssertEqual(bernie.asString(), "-529.1242424242424242424242424242424")

let power = BigDecimal.pow(BigDecimal("1.2"), BigDecimal("1000.5"), decimal128)
XCTAssertEqual(power.description,
Expand All @@ -90,6 +93,9 @@ class TestOperations: XCTestCase {
let fact1000limited = BigDecimal.factorial(1000, decimal128)
XCTAssertEqual(fact1000limited.description,
"4.023872600770937735437024339230040E+2567")

let fact = BigDecimal.factorial(BigDecimal(1000), decimal128)
XCTAssertEqual(fact.description, "4.023872600770937735437024339230040E+2567")

let gammaHalf = BigDecimal.gamma(BigDecimal("0.5"), decimal128)
XCTAssertEqual(gammaHalf.description,
Expand Down
24 changes: 16 additions & 8 deletions Tests/BigDecimalTests/TestPow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@ import XCTest
@testable import BigDecimal

class TestPow: XCTestCase {
func testPow() throws {
XCTAssertEqual(BigDecimal("5.5").pow(-3, .decimal128).asString(), "0.006010518407212622088655146506386176")
XCTAssertEqual(BigDecimal(5.5).pow(-3, .decimal128).round(.decimal32).asString(), "0.006010518")
XCTAssertEqual(BigDecimal.pow(BigDecimal("5.5"), BigDecimal("-3")).asString(), "0.006010518407212622088655146506386176")
XCTAssertEqual(BigDecimal("5.5").pow(3).asString(), "166.375")
XCTAssertEqual(BigDecimal.pow(BigDecimal("5.5"), BigDecimal("3.2")).asString(), "233.9702323679928009901371156854989")
XCTAssertEqual(BigDecimal.pow(BigDecimal("2"), BigDecimal("-4")).asString(), "0.0625")
}
let a55 = BigDecimal("5.5")

func testPow() throws {
XCTAssertEqual(a55.pow(-3, .decimal128).asString(), "0.006010518407212622088655146506386176")
XCTAssertEqual(BigDecimal(5.5).pow(-3, .decimal128).round(.decimal32).asString(), "0.006010518")
XCTAssertEqual(BigDecimal.pow(a55, BigDecimal("-3")).asString(), "0.006010518407212622088655146506386176")
XCTAssertEqual(a55.pow(3).asString(), "166.375")
XCTAssertEqual(BigDecimal.pow(a55, BigDecimal("3.2")).asString(), "233.9702323679928009901371156854989")
XCTAssertEqual(BigDecimal.pow(BigDecimal("2"), BigDecimal("-4")).asString(), "0.0625")
}

func testExp() throws {
XCTAssertEqual(BigDecimal.exp(a55).asString(), "244.6919322642203879151889495118394")
XCTAssertEqual(BigDecimal.expMinusOne(a55).asString(), "243.6919322642203879151889495118394")
XCTAssertEqual(BigDecimal.exp2(a55).asString(), "45.25483399593904156165403917471034")
}
}
39 changes: 39 additions & 0 deletions Tests/BigDecimalTests/TestTrig.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// TestTrig.swift
// BigDecimal
//
// Created by Mike Griebling on 11.10.2024.
//

import XCTest
@testable import BigDecimal

final class TestTrig: XCTestCase {

override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
}

override func tearDownWithError() throws {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}

func testTrig() throws {
let pi2 = BigDecimal.pi / 2
XCTAssertEqual(BigDecimal.sin(pi2).asString(), "0.8414709848078965066525023216302990")
XCTAssertEqual(BigDecimal.cos(pi2).asString(), "0.5403023058681397174009366074429766")
XCTAssertEqual(BigDecimal.tan(pi2).asString(), "1.557407724654902230506974807458360")
XCTAssertEqual(BigDecimal.asin(BigDecimal.sin(pi2)).asString(), "1.000000000000000000000000000000000")
XCTAssertEqual(BigDecimal.acos(BigDecimal.cos(pi2)).asString(), "1.000000000000000000000000000000000")
XCTAssertEqual(BigDecimal.atan(BigDecimal.tan(pi2)).asString(), "0.9999999999999999999999999999999999")
XCTAssertEqual(BigDecimal.atan2(y: .one, x: .ten).asString(), "0.09966865249116202737844611987802059")
}

func testPerformanceExample() throws {
// This is an example of a performance test case.
self.measure {
// Put the code you want to measure the time of here.
}
}

}

0 comments on commit 4414e0e

Please sign in to comment.