Skip to content

Commit bc217ef

Browse files
bors[bot]lucasmarqsjaparic
committed
79: implement ceil r=japaric a=lucasmarqs closes rust-lang#53 Co-authored-by: Lucas Marques <[email protected]> Co-authored-by: Jorge Aparicio <[email protected]>
2 parents 97fe0d8 + 8de7c5a commit bc217ef

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,6 @@ impl F32Ext for f32 {
347347
pub trait F64Ext: private::Sealed {
348348
fn floor(self) -> Self;
349349

350-
#[cfg(todo)]
351350
fn ceil(self) -> Self;
352351

353352
fn round(self) -> Self;
@@ -455,7 +454,6 @@ impl F64Ext for f64 {
455454
floor(self)
456455
}
457456

458-
#[cfg(todo)]
459457
#[inline]
460458
fn ceil(self) -> Self {
461459
ceil(self)

src/math/ceil.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use core::f64;
2+
3+
const TOINT: f64 = 1. / f64::EPSILON;
4+
5+
#[inline]
6+
pub fn ceil(x: f64) -> f64 {
7+
let u: u64 = x.to_bits();
8+
let e: i64 = (u >> 52 & 0x7ff) as i64;
9+
let y: f64;
10+
11+
if e >= 0x3ff + 52 || x == 0. {
12+
return x;
13+
}
14+
// y = int(x) - x, where int(x) is an integer neighbor of x
15+
y = if (u >> 63) != 0 {
16+
x - TOINT + TOINT - x
17+
} else {
18+
x + TOINT - TOINT - x
19+
};
20+
// special case because of non-nearest rounding modes
21+
if e <= 0x3ff - 1 {
22+
force_eval!(y);
23+
return if (u >> 63) != 0 { -0. } else { 1. };
24+
}
25+
if y < 0. {
26+
x + y + 1.
27+
} else {
28+
x + y
29+
}
30+
}
31+
32+
#[cfg(test)]
33+
mod tests {
34+
#[test]
35+
fn sanity_check() {
36+
assert_eq!(super::ceil(1.1), 2.0);
37+
assert_eq!(super::ceil(2.9), 3.0);
38+
}
39+
}

src/math/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ macro_rules! force_eval {
66
};
77
}
88

9+
mod ceil;
910
mod ceilf;
1011
mod cosf;
1112
mod expf;
@@ -35,6 +36,7 @@ mod trunc;
3536
mod truncf;
3637

3738
// Use separated imports instead of {}-grouped imports for easier merging.
39+
pub use self::ceil::ceil;
3840
pub use self::ceilf::ceilf;
3941
pub use self::cosf::cosf;
4042
pub use self::expf::expf;

test-generator/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ f64_f64! {
700700
// asin,
701701
// atan,
702702
// cbrt,
703-
// ceil,
703+
ceil,
704704
// cos,
705705
// cosh,
706706
// exp,

0 commit comments

Comments
 (0)