@@ -396,16 +396,27 @@ function Floor(value : Double) : Int {
396
396
}
397
397
398
398
/// # Summary
399
- /// Returns the nearest integer to the specified number.
400
- /// For example: Round(3.7) = 4; Round(-3.7) = -4
399
+ /// Returns the nearest integer to the specified number. Half is rounded towards zero.
400
+ /// For example: Round(3.7) = 4; Round(-3.7) = -4; Round(3.5) = 3;
401
+ ///
402
+ /// # References
403
+ /// [Wikipedia article - Rounding](https://en.wikipedia.org/wiki/Rounding#Rounding_half_toward_zero)
401
404
function Round (value : Double ) : Int {
402
405
let (truncated , remainder , isPositive ) = ExtendedTruncation (value );
403
- if AbsD (remainder ) <= 1e - 15 {
404
- truncated
405
- } else {
406
- let abs = AbsD (remainder );
407
- truncated + (abs <= 0.5 ? 0 | (isPositive ? 1 | - 1 ))
408
- }
406
+ let abs = AbsD (remainder );
407
+ truncated + (abs <= 0.5 ? 0 | (isPositive ? 1 | - 1 ))
408
+ }
409
+
410
+ /// # Summary
411
+ /// Returns the nearest integer to the specified number. Half is rounded away from zero.
412
+ /// For example: RoundHalfAwayFromZero(-3.7) = -4, RoundHalfAwayFromZero(3.5) = 4;
413
+ ///
414
+ /// # References
415
+ /// [Wikipedia article - Rounding](https://en.wikipedia.org/wiki/Rounding#Rounding_half_away_from_zero)
416
+ function RoundHalfAwayFromZero (value : Double ) : Int {
417
+ let (truncated , remainder , isPositive ) = ExtendedTruncation (value );
418
+ let abs = AbsD (remainder );
419
+ truncated + (abs < 0.5 ? 0 | (isPositive ? 1 | - 1 ))
409
420
}
410
421
411
422
//
@@ -1493,6 +1504,7 @@ export
1493
1504
Ceiling ,
1494
1505
Floor ,
1495
1506
Round ,
1507
+ RoundHalfAwayFromZero ,
1496
1508
DivRemI ,
1497
1509
DivRemL ,
1498
1510
ModulusI ,
0 commit comments