27
27
28
28
import Foundation
29
29
30
+ /**
31
+
32
+ # DecimalNumberBehaviorType
33
+
34
+ Defines the decimal number behavior, i.e. `NSDecimalNumberBehaviors`
35
+ of the type.
36
+
37
+ */
30
38
public protocol DecimalNumberBehaviorType {
31
39
32
40
/// Specify the decimal number (i.e. rounding, scale etc) for base 10 calculations
33
41
static var decimalNumberBehaviors : NSDecimalNumberBehaviors { get }
34
42
}
35
43
36
- public struct DecimalNumberBehavior {
44
+ /**
37
45
38
- private static func behaviorWithRoundingMode( mode: NSRoundingMode ) -> NSDecimalNumberBehaviors {
39
- return NSDecimalNumberHandler ( roundingMode: mode, scale: 38 , raiseOnExactness: false , raiseOnOverflow: true , raiseOnUnderflow: true , raiseOnDivideByZero: true )
40
- }
46
+ # DecimalNumberBehavior
47
+
48
+ This is a name space of types which conform to `DecimalNumberBehaviorType`
49
+ with common rounding modes. All have maximum precision, of 38 significant
50
+ digits.
51
+ */
52
+ public struct DecimalNumberBehavior {
41
53
54
+ /// Plain rounding mode
42
55
public struct Plain : DecimalNumberBehaviorType {
43
56
public static let decimalNumberBehaviors = DecimalNumberBehavior . behaviorWithRoundingMode ( . RoundPlain)
44
57
}
45
58
59
+ /// Round down mode
46
60
public struct RoundDown : DecimalNumberBehaviorType {
47
61
public static let decimalNumberBehaviors = DecimalNumberBehavior . behaviorWithRoundingMode ( . RoundDown)
48
62
}
49
63
64
+ /// Round up mode
50
65
public struct RoundUp : DecimalNumberBehaviorType {
51
66
public static let decimalNumberBehaviors = DecimalNumberBehavior . behaviorWithRoundingMode ( . RoundUp)
52
67
}
53
68
69
+ /// Bankers rounding mode, see `NSRoundingMode.RoundBankers` for info.
54
70
public struct Bankers : DecimalNumberBehaviorType {
55
71
public static let decimalNumberBehaviors = DecimalNumberBehavior . behaviorWithRoundingMode ( . RoundBankers)
56
72
}
73
+
74
+ private static func behaviorWithRoundingMode( mode: NSRoundingMode , scale: Int16 = 38 ) -> NSDecimalNumberBehaviors {
75
+ return NSDecimalNumberHandler ( roundingMode: mode, scale: 38 , raiseOnExactness: false , raiseOnOverflow: true , raiseOnUnderflow: true , raiseOnDivideByZero: true )
76
+ }
57
77
}
58
78
59
79
@@ -93,82 +113,98 @@ public protocol DecimalNumberType: SignedNumberType, IntegerLiteralConvertible,
93
113
Subtract a matching `DecimalNumberType` from the receiver.
94
114
95
115
- parameter other: another instance of this type.
96
- - parameter behaviors: an optional NSDecimalNumberBehaviors?
97
116
- returns: another instance of this type.
98
117
*/
99
118
@warn_unused_result
100
- func subtract( _: Self , withBehaviors : NSDecimalNumberBehaviors ) -> Self
119
+ func subtract( _: Self ) -> Self
101
120
102
121
/**
103
122
Add a matching `DecimalNumberType` to the receiver.
104
123
105
124
- parameter other: another instance of this type.
106
- - parameter behaviors: an optional NSDecimalNumberBehaviors?
107
125
- returns: another instance of this type.
108
126
*/
109
127
@warn_unused_result
110
- func add( _: Self , withBehaviors : NSDecimalNumberBehaviors ) -> Self
128
+ func add( _: Self ) -> Self
111
129
112
130
/**
113
131
Multiply a matching `DecimalNumberType` with the receiver.
114
132
115
133
- parameter other: another instance of this type.
116
- - parameter behaviors: an optional NSDecimalNumberBehaviors?
117
134
- returns: another instance of this type.
118
135
*/
119
136
@warn_unused_result
120
- func multiplyBy( _: Self , withBehaviors : NSDecimalNumberBehaviors ) -> Self
137
+ func multiplyBy( _: Self ) -> Self
121
138
122
139
/**
123
140
Multiply another `DecimalNumberType` with the receiver. The other
124
141
`DecimalNumberType` must have the same underlying `DecimalStorageType` as
125
142
this `DecimalNumberType`.
126
143
127
144
- parameter other: another `DecimalNumberType` value of different type.
128
- - parameter behaviors: an optional NSDecimalNumberBehaviors?
129
145
- returns: a different `DecimalNumberType` value.
130
146
*/
131
147
@warn_unused_result
132
- func multiplyBy< Other: DecimalNumberType where Other. DecimalStorageType == DecimalStorageType > ( _: Other , withBehaviors : NSDecimalNumberBehaviors ) -> Other
148
+ func multiplyBy< Other: DecimalNumberType where Other. DecimalStorageType == DecimalStorageType > ( _: Other ) -> Other
133
149
134
150
/**
135
151
Divide the receiver by a matching `DecimalNumberType`.
136
152
137
153
- parameter other: another instance of this type.
138
- - parameter behaviors: an optional NSDecimalNumberBehaviors?
139
154
- returns: another instance of this type.
140
155
*/
141
156
@warn_unused_result
142
- func divideBy( _: Self , withBehaviors : NSDecimalNumberBehaviors ) -> Self
157
+ func divideBy( _: Self ) -> Self
143
158
144
159
/**
145
160
Divide the receiver by another `DecimalNumberType`. The other
146
161
`DecimalNumberType` must have the same underlying `DecimalStorageType` as
147
162
this `DecimalNumberType`.
148
163
149
164
- parameter other: another `DecimalNumberType` value of different type.
150
- - parameter behaviors: an optional NSDecimalNumberBehaviors?
151
165
- returns: another instance of this type.
152
166
*/
153
167
@warn_unused_result
154
- func divideBy< Other: DecimalNumberType where Other. DecimalStorageType == DecimalStorageType > ( _: Other , withBehaviors : NSDecimalNumberBehaviors ) -> Other
168
+ func divideBy< Other: DecimalNumberType where Other. DecimalStorageType == DecimalStorageType > ( _: Other ) -> Other
155
169
156
170
/**
157
171
The remainder of dividing another `DecimalNumberType` into the receiver.
158
172
159
173
- parameter other: another instance of this type.
160
- - parameter behaviors: an optional NSDecimalNumberBehaviors?
161
174
- returns: another instance of this type.
162
175
*/
163
176
@warn_unused_result
164
- func remainder( _: Self , withBehaviors : NSDecimalNumberBehaviors ) -> Self
177
+ func remainder( _: Self ) -> Self
165
178
}
166
179
180
+ public extension DecimalNumberType where DecimalStorageType == NSDecimalNumber {
181
+
182
+ @warn_unused_result
183
+ func multiplyBy< Other: DecimalNumberType where Other. DecimalStorageType == NSDecimalNumber > ( other: Other ) -> Other {
184
+ return Other ( storage: storage. multiplyBy ( other. storage, withBehaviors: Other . DecimalNumberBehavior. decimalNumberBehaviors) )
185
+ }
186
+
187
+ @warn_unused_result
188
+ func divideBy< Other: DecimalNumberType where Other. DecimalStorageType == NSDecimalNumber > ( other: Other ) -> Other {
189
+ return Other ( storage: storage. divideBy ( other. storage, withBehaviors: Other . DecimalNumberBehavior. decimalNumberBehaviors) )
190
+ }
191
+ }
192
+
193
+ extension DecimalNumberType where Self. IntegerLiteralType == Int {
194
+
195
+ /// Get the reciprocal of the receiver.
196
+ public var reciprocal : Self {
197
+ return Self ( integerLiteral: 1 ) . divideBy ( self )
198
+ }
199
+ }
200
+
201
+ // MARK: - Operators
202
+
167
203
// MARK: - Subtraction
168
204
169
205
@warn_unused_result
170
206
public func - < T: DecimalNumberType > ( lhs: T , rhs: T ) -> T {
171
- return lhs. subtract ( rhs, withBehaviors : T . DecimalNumberBehavior . decimalNumberBehaviors )
207
+ return lhs. subtract ( rhs)
172
208
}
173
209
174
210
@warn_unused_result
@@ -191,18 +227,11 @@ public func -<T: DecimalNumberType>(lhs: T.FloatLiteralType, rhs: T) -> T {
191
227
return T ( floatLiteral: lhs) - rhs
192
228
}
193
229
194
- // MARK: - Remainder
195
-
196
- @warn_unused_result
197
- public func % < T: DecimalNumberType > ( lhs: T , rhs: T ) -> T {
198
- return lhs. remainder ( rhs, withBehaviors: T . DecimalNumberBehavior. decimalNumberBehaviors)
199
- }
200
-
201
230
// MARK: - Addition
202
231
203
232
@warn_unused_result
204
233
public func + < T: DecimalNumberType > ( lhs: T , rhs: T ) -> T {
205
- return lhs. add ( rhs, withBehaviors : T . DecimalNumberBehavior . decimalNumberBehaviors )
234
+ return lhs. add ( rhs)
206
235
}
207
236
208
237
@warn_unused_result
@@ -229,7 +258,7 @@ public func +<T: DecimalNumberType>(lhs: T.FloatLiteralType, rhs: T) -> T {
229
258
230
259
@warn_unused_result
231
260
public func * < T: DecimalNumberType > ( lhs: T , rhs: T ) -> T {
232
- return lhs. multiplyBy ( rhs, withBehaviors : T . DecimalNumberBehavior . decimalNumberBehaviors )
261
+ return lhs. multiplyBy ( rhs)
233
262
}
234
263
235
264
@warn_unused_result
@@ -257,14 +286,14 @@ public func *<T, V where
257
286
T: DecimalNumberType ,
258
287
V: DecimalNumberType ,
259
288
T. DecimalStorageType == V . DecimalStorageType > ( lhs: T , rhs: V ) -> V {
260
- return lhs. multiplyBy ( rhs, withBehaviors : V . DecimalNumberBehavior . decimalNumberBehaviors )
289
+ return lhs. multiplyBy ( rhs)
261
290
}
262
291
263
292
// MARK: - Division
264
293
265
294
@warn_unused_result
266
295
public func / < T: DecimalNumberType > ( lhs: T , rhs: T ) -> T {
267
- return lhs. divideBy ( rhs, withBehaviors : T . DecimalNumberBehavior . decimalNumberBehaviors )
296
+ return lhs. divideBy ( rhs)
268
297
}
269
298
270
299
@warn_unused_result
@@ -282,28 +311,14 @@ public func /<T, V where
282
311
T: DecimalNumberType ,
283
312
V: DecimalNumberType ,
284
313
T. DecimalStorageType == V . DecimalStorageType > ( lhs: T , rhs: V ) -> V {
285
- return lhs. divideBy ( rhs, withBehaviors: V . DecimalNumberBehavior. decimalNumberBehaviors)
286
- }
287
-
288
- extension DecimalNumberType where DecimalStorageType == NSDecimalNumber {
289
-
290
- @warn_unused_result
291
- public func multiplyBy< Other: DecimalNumberType where Other. DecimalStorageType == NSDecimalNumber > ( other: Other , withBehaviors behaviors: NSDecimalNumberBehaviors ) -> Other {
292
- return Other ( storage: storage. multiplyBy ( other. storage, withBehaviors: behaviors) )
293
- }
294
-
295
- @warn_unused_result
296
- public func divideBy< Other: DecimalNumberType where Other. DecimalStorageType == NSDecimalNumber > ( other: Other , withBehaviors behaviors: NSDecimalNumberBehaviors ) -> Other {
297
- return Other ( storage: storage. divideBy ( other. storage, withBehaviors: behaviors) )
298
- }
314
+ return lhs. divideBy ( rhs)
299
315
}
300
316
301
- extension DecimalNumberType where Self . IntegerLiteralType == Int {
317
+ // MARK: - Remainder
302
318
303
- /// Get the reciprocal of the receiver.
304
- public var reciprocal : Self {
305
- return Self ( integerLiteral: 1 ) . divideBy ( self , withBehaviors: DecimalNumberBehavior . decimalNumberBehaviors)
306
- }
319
+ @warn_unused_result
320
+ public func % < T: DecimalNumberType > ( lhs: T , rhs: T ) -> T {
321
+ return lhs. remainder ( rhs)
307
322
}
308
323
309
324
0 commit comments