mirror of
https://github.com/PaddiM8/kalker.git
synced 2025-02-12 06:29:17 +01:00
Bitshift operations, closes #80
This commit is contained in:
parent
1bdecd3921
commit
7892eee23e
@ -91,10 +91,15 @@ lazy_static! {
|
||||
m.insert("sqrt", (UnaryFuncInfo(sqrt, Other), ""));
|
||||
m.insert("√", (UnaryFuncInfo(sqrt, Other), ""));
|
||||
m.insert("trunc", (UnaryFuncInfo(trunc, Other), ""));
|
||||
m.insert("bitcmp", (UnaryFuncInfo(bitcmp, Other), ""));
|
||||
m
|
||||
};
|
||||
pub static ref BINARY_FUNCS: HashMap<&'static str, (BinaryFuncInfo, &'static str)> = {
|
||||
let mut m = HashMap::new();
|
||||
m.insert("bitand", (BinaryFuncInfo(bitand, Other), ""));
|
||||
m.insert("bitor", (BinaryFuncInfo(bitor, Other), ""));
|
||||
m.insert("bitxor", (BinaryFuncInfo(bitxor, Other), ""));
|
||||
m.insert("bitshift", (BinaryFuncInfo(bitshift, Other), ""));
|
||||
m.insert("max", (BinaryFuncInfo(max, Other), ""));
|
||||
m.insert("min", (BinaryFuncInfo(min, Other), ""));
|
||||
m.insert("hypot", (BinaryFuncInfo(hypot, Other), ""));
|
||||
|
@ -50,6 +50,32 @@ pub(crate) mod funcs {
|
||||
2f64.sqrt() * pi.sqrt() * t.powf(x - 0.5f64) * (-t).exp() * a
|
||||
}
|
||||
|
||||
pub fn bitcmp(x: KalkNum, y: KalkNum) -> KalkNum {
|
||||
KalkNum::from(!x.value.round() as i32)
|
||||
}
|
||||
|
||||
pub fn bitand(x: KalkNum, y: KalkNum) -> KalkNum {
|
||||
KalkNum::from(x.value.round() as i32 & y.value.round() as i32)
|
||||
}
|
||||
|
||||
pub fn bitor(x: KalkNum, y: KalkNum) -> KalkNum {
|
||||
KalkNum::from(x.value.round() as i32 | y.value.round() as i32)
|
||||
}
|
||||
|
||||
pub fn bitxor(x: KalkNum, y: KalkNum) -> KalkNum {
|
||||
KalkNum::from(x.value.round() as i32 ^ y.value.round() as i32)
|
||||
}
|
||||
|
||||
pub fn bitshift(x: KalkNum, y: KalkNum) -> KalkNum {
|
||||
let x = x.value.round() as i32;
|
||||
let y = y.value.round() as i32;
|
||||
if y < 0 {
|
||||
KalkNum::from((x >> y.abs()))
|
||||
} else {
|
||||
KalkNum::from((x << y))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn hypot(x: KalkNum, y: KalkNum) -> KalkNum {
|
||||
if x.has_imaginary() || y.has_imaginary() {
|
||||
let abs_x = abs(x);
|
||||
|
@ -18,6 +18,41 @@ pub(crate) mod funcs {
|
||||
KalkNum::new(x.value.gamma(), &x.unit)
|
||||
}
|
||||
|
||||
pub fn bitcmp(x: KalkNum) -> KalkNum {
|
||||
KalkNum::from(!x.value.to_i32_saturating().unwrap_or(i32::MAX))
|
||||
}
|
||||
|
||||
pub fn bitand(x: KalkNum, y: KalkNum) -> KalkNum {
|
||||
KalkNum::from(
|
||||
x.value.to_i32_saturating().unwrap_or(i32::MAX)
|
||||
& y.value.to_i32_saturating().unwrap_or(i32::MAX),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn bitor(x: KalkNum, y: KalkNum) -> KalkNum {
|
||||
KalkNum::from(
|
||||
x.value.to_i32_saturating().unwrap_or(i32::MAX)
|
||||
| y.value.to_i32_saturating().unwrap_or(i32::MAX),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn bitxor(x: KalkNum, y: KalkNum) -> KalkNum {
|
||||
KalkNum::from(
|
||||
x.value.to_i32_saturating().unwrap_or(i32::MAX)
|
||||
^ y.value.to_i32_saturating().unwrap_or(i32::MAX),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn bitshift(x: KalkNum, y: KalkNum) -> KalkNum {
|
||||
let x = x.value.to_i32_saturating().unwrap_or(i32::MAX) as i32;
|
||||
let y = y.value.to_i32_saturating().unwrap_or(i32::MAX) as i32;
|
||||
if y < 0 {
|
||||
KalkNum::from(x >> y.abs())
|
||||
} else {
|
||||
KalkNum::from(x << y)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn hypot(x: KalkNum, y: KalkNum) -> KalkNum {
|
||||
if x.has_imaginary() || y.has_imaginary() {
|
||||
let abs_x = abs(x);
|
||||
|
Loading…
Reference in New Issue
Block a user