You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by tu...@apache.org on 2023/04/06 23:05:27 UTC
[arrow-rs] branch master updated: Minor: add methods "is_positive" and "signum" to i256 (#4024)
This is an automated email from the ASF dual-hosted git repository.
tustvold pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
The following commit(s) were added to refs/heads/master by this push:
new 2b77f6439 Minor: add methods "is_positive" and "signum" to i256 (#4024)
2b77f6439 is described below
commit 2b77f643947084a8c10164dc6dfdfecf6bc1a039
Author: Igor Izvekov <iz...@gmail.com>
AuthorDate: Fri Apr 7 02:05:21 2023 +0300
Minor: add methods "is_positive" and "signum" to i256 (#4024)
* Minor: add method "is_positive" to i256
* fix: cancel changes to functions "checked_add" and "checked_sub"
* feat: signum; test for is_positive, is_negative, signum
* feat: use i256::ONE, i256::MINUS_ONE, i256::ZERO
---
arrow-buffer/src/bigint.rs | 41 +++++++++++++++++++++++++++++++++++++++++
1 file changed, 41 insertions(+)
diff --git a/arrow-buffer/src/bigint.rs b/arrow-buffer/src/bigint.rs
index 3a9c4aac8..fab75b792 100644
--- a/arrow-buffer/src/bigint.rs
+++ b/arrow-buffer/src/bigint.rs
@@ -483,11 +483,31 @@ impl i256 {
acc.wrapping_mul(base)
}
+ /// Returns a number [`i256`] representing sign of this [`i256`].
+ ///
+ /// 0 if the number is zero
+ /// 1 if the number is positive
+ /// -1 if the number is negative
+ pub const fn signum(self) -> Self {
+ if self.is_positive() {
+ i256::ONE
+ } else if self.is_negative() {
+ i256::MINUS_ONE
+ } else {
+ i256::ZERO
+ }
+ }
+
/// Returns `true` if this [`i256`] is negative
#[inline]
pub const fn is_negative(self) -> bool {
self.high.is_negative()
}
+
+ /// Returns `true` if this [`i256`] is positive
+ pub const fn is_positive(self) -> bool {
+ self.high.is_positive() || self.high == 0 && self.low != 0
+ }
}
/// Temporary workaround due to lack of stable const array slicing
@@ -917,6 +937,27 @@ mod tests {
}
}
+ #[test]
+ fn test_signed_ops() {
+ // signum
+ assert_eq!(i256::from_i128(1).signum(), i256::ONE);
+ assert_eq!(i256::from_i128(0).signum(), i256::ZERO);
+ assert_eq!(i256::from_i128(-0).signum(), i256::ZERO);
+ assert_eq!(i256::from_i128(-1).signum(), i256::MINUS_ONE);
+
+ // is_positive
+ assert!(i256::from_i128(1).is_positive());
+ assert!(!i256::from_i128(0).is_positive());
+ assert!(!i256::from_i128(-0).is_positive());
+ assert!(!i256::from_i128(-1).is_positive());
+
+ // is_negative
+ assert!(!i256::from_i128(1).is_negative());
+ assert!(!i256::from_i128(0).is_negative());
+ assert!(!i256::from_i128(-0).is_negative());
+ assert!(i256::from_i128(-1).is_negative());
+ }
+
#[test]
#[cfg_attr(miri, ignore)]
fn test_i256_fuzz() {