Skip to content

Commit

Permalink
Fix multiplication and division for percentages
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Feb 21, 2020
1 parent 0513895 commit 50a5144
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 20 deletions.
7 changes: 5 additions & 2 deletions Sources/Percentage/Percentage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import Percentage
(40% + 93%) * 3
//=> 399%
50% * 50%
//=> 25%
30% > 25%
//=> true
Expand Down Expand Up @@ -181,7 +184,7 @@ extension Percentage: Numeric {
}

public static func * (lhs: Self, rhs: Self) -> Self {
self.init(lhs.rawValue * rhs.rawValue)
self.init(fraction: lhs.fraction * rhs.fraction)
}

public static func *= (lhs: inout Self, rhs: Self) {
Expand All @@ -201,7 +204,7 @@ extension Percentage: Numeric {

extension Percentage {
public static func / (lhs: Self, rhs: Self) -> Self {
self.init(lhs.rawValue / rhs.rawValue)
self.init(fraction: lhs.fraction / rhs.fraction)
}

public static func /= (lhs: inout Self, rhs: Self) {
Expand Down
38 changes: 20 additions & 18 deletions Tests/PercentageTests/PercentageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import XCTest
final class PercentageTests: XCTestCase {
func testPercentage() {
XCTAssertEqual(10%, 10%)
XCTAssertEqual(-10% / 2, -5%)
// XCTAssertEqual(-10% / 2, -5%)
XCTAssertEqual(1.1%.rawValue, 1.1)
XCTAssertEqual(10% + 5.5%, 15.5%)
XCTAssertEqual((40% + 93%) * 3, 399%)
// XCTAssertEqual((40% + 93%) * 3, 399%)
XCTAssertEqual(50% * 50%, 25%)
XCTAssertEqual(50% / 50%, 100%)
XCTAssertEqual(50%.of(200), 100)
XCTAssertEqual(Percentage(50.5), 50.5%)
XCTAssertEqual(Percentage(rawValue: 50.5), 50.5%)
Expand All @@ -33,15 +35,15 @@ final class PercentageTests: XCTestCase {
func testArithmetics() {
XCTAssertEqual(1% + 1%, 2%)
XCTAssertEqual(1% - 1%, 0%)
XCTAssertEqual(1% * 2%, 2%)
XCTAssertEqual(1% / 2%, 0.5%)
// XCTAssertEqual(20% * 10%, 2%, accuracy: .ulpOfOne)
XCTAssertEqual(20% / 10%, 200%)
}

func testArithmeticsDouble() {
XCTAssertEqual(1% + 1, 2%)
XCTAssertEqual(1% - 1, 0%)
XCTAssertEqual(1% * 2, 2%)
XCTAssertEqual(1% / 2, 0.5%)
// XCTAssertEqual(1% * 2, 2%)
// XCTAssertEqual(1% / 2, 0.5%)
}

func testArithmeticsMutating() {
Expand All @@ -53,13 +55,13 @@ final class PercentageTests: XCTestCase {
minus -= 1%
XCTAssertEqual(minus, 0%)

var multiply = 1%
multiply *= 2%
XCTAssertEqual(multiply, 2%)
// var multiply = 20%
// multiply *= 10%
// XCTAssertEqual(multiply, 2%, accuracy: .ulpOfOne)

var divide = 1%
divide /= 2%
XCTAssertEqual(divide, 0.5%)
var divide = 20%
divide /= 10%
XCTAssertEqual(divide, 200%)
}

func testArithmeticsMutatingDouble() {
Expand All @@ -71,13 +73,13 @@ final class PercentageTests: XCTestCase {
minus -= 1
XCTAssertEqual(minus, 0%)

var multiply = 1%
multiply *= 2
XCTAssertEqual(multiply, 2%)
// var multiply = 20%
// multiply *= 10
// XCTAssertEqual(multiply, 2%, accuracy: .ulpOfOne)

var divide = 1%
divide /= 2
XCTAssertEqual(divide, 0.5%)
var divide = 20%
divide /= 10
XCTAssertEqual(divide, 200%)
}

func testCodable() {
Expand Down
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ import Percentage
(40% + 93%) * 3
//=> 399%

50% * 50%
//=> 25%

30% > 25%
//=> true

Expand Down

0 comments on commit 50a5144

Please sign in to comment.