Skip to content

Commit 1aa1cb5

Browse files
committed
Fixed: limited integer bounds & precision to the largest valid sequence of 9s.
1 parent 86ed227 commit 1aa1cb5

File tree

6 files changed

+46
-20
lines changed

6 files changed

+46
-20
lines changed

Sources/DiffableTextKitXNumber/Graphs/Graph+Floats.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ where Value: _Input & BinaryFloatingPoint & SignedNumeric {
3939
/// - Limited by `Double.max` due to `FloatingPointFormatStyle`.
4040
fileprivate init() where Value: LosslessStringConvertible {
4141
let size = Swift.min(Value.significandBitCount, Double.significandBitCount)
42-
self.precision = Int.init(log10(pow(2.0, Double.init(size))))
43-
self.max = Value(String(repeating: "9", count: precision ))!
42+
self.precision = Int(log10(pow(2, Double.init(size))))/*-*/
43+
self.max = Value(String(repeating: "9", count: precision))!
4444
self.min = -(max)
4545
}
4646

Sources/DiffableTextKitXNumber/Graphs/Graph+Integers.swift

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,20 @@ where Value: _Input & FixedWidthInteger {
3535
// MARK: Initializers
3636
//=------------------------------------------------------------------------=
3737

38-
/// - Limited by `Int.max` due to `IntegerFormatStyle`.
39-
fileprivate init() {
40-
let large = Value.bitWidth >= Int.bitWidth
41-
self.max = large ? Value(Int.max) : Value.max
42-
self.min = large && Value.isSigned ? Value(Int.min) : Value.min
43-
self.precision = Int(ceil(log10(Double(max))))
38+
/// - Limited by Int.max due to IntegerFormatStyle.
39+
/// - Limited by longest sequence of 9s due to IntegerFormatStyle.
40+
fileprivate init() where Value: LosslessStringConvertible {
41+
let limit = Value(clamping: Int.max)
42+
//=--------------------------------------=
43+
// Precision
44+
//=--------------------------------------=
45+
self.precision = Int(floor(log10(Double(limit))))
46+
let nines = String(repeating: "9", count: precision)
47+
//=--------------------------------------=
48+
// Bounds
49+
//=--------------------------------------=
50+
self.max = Value(nines)! // max <= precision, 9s <= Int.max
51+
self.min = Value.isSigned ? Value("-" + nines)! : Value.min
4452
}
4553

4654
//=------------------------------------------------------------------------=

Tests/DiffableTextKitXNumberTests/Graphs/Graph.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ final class GraphTests: XCTestCase {
2424
//=------------------------------------------------------------------------=
2525

2626
func testBounds_Decimal_Double_Int64() {
27-
XCTAssertEqual(Decimal.max, Decimal(string: String(repeating: "9", count: 38))!)
28-
XCTAssertEqual(Double .max, Double( String(repeating: "9", count: 15))!)
29-
XCTAssertEqual(Int64 .max, Int64.max)
27+
XCTAssertEqual(Decimal._NumberTextGraph.max, Decimal(string: String(repeating: "9", count: 38))!)
28+
XCTAssertEqual(Double ._NumberTextGraph.max, Double .init(String(repeating: "9", count: 15))!)
29+
XCTAssertEqual(Int64 ._NumberTextGraph.max, Int64 .init(String(repeating: "9", count: 18))!)
3030
}
3131

3232
func testPrecision_Decimal_Double_Int64() {
33-
XCTAssertEqual(Decimal.precision, 38)
34-
XCTAssertEqual(Double .precision, 15)
35-
XCTAssertEqual(Int64 .precision, 19)
33+
XCTAssertEqual(Decimal._NumberTextGraph.precision, 38)
34+
XCTAssertEqual(Double ._NumberTextGraph.precision, 15)
35+
XCTAssertEqual(Int64 ._NumberTextGraph.precision, 18)
3636
}
3737
}
3838

Tests/DiffableTextKitXNumberTests/Models/Precision.swift

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,14 @@ final class PrecisionTests: XCTestCase {
8686
}
8787

8888
func testInt() {
89-
let digits = 1...19
90-
let integer = 1...19
89+
let digits = 1...18 // 9s
90+
let integer = 1...18 // 9s
9191
let fraction = 0...00
9292

9393
var precision = _Precision<Int>()
9494
XCTAssert(precision, digits: digits, integer: integer, fraction: fraction)
9595

96-
precision = _Precision<Int>(7...7)
96+
precision = _Precision<Int>( 7...7)
9797
XCTAssert(precision, digits: 7...7, integer: integer, fraction: fraction)
9898

9999
precision = _Precision<Int>(integer: 7...7, fraction: 7...7)
@@ -102,6 +102,24 @@ final class PrecisionTests: XCTestCase {
102102
precision = _Precision<Int>(integer: (-99)...99, fraction: (-99)...99)
103103
XCTAssert(precision, digits: digits, integer: integer, fraction: fraction)
104104
}
105+
106+
func testUInt() {
107+
let digits = 1...18 // 9s
108+
let integer = 1...18 // 9s
109+
let fraction = 0...00
110+
111+
var precision = _Precision<UInt>()
112+
XCTAssert(precision, digits: digits, integer: integer, fraction: fraction)
113+
114+
precision = _Precision<UInt>(7...7)
115+
XCTAssert(precision, digits: 7...7, integer: integer, fraction: fraction)
116+
117+
precision = _Precision<UInt>(integer: 7...7, fraction: 7...7)
118+
XCTAssert(precision, digits: digits, integer: 7...7, fraction: fraction)
119+
120+
precision = _Precision<UInt>(integer: (-99)...99, fraction: (-99)...99)
121+
XCTAssert(precision, digits: digits, integer: integer, fraction: fraction)
122+
}
105123
}
106124

107125
#endif

Tests/DiffableTextKitXNumberTests/Styles/Style+Currency.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ extension StyleTestsOnCurrency {
137137
}
138138

139139
func testUInt8() {
140-
XCTAssertCurrencies(123 as UInt8)
140+
XCTAssertCurrencies(012 as UInt8) // 9s
141141
}
142142

143143
func testUInt16() {

Tests/DiffableTextKitXNumberTests/Styles/Style+Number.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ extension StyleTestsOnNumber {
7575
}
7676

7777
func testInt8() {
78-
XCTAssertLocales(-123 as Int8)
78+
XCTAssertLocales(-012 as Int8) // 9s
7979
}
8080

8181
func testInt16() {
@@ -106,7 +106,7 @@ extension StyleTestsOnNumber {
106106
}
107107

108108
func testUInt8() {
109-
XCTAssertLocales(123 as UInt8)
109+
XCTAssertLocales(012 as UInt8) // 9s
110110
}
111111

112112
func testUInt16() {

0 commit comments

Comments
 (0)