about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBrian Anderson <banderson@mozilla.com>2012-02-12 22:04:06 -0800
committerBrian Anderson <banderson@mozilla.com>2012-02-12 22:04:06 -0800
commitad2f566ff244f442be5c4b304729a534d8a8185c (patch)
tree941f5d07a46a21342508c3abda0f02fe6b7a4048
parentacc57a44fde9e60163f3d4700bdca453f8a23a11 (diff)
downloadrust-ad2f566ff244f442be5c4b304729a534d8a8185c.tar.gz
rust-ad2f566ff244f442be5c4b304729a534d8a8185c.zip
core: Add abs functions for signed integer types
-rw-r--r--src/libcore/i16.rs5
-rw-r--r--src/libcore/i32.rs5
-rw-r--r--src/libcore/i64.rs5
-rw-r--r--src/libcore/i8.rs5
-rw-r--r--src/libcore/int.rs5
5 files changed, 25 insertions, 0 deletions
diff --git a/src/libcore/i16.rs b/src/libcore/i16.rs
index 1a4bc209902..fc6dd5f34de 100644
--- a/src/libcore/i16.rs
+++ b/src/libcore/i16.rs
@@ -34,3 +34,8 @@ fn range(lo: i16, hi: i16, it: fn(i16)) {
 fn compl(i: i16) -> i16 {
     u16::compl(i as u16) as i16
 }
+
+#[doc = "Computes the absolute value"]
+fn abs(i: i16) -> i16 {
+    if negative(i) { -i } else { i }
+}
diff --git a/src/libcore/i32.rs b/src/libcore/i32.rs
index 9e20712fdcc..519752682b8 100644
--- a/src/libcore/i32.rs
+++ b/src/libcore/i32.rs
@@ -34,3 +34,8 @@ fn range(lo: i32, hi: i32, it: fn(i32)) {
 fn compl(i: i32) -> i32 {
     u32::compl(i as u32) as i32
 }
+
+#[doc = "Computes the absolute value"]
+fn abs(i: i32) -> i32 {
+    if negative(i) { -i } else { i }
+}
diff --git a/src/libcore/i64.rs b/src/libcore/i64.rs
index 97c746b7b11..b8d12df92a1 100644
--- a/src/libcore/i64.rs
+++ b/src/libcore/i64.rs
@@ -34,3 +34,8 @@ fn range(lo: i64, hi: i64, it: fn(i64)) {
 fn compl(i: i64) -> i64 {
     u64::compl(i as u64) as i64
 }
+
+#[doc = "Computes the absolute value"]
+fn abs(i: i64) -> i64 {
+    if negative(i) { -i } else { i }
+}
diff --git a/src/libcore/i8.rs b/src/libcore/i8.rs
index 96fdbf9934f..1d96e0b247b 100644
--- a/src/libcore/i8.rs
+++ b/src/libcore/i8.rs
@@ -34,3 +34,8 @@ fn range(lo: i8, hi: i8, it: fn(i8)) {
 fn compl(i: i8) -> i8 {
     u8::compl(i as u8) as i8
 }
+
+#[doc = "Computes the absolute value"]
+fn abs(i: i8) -> i8 {
+    if negative(i) { -i } else { i }
+}
diff --git a/src/libcore/int.rs b/src/libcore/int.rs
index 8b32450e36c..41814b123ad 100644
--- a/src/libcore/int.rs
+++ b/src/libcore/int.rs
@@ -191,6 +191,11 @@ fn compl(i: int) -> int {
     uint::compl(i as uint) as int
 }
 
+#[doc = "Computes the absolute value"]
+fn abs(i: int) -> int {
+    if negative(i) { -i } else { i }
+}
+
 #[test]
 fn test_from_str() {
     assert(from_str("0") == 0);