Skip to content

Commit 201a96a

Browse files
Merge ec12769 into 8446d5c
2 parents 8446d5c + ec12769 commit 201a96a

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

library/src/tests/math.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,28 @@ fn check_floor() {
367367

368368
#[test]
369369
fn check_round() {
370-
test_expression("Microsoft.Quantum.Math.Round(3.1)", &Value::Int(3));
371-
test_expression("Microsoft.Quantum.Math.Round(-3.7)", &Value::Int(-4));
370+
test_expression("Std.Math.Round(0.5-1.0e-16)", &Value::Int(0));
371+
test_expression("Std.Math.Round(0.5)", &Value::Int(0));
372+
test_expression("Std.Math.Round(0.5+1.0e-16)", &Value::Int(1));
373+
test_expression("Std.Math.Round(3.1)", &Value::Int(3));
374+
test_expression("Std.Math.Round(-3.7)", &Value::Int(-4));
375+
test_expression("Std.Math.Round(-3.5)", &Value::Int(-3));
376+
}
377+
378+
#[test]
379+
fn check_round_away_from_zero() {
380+
test_expression(
381+
"Std.Math.RoundHalfAwayFromZero(0.5-1.0e-16)",
382+
&Value::Int(0),
383+
);
384+
test_expression("Std.Math.RoundHalfAwayFromZero(0.5)", &Value::Int(1));
385+
test_expression(
386+
"Std.Math.RoundHalfAwayFromZero(0.5+1.0e-16)",
387+
&Value::Int(1),
388+
);
389+
test_expression("Std.Math.RoundHalfAwayFromZero(3.1)", &Value::Int(3));
390+
test_expression("Std.Math.RoundHalfAwayFromZero(-3.7)", &Value::Int(-4));
391+
test_expression("Std.Math.RoundHalfAwayFromZero(-3.5)", &Value::Int(-4));
372392
}
373393

374394
//

library/std/src/Std/Math.qs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -396,16 +396,27 @@ function Floor(value : Double) : Int {
396396
}
397397

398398
/// # 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)
401404
function Round(value : Double) : Int {
402405
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))
409420
}
410421

411422
//
@@ -1493,6 +1504,7 @@ export
14931504
Ceiling,
14941505
Floor,
14951506
Round,
1507+
RoundHalfAwayFromZero,
14961508
DivRemI,
14971509
DivRemL,
14981510
ModulusI,

0 commit comments

Comments
 (0)