Skip to content

Commit 5af53e2

Browse files
Kimundibrson
authored andcommitted
---
yaml --- r: 42263 b: refs/heads/master c: 96f0512 h: refs/heads/master i: 42261: 5a53183 42259: 13e66ac 42255: acb390a v: v3
1 parent daf1ef5 commit 5af53e2

File tree

7 files changed

+127
-1
lines changed

7 files changed

+127
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 40f0b45f8e42357f1f16669ab937e13df21161f3
2+
refs/heads/master: 96f0512a45ffebbe7488332ec5e44827a14ce78d
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 2f46b763da2c098913884f101b6d71d69af41b49
55
refs/heads/try: 3d5418789064fdb463e872a4e651af1c628a3650

trunk/src/libcore/num/f32.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,33 @@ pub extern {
306306
fn floorf32(val: f32) -> f32;
307307
}
308308

309+
impl f32: num::Round {
310+
#[inline(always)]
311+
pure fn round(&self, mode: num::RoundMode) -> f32 {
312+
match mode {
313+
num::RoundDown => floor(*self),
314+
num::RoundUp => ceil(*self),
315+
num::RoundToZero if is_negative(*self) => ceil(*self),
316+
num::RoundToZero => floor(*self),
317+
num::RoundFromZero if is_negative(*self) => floor(*self),
318+
num::RoundFromZero => ceil(*self)
319+
}
320+
}
321+
322+
#[inline(always)]
323+
pure fn floor(&self) -> f32 { floor(*self) }
324+
#[inline(always)]
325+
pure fn ceil(&self) -> f32 { ceil(*self) }
326+
#[inline(always)]
327+
pure fn fract(&self) -> f32 {
328+
if is_negative(*self) {
329+
(*self) - ceil(*self)
330+
} else {
331+
(*self) - floor(*self)
332+
}
333+
}
334+
}
335+
309336
//
310337
// Local Variables:
311338
// mode: rust

trunk/src/libcore/num/f64.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,33 @@ pub extern {
330330
fn floorf64(val: f64) -> f64;
331331
}
332332

333+
impl f64: num::Round {
334+
#[inline(always)]
335+
pure fn round(&self, mode: num::RoundMode) -> f64 {
336+
match mode {
337+
num::RoundDown => floor(*self),
338+
num::RoundUp => ceil(*self),
339+
num::RoundToZero if is_negative(*self) => ceil(*self),
340+
num::RoundToZero => floor(*self),
341+
num::RoundFromZero if is_negative(*self) => floor(*self),
342+
num::RoundFromZero => ceil(*self)
343+
}
344+
}
345+
346+
#[inline(always)]
347+
pure fn floor(&self) -> f64 { floor(*self) }
348+
#[inline(always)]
349+
pure fn ceil(&self) -> f64 { ceil(*self) }
350+
#[inline(always)]
351+
pure fn fract(&self) -> f64 {
352+
if is_negative(*self) {
353+
(*self) - ceil(*self)
354+
} else {
355+
(*self) - floor(*self)
356+
}
357+
}
358+
}
359+
333360
//
334361
// Local Variables:
335362
// mode: rust

trunk/src/libcore/num/float.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,39 @@ impl float: num::One {
488488
static pure fn one() -> float { 1.0 }
489489
}
490490

491+
impl float: num::Round {
492+
#[inline(always)]
493+
pure fn round(&self, mode: num::RoundMode) -> float {
494+
match mode {
495+
num::RoundDown
496+
=> f64::floor(*self as f64) as float,
497+
num::RoundUp
498+
=> f64::ceil(*self as f64) as float,
499+
num::RoundToZero if is_negative(*self)
500+
=> f64::ceil(*self as f64) as float,
501+
num::RoundToZero
502+
=> f64::floor(*self as f64) as float,
503+
num::RoundFromZero if is_negative(*self)
504+
=> f64::floor(*self as f64) as float,
505+
num::RoundFromZero
506+
=> f64::ceil(*self as f64) as float
507+
}
508+
}
509+
510+
#[inline(always)]
511+
pure fn floor(&self) -> float { f64::floor(*self as f64) as float}
512+
#[inline(always)]
513+
pure fn ceil(&self) -> float { f64::ceil(*self as f64) as float}
514+
#[inline(always)]
515+
pure fn fract(&self) -> float {
516+
if is_negative(*self) {
517+
(*self) - (f64::ceil(*self as f64) as float)
518+
} else {
519+
(*self) - (f64::floor(*self as f64) as float)
520+
}
521+
}
522+
}
523+
491524
#[test]
492525
pub fn test_from_str() {
493526
assert from_str(~"3") == Some(3.);

trunk/src/libcore/num/int-template.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,18 @@ impl T: num::One {
200200
static pure fn one() -> T { 1 }
201201
}
202202
203+
impl T: num::Round {
204+
#[inline(always)]
205+
pure fn round(&self, _: num::RoundMode) -> T { *self }
206+
207+
#[inline(always)]
208+
pure fn floor(&self) -> T { *self }
209+
#[inline(always)]
210+
pure fn ceil(&self) -> T { *self }
211+
#[inline(always)]
212+
pure fn fract(&self) -> T { 0 }
213+
}
214+
203215
/**
204216
* Parse a buffer of bytes
205217
*

trunk/src/libcore/num/num.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,18 @@ pub trait Zero {
3535
pub trait One {
3636
static pure fn one() -> Self;
3737
}
38+
39+
pub trait Round {
40+
pure fn round(&self, mode: RoundMode) -> self;
41+
42+
pure fn floor(&self) -> self;
43+
pure fn ceil(&self) -> self;
44+
pure fn fract(&self) -> self;
45+
}
46+
47+
pub enum RoundMode {
48+
RoundDown,
49+
RoundUp,
50+
RoundToZero,
51+
RoundFromZero
52+
}

trunk/src/libcore/num/uint-template.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,18 @@ impl T: num::One {
160160
static pure fn one() -> T { 1 }
161161
}
162162
163+
impl T: num::Round {
164+
#[inline(always)]
165+
pure fn round(&self, _: num::RoundMode) -> T { *self }
166+
167+
#[inline(always)]
168+
pure fn floor(&self) -> T { *self }
169+
#[inline(always)]
170+
pure fn ceil(&self) -> T { *self }
171+
#[inline(always)]
172+
pure fn fract(&self) -> T { 0 }
173+
}
174+
163175
/**
164176
* Parse a buffer of bytes
165177
*

0 commit comments

Comments
 (0)