File tree 2 files changed +30
-4
lines changed
stdlib/public/SDK/Foundation
2 files changed +30
-4
lines changed Original file line number Diff line number Diff line change 2
2
//
3
3
// This source file is part of the Swift.org open source project
4
4
//
5
- // Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5
+ // Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
6
6
// Licensed under Apache License v2.0 with Runtime Library Exception
7
7
//
8
8
// See https://swift.org/LICENSE.txt for license information
@@ -362,11 +362,33 @@ extension Decimal {
362
362
}
363
363
364
364
public init ( _ value: UInt64 ) {
365
- self . init ( Double ( value) )
365
+ self = Decimal ( )
366
+ if value == 0 {
367
+ return
368
+ }
369
+
370
+ var compactValue = value
371
+ var exponent : Int32 = 0
372
+ while compactValue % 10 == 0 {
373
+ compactValue /= 10
374
+ exponent += 1
375
+ }
376
+ _isCompact = 1
377
+ _exponent = exponent
378
+
379
+ let wordCount = ( ( UInt64 . bitWidth - compactValue. leadingZeroBitCount) + ( UInt16 . bitWidth - 1 ) ) / UInt16. bitWidth
380
+ _length = UInt32 ( wordCount)
381
+ _mantissa. 0 = UInt16 ( truncatingIfNeeded: compactValue >> 0 )
382
+ _mantissa. 1 = UInt16 ( truncatingIfNeeded: compactValue >> 16 )
383
+ _mantissa. 2 = UInt16 ( truncatingIfNeeded: compactValue >> 32 )
384
+ _mantissa. 3 = UInt16 ( truncatingIfNeeded: compactValue >> 48 )
366
385
}
367
386
368
387
public init ( _ value: Int64 ) {
369
- self . init ( Double ( value) )
388
+ self . init ( value. magnitude)
389
+ if value < 0 {
390
+ _isNegative = 1
391
+ }
370
392
}
371
393
372
394
public init ( _ value: UInt ) {
Original file line number Diff line number Diff line change 1
- // Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
1
+ // Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
2
2
// Licensed under Apache License v2.0 with Runtime Library Exception
3
3
//
4
4
// See https://swift.org/LICENSE.txt for license information
@@ -119,6 +119,10 @@ class TestDecimal : TestDecimalSuper {
119
119
expectFalse ( zero. isInfinite)
120
120
expectFalse ( zero. isNaN)
121
121
expectFalse ( zero. isSignaling)
122
+
123
+ let d1 = Decimal ( 1234567890123456789 as UInt64 )
124
+ expectEqual ( d1. _exponent, 0 )
125
+ expectEqual ( d1. _length, 4 )
122
126
}
123
127
func test_Constants( ) {
124
128
expectEqual ( 8 , NSDecimalMaxSize)
You can’t perform that action at this time.
0 commit comments