-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace ip_round extension on Double for BinaryFloatingPoint #161
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// | ||
// BinaryFloatingPoint+Extensions.swift | ||
// SwiftWisdom | ||
// | ||
// Created by Stephen Wong on 5/1/16. | ||
// Copyright © 2016 Intrepid. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
extension BinaryFloatingPoint { | ||
/// https://codereview.stackexchange.com/questions/142748/swift-floatingpoint-rounded-to-places | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We might want to give this link the |
||
public func ip_rounded(toDecimalPlaces places: Int) -> Self { | ||
guard places >= 0 else { return self } | ||
let divisor = Self((0..<places).reduce(1.0) { (result, _) in result * 10.0 }) | ||
return (self * divisor).rounded() / divisor | ||
} | ||
|
||
/// https://codereview.stackexchange.com/questions/142748/swift-floatingpoint-rounded-to-places | ||
public mutating func ip_round(toDecimalPlaces places: Int) { | ||
guard places >= 0 else { return } | ||
let divisor = Self((0..<places).reduce(1.0) { (result, _) in result * 10.0 }) | ||
self = (self * divisor).rounded() / divisor | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,14 @@ | |
// Double+Extensions.swift | ||
// SwiftWisdom | ||
// | ||
// Created by Stephen Wong on 5/1/16. | ||
// Copyright © 2016 Intrepid. All rights reserved. | ||
// Created by Litteral, Maximilian on 2/22/19. | ||
// Copyright © 2019 Intrepid. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
extension Double { | ||
@available(*, unavailable, renamed: "ip_rounded(toDecimalPlaces:)") | ||
public func ip_round(toDecimalPlaces decimalPlaces: Int) -> Double { | ||
let decimalShiftBase10 = pow(10.0, Double(decimalPlaces)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this code now cannot be called, it should be removed. But since the method return a value, it needs to call |
||
let roundedNumber = (self * decimalShiftBase10).rounded() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// | ||
// BinaryFloatingPointTests.swift | ||
// SwiftWisdom | ||
// | ||
// Created by Stephen Wong on 5/1/16. | ||
// Copyright © 2016 Intrepid. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
import SwiftWisdom | ||
|
||
class BinaryFloatingPointTests: XCTestCase { | ||
|
||
func testRoundingDoubles() { | ||
let unroundedDoubles = [.pi, 5.0156, 10.014, 9999.99499] | ||
let roundedToOneDecimalPlaces = [3.1, 5.0, 10.0, 10000.0] | ||
let roundedToTwoDecimalPlaces = [3.14, 5.02, 10.01, 9999.99] | ||
let roundedToFiveDecimalPlaces = [3.14159, 5.0156, 10.014, 9999.99499] | ||
|
||
for (index, unroundedDouble) in unroundedDoubles.enumerated() { | ||
XCTAssertEqual(unroundedDouble.ip_rounded(toDecimalPlaces: 1), roundedToOneDecimalPlaces[index]) | ||
XCTAssertEqual(unroundedDouble.ip_rounded(toDecimalPlaces: 2), roundedToTwoDecimalPlaces[index]) | ||
XCTAssertEqual(unroundedDouble.ip_rounded(toDecimalPlaces: 5), roundedToFiveDecimalPlaces[index]) | ||
} | ||
} | ||
|
||
func testRoundingFloats() { | ||
let unroundedFloats: [Float] = [.pi, 5.0156, 10.014, 9999.99499] | ||
let roundedToOneDecimalPlaces: [Float] = [3.1, 5.0, 10.0, 10000.0] | ||
let roundedToTwoDecimalPlaces: [Float] = [3.14, 5.02, 10.01, 9999.99] | ||
let roundedToFiveDecimalPlaces: [Float] = [3.14159, 5.0156, 10.014, 9999.99499] | ||
|
||
for (index, unroundedDouble) in unroundedFloats.enumerated() { | ||
XCTAssertEqual(unroundedDouble.ip_rounded(toDecimalPlaces: 1), roundedToOneDecimalPlaces[index]) | ||
XCTAssertEqual(unroundedDouble.ip_rounded(toDecimalPlaces: 2), roundedToTwoDecimalPlaces[index], accuracy: 0.01) | ||
XCTAssertEqual(unroundedDouble.ip_rounded(toDecimalPlaces: 5), roundedToFiveDecimalPlaces[index]) | ||
} | ||
} | ||
|
||
func testRoundingCGFloats() { | ||
let unroundedFloats: [CGFloat] = [.pi, 5.0156, 10.014, 9999.99499] | ||
let roundedToOneDecimalPlaces: [CGFloat] = [3.1, 5.0, 10.0, 10000.0] | ||
let roundedToTwoDecimalPlaces: [CGFloat] = [3.14, 5.02, 10.01, 9999.99] | ||
let roundedToFiveDecimalPlaces: [CGFloat] = [3.14159, 5.0156, 10.014, 9999.99499] | ||
|
||
for (index, unroundedDouble) in unroundedFloats.enumerated() { | ||
XCTAssertEqual(unroundedDouble.ip_rounded(toDecimalPlaces: 1), roundedToOneDecimalPlaces[index]) | ||
XCTAssertEqual(unroundedDouble.ip_rounded(toDecimalPlaces: 2), roundedToTwoDecimalPlaces[index]) | ||
XCTAssertEqual(unroundedDouble.ip_rounded(toDecimalPlaces: 5), roundedToFiveDecimalPlaces[index]) | ||
} | ||
} | ||
|
||
func testRoundInPlace() { | ||
// Double | ||
var pi_double: Double = .pi | ||
pi_double.ip_round(toDecimalPlaces: 1) | ||
XCTAssertEqual(pi_double, 3.1) | ||
pi_double = .pi // Reset | ||
pi_double.ip_round(toDecimalPlaces: 2) | ||
XCTAssertEqual(pi_double, 3.14) | ||
pi_double = .pi // Reset | ||
pi_double.ip_round(toDecimalPlaces: 5) | ||
XCTAssertEqual(pi_double, 3.14159) | ||
|
||
// Float | ||
var pi_float: Float = .pi | ||
pi_float.ip_round(toDecimalPlaces: 1) | ||
XCTAssertEqual(pi_float, 3.1) | ||
pi_float = .pi // Reset | ||
pi_float.ip_round(toDecimalPlaces: 2) | ||
XCTAssertEqual(pi_float, 3.14) | ||
pi_float = .pi // Reset | ||
pi_float.ip_round(toDecimalPlaces: 5) | ||
XCTAssertEqual(pi_float, 3.14159) | ||
|
||
// CGFloat | ||
var pi_cgfloat: CGFloat = .pi | ||
pi_cgfloat.ip_round(toDecimalPlaces: 1) | ||
XCTAssertEqual(pi_cgfloat, 3.1) | ||
pi_cgfloat = .pi // Reset | ||
pi_cgfloat.ip_round(toDecimalPlaces: 2) | ||
XCTAssertEqual(pi_cgfloat, 3.14) | ||
pi_cgfloat = .pi // Reset | ||
pi_cgfloat.ip_round(toDecimalPlaces: 5) | ||
XCTAssertEqual(pi_cgfloat, 3.14159) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did this change? Did you re-run
pod install
If so, you could probably check in thePodfile.lock
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did run
pod install
to generate the workspaceThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm, since that made THIS change, didn't it also update
Podfile.lock
? You should rerun it and check in the lock file if it changed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like the lock file did not change.