@@ -279,7 +279,6 @@ public protocol FloatingPoint: Comparable, IntegerLiteralConvertible,
279
279
/// representable value.
280
280
mutating func divide( by other: Self )
281
281
282
- /* TODO: Need a remainder builtin or to call C remainder[fl] for these
283
282
/// Remainder of `self` divided by `other`. This is the IEEE 754 remainder
284
283
/// operation.
285
284
///
@@ -298,7 +297,6 @@ public protocol FloatingPoint: Comparable, IntegerLiteralConvertible,
298
297
299
298
/// Mutating form of `remainder`.
300
299
mutating func formRemainder( dividingBy other: Self )
301
- */
302
300
303
301
/// Remainder of `self` divided by `other` using truncating division.
304
302
/// Equivalent to the C standard library function `fmod`.
@@ -314,8 +312,6 @@ public protocol FloatingPoint: Comparable, IntegerLiteralConvertible,
314
312
/// Mutating form of `truncatingRemainder`.
315
313
mutating func formTruncatingRemainder( dividingBy other: Self )
316
314
317
- /* TODO: Need to call C sqrt and fma or llvm sqrt and fma intrinsics
318
- * for these.
319
315
/// Square root of `self`.
320
316
func squareRoot( ) -> Self
321
317
@@ -328,7 +324,6 @@ public protocol FloatingPoint: Comparable, IntegerLiteralConvertible,
328
324
329
325
/// Fused multiply-add, accumulating the product of `lhs` and `rhs` to `self`.
330
326
mutating func addProduct( _ lhs: Self , _ rhs: Self )
331
- */
332
327
333
328
/// The minimum of `x` and `y`. Implements the IEEE 754 `minNum` operation.
334
329
///
@@ -364,6 +359,35 @@ public protocol FloatingPoint: Comparable, IntegerLiteralConvertible,
364
359
/// `y` are quiet NaNs, a quiet NaN is returned.
365
360
static func maximumMagnitude( _ x: Self , _ y: Self ) -> Self
366
361
362
+ /// Returns the value of `self` rounded to an integral value using the
363
+ /// specified rounding rule. If the rounding rule is omitted, it defaults
364
+ /// to `.toNearestOrAwayFromZero`, aka "schoolbook rounding".
365
+ ///
366
+ /// This implements the C library rounding functions and the IEEE 754
367
+ /// operations that they bind.
368
+ ///
369
+ /// - `round(x)` is `x.rounded()` (the default rounding rule is provided by
370
+ /// an extension. This implements the IEEE 754 `roundToIntegralTiesToAway`
371
+ /// operation.
372
+ ///
373
+ /// - `roundeven(x)` is `x.rounded(.toNearestOrEven)`. This implements
374
+ /// the IEEE 754 `roundToIntegralTiesToEven` operation.
375
+ ///
376
+ /// - `trunc(x)` is `x.rounded(.towardZero)`. This implements the IEEE 754
377
+ /// `roundToIntegralTowardZero` operation.
378
+ ///
379
+ /// - `ceil(x)` is `x.rounded(.up)`. This implements the IEEE 754
380
+ /// `roundToIntegralTowardPositive` operation.
381
+ ///
382
+ /// - `floor(x)` is `x.rounded(.down)`. This implements the IEEE 754
383
+ /// `roundToIntegralTowardNegative` operation.
384
+ func rounded( _ rule: FloatingPointRoundingRule ) -> Self
385
+
386
+ /// Rounds `self` to an integral value using the specified rounding rule.
387
+ /// If the rounding rule is omitted, it defaults to
388
+ /// `.toNearestOrAwayFromZero`, aka "schoolbook rounding".
389
+ mutating func round( _ rule: FloatingPointRoundingRule )
390
+
367
391
/// The least representable value that compares greater than `self`.
368
392
///
369
393
/// - If `x` is `-infinity`, then `x.nextUp` is `-greatestMagnitude`.
@@ -490,6 +514,35 @@ public enum FloatingPointClassification {
490
514
case positiveInfinity
491
515
}
492
516
517
+ /// Describes a rule for rounding a floating-point number.
518
+ public enum FloatingPointRoundingRule {
519
+
520
+ /// The result is the closest allowed value; if two values are equally close,
521
+ /// the one with greater magnitude is chosen. Also known as "schoolbook
522
+ /// rounding".
523
+ case toNearestOrAwayFromZero
524
+
525
+ /// The result is the closest allowed value; if two values are equally close,
526
+ /// the even one is chosen. Also known as "bankers rounding".
527
+ case toNearestOrEven
528
+
529
+ /// The result is the closest allowed value that is greater than or equal
530
+ /// to the source.
531
+ case up
532
+
533
+ /// The result is the closest allowed value that is less than or equal to
534
+ /// the source.
535
+ case down
536
+
537
+ /// The result is the closest allowed value whose magnitude is less than or
538
+ /// equal to that of the source.
539
+ case towardZero
540
+
541
+ /// The result is the closest allowed value whose magnitude is greater than
542
+ /// or equal to that of the source.
543
+ case awayFromZero
544
+ }
545
+
493
546
@_transparent
494
547
public prefix func + < T : FloatingPoint > ( x: T ) -> T {
495
548
return x
@@ -522,12 +575,10 @@ public func ${op[0]}=<T : FloatingPoint>(lhs: inout T, rhs: T) {
522
575
523
576
% end
524
577
525
- /* TODO: uncomment once implemented.
526
578
@_transparent
527
579
public func sqrt< T : FloatingPoint> ( _ rhs: T) - > T {
528
580
return rhs. squareRoot ( )
529
581
}
530
- */
531
582
532
583
@_transparent
533
584
public func == < T : FloatingPoint > ( lhs: T , rhs: T ) -> Bool {
@@ -675,14 +726,47 @@ extension FloatingPoint {
675
726
return Self ( 1 ) . ulp
676
727
}
677
728
729
+ @_transparent
730
+ public func rounded( _ rule: FloatingPointRoundingRule ) -> Self {
731
+ var lhs = self
732
+ lhs. round ( rule)
733
+ return lhs
734
+ }
735
+
736
+ /// Returns `self` rounded to the closest integral value. If `self` is
737
+ /// exactly halfway between two integers (e.g. 1.5), the integral value
738
+ /// with greater magnitude (2.0 in this example) is returned.
739
+ ///
740
+ /// Other rounding modes can be explicitly specified with
741
+ /// `.rounded(_: FloatingPointRoundingRule)`.
742
+ @_transparent
743
+ public func rounded( ) -> Self {
744
+ return rounded ( . toNearestOrAwayFromZero)
745
+ }
746
+
747
+ /// Rounds `self` to the closest integral value. If `self` is exactly
748
+ /// halfway between two integers (e.g. 1.5), the integral value with
749
+ /// greater magnitude is selected.
750
+ ///
751
+ /// Other rounding modes can be explicitly specified with
752
+ /// `.round(_: FloatingPointRoundingRule)`.
753
+ @_transparent
754
+ public mutating func round( ) {
755
+ round ( . toNearestOrAwayFromZero)
756
+ }
757
+
758
+ @_transparent
759
+ public var nextDown : Self {
760
+ return - ( - self ) . nextUp
761
+ }
762
+
678
763
@_transparent
679
764
public func truncatingRemainder( dividingBy rhs: Self ) -> Self {
680
765
var lhs = self
681
766
lhs. formTruncatingRemainder ( dividingBy: rhs)
682
767
return lhs
683
768
}
684
769
685
- /* TODO: uncomment once implemented.
686
770
@_transparent
687
771
public func remainder( dividingBy rhs: Self ) -> Self {
688
772
var lhs = self
@@ -703,7 +787,6 @@ extension FloatingPoint {
703
787
addend. addProduct ( lhs, rhs)
704
788
return addend
705
789
}
706
- */
707
790
708
791
public static func minimum( _ left: Self , _ right: Self ) -> Self {
709
792
if left. isSignalingNaN || right. isSignalingNaN {
0 commit comments