@@ -95,6 +95,11 @@ public struct Decimal {
95
95
}
96
96
}
97
97
98
+ extension Decimal {
99
+ public typealias RoundingMode = NSDecimalNumber . RoundingMode
100
+ public typealias CalculationError = NSDecimalNumber . CalculationError
101
+ }
102
+
98
103
extension Decimal {
99
104
public static let leastFiniteMagnitude = Decimal (
100
105
_exponent: 127 ,
@@ -104,6 +109,7 @@ extension Decimal {
104
109
_reserved: 0 ,
105
110
_mantissa: ( 0xffff , 0xffff , 0xffff , 0xffff , 0xffff , 0xffff , 0xffff , 0xffff )
106
111
)
112
+
107
113
public static let greatestFiniteMagnitude = Decimal (
108
114
_exponent: 127 ,
109
115
_length: 8 ,
@@ -112,6 +118,7 @@ extension Decimal {
112
118
_reserved: 0 ,
113
119
_mantissa: ( 0xffff , 0xffff , 0xffff , 0xffff , 0xffff , 0xffff , 0xffff , 0xffff )
114
120
)
121
+
115
122
public static let leastNormalMagnitude = Decimal (
116
123
_exponent: - 127 ,
117
124
_length: 1 ,
@@ -120,6 +127,7 @@ extension Decimal {
120
127
_reserved: 0 ,
121
128
_mantissa: ( 0x0001 , 0x0000 , 0x0000 , 0x0000 , 0x0000 , 0x0000 , 0x0000 , 0x0000 )
122
129
)
130
+
123
131
public static let leastNonzeroMagnitude = Decimal (
124
132
_exponent: - 127 ,
125
133
_length: 1 ,
@@ -128,6 +136,7 @@ extension Decimal {
128
136
_reserved: 0 ,
129
137
_mantissa: ( 0x0001 , 0x0000 , 0x0000 , 0x0000 , 0x0000 , 0x0000 , 0x0000 , 0x0000 )
130
138
)
139
+
131
140
public static let pi = Decimal (
132
141
_exponent: - 38 ,
133
142
_length: 8 ,
@@ -136,62 +145,94 @@ extension Decimal {
136
145
_reserved: 0 ,
137
146
_mantissa: ( 0x6623 , 0x7d57 , 0x16e7 , 0xad0d , 0xaf52 , 0x4641 , 0xdfa7 , 0xec58 )
138
147
)
148
+
139
149
public var exponent : Int {
140
- get {
141
- return Int ( self . __exponent)
142
- }
150
+ return Int ( _exponent)
143
151
}
152
+
144
153
public var significand : Decimal {
145
- get {
146
- return Decimal ( _exponent: 0 , _length: _length, _isNegative: _isNegative, _isCompact: _isCompact, _reserved : 0 , _mantissa : _mantissa )
147
- }
154
+ return Decimal (
155
+ _exponent: 0 , _length: _length, _isNegative: _isNegative, _isCompact: _isCompact,
156
+ _reserved : 0 , _mantissa : _mantissa )
148
157
}
158
+
149
159
public init ( sign: FloatingPointSign , exponent: Int , significand: Decimal ) {
150
- self . init ( _exponent: Int32 ( exponent) + significand. _exponent, _length: significand. _length, _isNegative: sign == . plus ? 0 : 1 , _isCompact: significand. _isCompact, _reserved: 0 , _mantissa: significand. _mantissa)
160
+ self . init (
161
+ _exponent: Int32 ( exponent) + significand. _exponent,
162
+ _length: significand. _length,
163
+ _isNegative: sign == . plus ? 0 : 1 ,
164
+ _isCompact: significand. _isCompact,
165
+ _reserved: 0 ,
166
+ _mantissa: significand. _mantissa)
151
167
}
168
+
152
169
public init ( signOf: Decimal , magnitudeOf magnitude: Decimal ) {
153
- self . init ( _exponent: magnitude. _exponent, _length: magnitude. _length, _isNegative: signOf. _isNegative, _isCompact: magnitude. _isCompact, _reserved: 0 , _mantissa: magnitude. _mantissa)
170
+ self . init (
171
+ _exponent: magnitude. _exponent,
172
+ _length: magnitude. _length,
173
+ _isNegative: signOf. _isNegative,
174
+ _isCompact: magnitude. _isCompact,
175
+ _reserved: 0 ,
176
+ _mantissa: magnitude. _mantissa)
154
177
}
178
+
155
179
public var sign : FloatingPointSign {
156
180
return _isNegative == 0 ? FloatingPointSign . plus : FloatingPointSign . minus
157
181
}
182
+
158
183
public static var radix : Int {
159
184
return 10
160
185
}
186
+
161
187
public var ulp : Decimal {
162
188
if !self . isFinite { return Decimal . nan }
163
- return Decimal ( _exponent: _exponent, _length: 8 , _isNegative: 0 , _isCompact: 1 , _reserved: 0 , _mantissa: ( 0x0001 , 0x0000 , 0x0000 , 0x0000 , 0x0000 , 0x0000 , 0x0000 , 0x0000 ) )
189
+ return Decimal (
190
+ _exponent: _exponent, _length: 8 , _isNegative: 0 , _isCompact: 1 ,
191
+ _reserved: 0 , _mantissa: ( 0x0001 , 0x0000 , 0x0000 , 0x0000 , 0x0000 , 0x0000 , 0x0000 , 0x0000 ) )
164
192
}
193
+
165
194
public func isEqual( to other: Decimal ) -> Bool {
166
195
return self . compare ( to: other) == . orderedSame
167
196
}
197
+
168
198
public func isLess( than other: Decimal ) -> Bool {
169
199
return self . compare ( to: other) == . orderedAscending
170
200
}
201
+
171
202
public func isLessThanOrEqualTo( _ other: Decimal ) -> Bool {
172
203
let comparison = self . compare ( to: other)
173
204
return comparison == . orderedAscending || comparison == . orderedSame
174
205
}
206
+
175
207
public func isTotallyOrdered( belowOrEqualTo other: Decimal ) -> Bool {
176
- // Notes : Decimal does not have -0 or infinities to worry about
208
+ // Note : Decimal does not have -0 or infinities to worry about
177
209
if self . isNaN {
178
210
return false
179
- } else if self < other {
211
+ }
212
+ if self < other {
180
213
return true
181
- } else if other < self {
214
+ }
215
+ if other < self {
182
216
return false
183
217
}
184
- // fall through to == behavior
218
+ // Fall through to == behavior
185
219
return true
186
220
}
221
+
187
222
public var isCanonical : Bool {
188
223
return true
189
224
}
225
+
190
226
public var nextUp : Decimal {
191
- return self + Decimal( _exponent: _exponent, _length: 1 , _isNegative: 0 , _isCompact: 1 , _reserved: 0 , _mantissa: ( 0x0001 , 0x0000 , 0x0000 , 0x0000 , 0x0000 , 0x0000 , 0x0000 , 0x0000 ) )
227
+ return self + Decimal(
228
+ _exponent: _exponent, _length: 1 , _isNegative: 0 , _isCompact: 1 ,
229
+ _reserved: 0 , _mantissa: ( 0x0001 , 0x0000 , 0x0000 , 0x0000 , 0x0000 , 0x0000 , 0x0000 , 0x0000 ) )
192
230
}
231
+
193
232
public var nextDown : Decimal {
194
- return self - Decimal( _exponent: _exponent, _length: 1 , _isNegative: 0 , _isCompact: 1 , _reserved: 0 , _mantissa: ( 0x0001 , 0x0000 , 0x0000 , 0x0000 , 0x0000 , 0x0000 , 0x0000 , 0x0000 ) )
233
+ return self - Decimal(
234
+ _exponent: _exponent, _length: 1 , _isNegative: 0 , _isCompact: 1 ,
235
+ _reserved: 0 , _mantissa: ( 0x0001 , 0x0000 , 0x0000 , 0x0000 , 0x0000 , 0x0000 , 0x0000 , 0x0000 ) )
195
236
}
196
237
}
197
238
@@ -587,27 +628,36 @@ extension Decimal : Strideable {
587
628
}
588
629
}
589
630
631
+ // The methods in this extension exist to match the protocol requirements of
632
+ // FloatingPoint, even if we can't conform directly.
633
+ //
634
+ // If it becomes clear that conformance is truly impossible, we can deprecate
635
+ // some of the methods (e.g. `isEqual(to:)` in favor of operators).
590
636
extension Decimal {
591
- public typealias RoundingMode = NSDecimalNumber . RoundingMode
592
- public typealias CalculationError = NSDecimalNumber . CalculationError
593
637
public init ( _ value: UInt8 ) {
594
638
self . init ( UInt64 ( value) )
595
639
}
640
+
596
641
public init ( _ value: Int8 ) {
597
642
self . init ( Int64 ( value) )
598
643
}
644
+
599
645
public init ( _ value: UInt16 ) {
600
646
self . init ( UInt64 ( value) )
601
647
}
648
+
602
649
public init ( _ value: Int16 ) {
603
650
self . init ( Int64 ( value) )
604
651
}
652
+
605
653
public init ( _ value: UInt32 ) {
606
654
self . init ( UInt64 ( value) )
607
655
}
656
+
608
657
public init ( _ value: Int32 ) {
609
658
self . init ( Int64 ( value) )
610
659
}
660
+
611
661
public init ( _ value: Double ) {
612
662
precondition ( !value. isInfinite, " Decimal does not yet fully adopt FloatingPoint " )
613
663
if value. isNaN {
@@ -629,26 +679,27 @@ extension Decimal {
629
679
}
630
680
var mantissa = UInt64 ( val)
631
681
632
- var i = Int32 ( 0 )
633
- // this is a bit ugly but it is the closest approximation of the C initializer that can be expressed here.
682
+ var i : Int32 = 0
683
+ // This is a bit ugly but it is the closest approximation of the C
684
+ // initializer that can be expressed here.
634
685
while mantissa != 0 && i < NSDecimalMaxSize {
635
686
switch i {
636
687
case 0 :
637
- _mantissa. 0 = UInt16 ( truncatingIfNeeded: mantissa)
688
+ _mantissa. 0 = UInt16 ( truncatingIfNeeded: mantissa)
638
689
case 1 :
639
- _mantissa. 1 = UInt16 ( truncatingIfNeeded: mantissa)
690
+ _mantissa. 1 = UInt16 ( truncatingIfNeeded: mantissa)
640
691
case 2 :
641
- _mantissa. 2 = UInt16 ( truncatingIfNeeded: mantissa)
692
+ _mantissa. 2 = UInt16 ( truncatingIfNeeded: mantissa)
642
693
case 3 :
643
- _mantissa. 3 = UInt16 ( truncatingIfNeeded: mantissa)
694
+ _mantissa. 3 = UInt16 ( truncatingIfNeeded: mantissa)
644
695
case 4 :
645
- _mantissa. 4 = UInt16 ( truncatingIfNeeded: mantissa)
696
+ _mantissa. 4 = UInt16 ( truncatingIfNeeded: mantissa)
646
697
case 5 :
647
- _mantissa. 5 = UInt16 ( truncatingIfNeeded: mantissa)
698
+ _mantissa. 5 = UInt16 ( truncatingIfNeeded: mantissa)
648
699
case 6 :
649
- _mantissa. 6 = UInt16 ( truncatingIfNeeded: mantissa)
700
+ _mantissa. 6 = UInt16 ( truncatingIfNeeded: mantissa)
650
701
case 7 :
651
- _mantissa. 7 = UInt16 ( truncatingIfNeeded: mantissa)
702
+ _mantissa. 7 = UInt16 ( truncatingIfNeeded: mantissa)
652
703
default :
653
704
fatalError ( " initialization overflow " )
654
705
}
@@ -716,10 +767,11 @@ extension Decimal {
716
767
}
717
768
718
769
public static var quietNaN : Decimal {
719
- var quiet = Decimal ( )
720
- quiet . _isNegative = 1
721
- return quiet
770
+ return Decimal (
771
+ _exponent : 0 , _length : 0 , _isNegative : 1 , _isCompact : 0 ,
772
+ _reserved : 0 , _mantissa : ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ) )
722
773
}
774
+
723
775
public var floatingPointClass : FloatingPointClassification {
724
776
if _length == 0 && _isNegative == 1 {
725
777
return . quietNaN
@@ -732,27 +784,35 @@ extension Decimal {
732
784
return . positiveNormal
733
785
}
734
786
}
787
+
735
788
public var isSignMinus : Bool {
736
789
return _isNegative != 0
737
790
}
791
+
738
792
public var isNormal : Bool {
739
793
return !isZero && !isInfinite && !isNaN
740
794
}
795
+
741
796
public var isFinite : Bool {
742
797
return !isNaN
743
798
}
799
+
744
800
public var isZero : Bool {
745
801
return _length == 0 && _isNegative == 0
746
802
}
803
+
747
804
public var isSubnormal : Bool {
748
805
return false
749
806
}
807
+
750
808
public var isInfinite : Bool {
751
809
return false
752
810
}
811
+
753
812
public var isNaN : Bool {
754
813
return _length == 0 && _isNegative == 1
755
814
}
815
+
756
816
public var isSignaling : Bool {
757
817
return false
758
818
}
0 commit comments