Skip to content

Commit ad2f566

Browse files
committed
core: Add abs functions for signed integer types
1 parent acc57a4 commit ad2f566

File tree

5 files changed

+25
-0
lines changed

5 files changed

+25
-0
lines changed

src/libcore/i16.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,8 @@ fn range(lo: i16, hi: i16, it: fn(i16)) {
3434
fn compl(i: i16) -> i16 {
3535
u16::compl(i as u16) as i16
3636
}
37+
38+
#[doc = "Computes the absolute value"]
39+
fn abs(i: i16) -> i16 {
40+
if negative(i) { -i } else { i }
41+
}

src/libcore/i32.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,8 @@ fn range(lo: i32, hi: i32, it: fn(i32)) {
3434
fn compl(i: i32) -> i32 {
3535
u32::compl(i as u32) as i32
3636
}
37+
38+
#[doc = "Computes the absolute value"]
39+
fn abs(i: i32) -> i32 {
40+
if negative(i) { -i } else { i }
41+
}

src/libcore/i64.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,8 @@ fn range(lo: i64, hi: i64, it: fn(i64)) {
3434
fn compl(i: i64) -> i64 {
3535
u64::compl(i as u64) as i64
3636
}
37+
38+
#[doc = "Computes the absolute value"]
39+
fn abs(i: i64) -> i64 {
40+
if negative(i) { -i } else { i }
41+
}

src/libcore/i8.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,8 @@ fn range(lo: i8, hi: i8, it: fn(i8)) {
3434
fn compl(i: i8) -> i8 {
3535
u8::compl(i as u8) as i8
3636
}
37+
38+
#[doc = "Computes the absolute value"]
39+
fn abs(i: i8) -> i8 {
40+
if negative(i) { -i } else { i }
41+
}

src/libcore/int.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,11 @@ fn compl(i: int) -> int {
191191
uint::compl(i as uint) as int
192192
}
193193

194+
#[doc = "Computes the absolute value"]
195+
fn abs(i: int) -> int {
196+
if negative(i) { -i } else { i }
197+
}
198+
194199
#[test]
195200
fn test_from_str() {
196201
assert(from_str("0") == 0);

0 commit comments

Comments
 (0)