-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathround.jule
More file actions
71 lines (63 loc) · 1.48 KB
/
round.jule
File metadata and controls
71 lines (63 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright 2025 mertcandav.
// Use of this source code is governed by a BSD 3-Clause
// license that can be found in the LICENSE file.
// Implementation derived from the test gonum package.
//
// Copyright ©2013 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
use "std/math"
// Returns the half away from zero rounded value of x with precision.
//
// Special cases are:
// Round(±0, prec) = +0
// Round(±Inf, prec) = ±Inf
// Round(NaN, prec) = NaN
fn Round(mut x: f64, prec: int): f64 {
if x == 0 {
// Make sure zero is returned
// without the negative bit set.
ret 0
}
// Fast path for positive precision on integers.
if prec >= 0 && x == math::Trunc(x) {
ret x
}
pow := math::Pow10(prec)
intermed := x * pow
if math::IsInf(intermed, 0) {
ret x
}
x = math::Round(intermed)
if x == 0 {
ret 0
}
ret x / pow
}
// Returns the half even rounded value of x with precision.
//
// Special cases are:
// RoundEven(±0, prec) = +0
// RoundEven(±Inf, prec) = ±Inf
// RoundEven(NaN, prec) = NaN
fn RoundEven(mut x: f64, prec: int): f64 {
if x == 0 {
// Make sure zero is returned
// without the negative bit set.
ret 0
}
// Fast path for positive precision on integers.
if prec >= 0 && x == math::Trunc(x) {
ret x
}
pow := math::Pow10(prec)
intermed := x * pow
if math::IsInf(intermed, 0) {
ret x
}
x = math::RoundEven(intermed)
if x == 0 {
ret 0
}
ret x / pow
}