Skip to content

Commit

Permalink
Releasing Euler v0.3.3 with improved decimal description
Browse files Browse the repository at this point in the history
  • Loading branch information
arguiot committed Dec 21, 2020
1 parent 9b85c16 commit a60e402
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Euler.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "Euler"
s.version = "0.3.2"
s.version = "0.3.3"
s.summary = "The open source computational framework for the Swift language (early stage)"
s.description = <<-DESC
The open source computational framework for the Swift language (early stage)
Expand Down
4 changes: 2 additions & 2 deletions Euler.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@
HEADER_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = Euler.xcodeproj/Euler_Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) $(TOOLCHAIN_DIR)/usr/lib/swift/macosx";
MARKETING_VERSION = 0.3.2;
MARKETING_VERSION = 0.3.3;
OTHER_CFLAGS = "$(inherited)";
OTHER_LDFLAGS = "$(inherited)";
OTHER_SWIFT_FLAGS = "$(inherited)";
Expand All @@ -847,7 +847,7 @@
HEADER_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = Euler.xcodeproj/Euler_Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) $(TOOLCHAIN_DIR)/usr/lib/swift/macosx";
MARKETING_VERSION = 0.3.2;
MARKETING_VERSION = 0.3.3;
OTHER_CFLAGS = "$(inherited)";
OTHER_LDFLAGS = "$(inherited)";
OTHER_SWIFT_FLAGS = "$(inherited)";
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ I’m already working with Apple to fix this, but if you have the solution, plea
[CocoaPods](https://cocoapods.org) is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate Euler into your Xcode project using CocoaPods, specify it in your `Podfile`:

```ruby
pod 'Euler', '~> 0.3.2'
pod 'Euler', '~> 0.3.3'
```

### Carthage

[Carthage](https://github.com/Carthage/Carthage) is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks. To integrate Euler into your Xcode project using Carthage, specify it in your `Cartfile`:

```ogdl
github "arguiot/Euler" ~> 0.3.2
github "arguiot/Euler" ~> 0.3.3
```

### Swift Package Manager
Expand All @@ -34,7 +34,7 @@ Once you have your Swift package set up, adding Euler as a dependency is as easy

```swift
dependencies: [
.package(url: "https://github.com/arguiot/Euler.git", .upToNextMajor(from: "0.3.2"))
.package(url: "https://github.com/arguiot/Euler.git", .upToNextMajor(from: "0.3.3"))
]
```

Expand Down
36 changes: 16 additions & 20 deletions Sources/Euler/BigNumber/BigDouble/BigDouble.swift
Original file line number Diff line number Diff line change
Expand Up @@ -443,13 +443,22 @@ public struct BigDouble:

let separator = self.locale.decimalSeparator ?? "."

if(rounded && precision > 0) {
if(precision > 0) {
currentPrecision = currentPrecision + 1
}

let multiplier = [10].exponentiating(currentPrecision)
let limbs = self.numerator.multiplyingBy(multiplier).divMod(self.denominator).quotient
var res = BigInt(limbs: limbs).description
let bi = BigInt(limbs: limbs)
var res = bi.description

if (rounded && precision > 0) {
let lastDigit = Int(res.suffix(1))!
if (lastDigit >= 5) {
let toTen = 10 - lastDigit
res = (bi + toTen).description
}
}

if currentPrecision <= res.count {
res.insert(contentsOf: separator, at: res.index(res.startIndex, offsetBy: res.count - currentPrecision))
Expand All @@ -459,26 +468,13 @@ public struct BigDouble:
res = "0\(separator)" + String(repeating: "0", count: currentPrecision - res.count) + res
}

var retVal = self.isNegative() && !limbs.equalTo(0) ? "-" + res : res
let retVal = self.isNegative() && !limbs.equalTo(0) ? "-" + res : res

if(rounded && precision > 0) {

let lastDigit = Int(retVal.suffix(1))! // this should always be a number
let secondDigit = String(retVal.suffix(2).prefix(1)) // this could be a decimal

retVal = String(retVal.prefix(retVal.count-2))
if (secondDigit != separator) {
if lastDigit >= 5 {
retVal = retVal + String(Int(secondDigit)! + 1)
} else {
retVal = retVal + String(Int(secondDigit)!)
}
} else {
retVal = retVal + separator + String(lastDigit)
}
var correctPrec = String(retVal.dropLast()) // To counter precision + 1
if (correctPrec.suffix(1) == separator) {
correctPrec = String(correctPrec.dropLast())
}

return retVal
return correctPrec
}
/// Hashable
public func hash(into hasher: inout Hasher) {
Expand Down
20 changes: 11 additions & 9 deletions Tests/EulerTests/BigDoubleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,16 @@ class BigDoubleTests : XCTestCase {

func testDecimalExpansionWithoutRounding() {
let testValues = [
("0", "0.0", 0),
("0", "0", 0),
("0", "0.0", 1),
("0", "0.00", 2),
("0", "0.000", 3),
("12.345", "12.0", 0),
("12.345", "12", 0),
("12.345", "12.3", 1),
("12.345", "12.34", 2),
("12.345", "12.345", 3),
("12.345", "12.3450", 4),
("-0.00009", "0.0000", 4),
("-0.00009", "-0.0000", 4),
("-0.00009", "-0.00009", 5),
("-0.00009", "-0.000090", 6),
]
Expand All @@ -140,11 +140,11 @@ class BigDoubleTests : XCTestCase {

func testDecimalExpansionWithRounding() {
let testValues = [
("0", "0.0", 0),
("0", "0", 0),
("0", "0.0", 1),
("0", "0.00", 2),
("0", "0.000", 3),
("12.345", "12.0", 0),
("12.345", "12", 0),
("12.345", "12.3", 1),
("12.345", "12.35", 2),
("12.345", "12.345", 3),
Expand All @@ -162,6 +162,7 @@ class BigDoubleTests : XCTestCase {
}

func test_decimalExpansionRandom() {
BN.precision = 4
func generateDoubleString(preDecimalCount: Int, postDecimalCount: Int) -> String {
var numStr = ""

Expand Down Expand Up @@ -207,7 +208,7 @@ class BigDoubleTests : XCTestCase {

let toBigDoubleAndBack = BigDouble(doubleString)!.decimalExpansion(precisionAfterDecimalPoint: postDecimalCount)

if toBigDoubleAndBack != doubleString {
if toBigDoubleAndBack != doubleString && toBigDoubleAndBack.contains(".") {
if toBigDoubleAndBack == "0.0" && ["0", "-0"].contains(doubleString) { continue }
// For expmple, input: "13" and output "13.0" is okay

Expand Down Expand Up @@ -270,14 +271,15 @@ class BigDoubleTests : XCTestCase {
bigD?.locale = Locale(identifier: "en_US")
XCTAssertEqual(bigD?.decimalDescription, "123456789.1235")
XCTAssertEqual(bigD?.scientificDescription, "1.2346×10⁸")
XCTAssertEqual(BN("1.999999")?.decimalDescription, "2.0000")
bigD?.precision = 10
XCTAssertEqual(bigD?.decimalDescription, "123456789.1234567890")
XCTAssertEqual(bigD?.scientificDescription, "1.2345678912×10⁸")
bigD?.precision = 20
XCTAssertEqual(bigD?.decimalDescription, "123456789.12345678900000000000")
XCTAssertEqual(bigD?.scientificDescription, "1.23456789123500000000×10⁸")
bigD?.precision = 0
XCTAssertEqual(bigD?.decimalDescription, "123456789.0")
XCTAssertEqual(bigD?.decimalDescription, "123456789")
XCTAssertEqual(bigD?.scientificDescription, "1×10⁸")


Expand All @@ -295,12 +297,12 @@ class BigDoubleTests : XCTestCase {
XCTAssertEqual(bigD?.decimalDescription, "-123456789.12345678900000000000")
XCTAssertEqual(bigD?.scientificDescription, "-1.23456789123500000000×10⁸")
bigD?.precision = 0
XCTAssertEqual(bigD?.decimalDescription, "-123456789.0")
XCTAssertEqual(bigD?.decimalDescription, "-123456789")
XCTAssertEqual(bigD?.scientificDescription, "-1×10⁸")

bigD = BigDouble("0.0000000003") // nine zeroes
bigD?.precision = 0
XCTAssertEqual(bigD?.decimalDescription, "0.0")
XCTAssertEqual(bigD?.decimalDescription, "0")
XCTAssertEqual(bigD?.scientificDescription, "3×10⁻¹⁰")
bigD?.precision = 10
XCTAssertEqual(bigD?.decimalDescription, "0.0000000003")
Expand Down

0 comments on commit a60e402

Please sign in to comment.