mirror of
https://github.com/PaddiM8/kalker.git
synced 2025-03-02 23:31:15 +01:00
Bitshift operations, closes #80
This commit is contained in:
parent
42895cb342
commit
de6e5106e9
@ -91,10 +91,15 @@ lazy_static! {
|
|||||||
m.insert("sqrt", (UnaryFuncInfo(sqrt, Other), ""));
|
m.insert("sqrt", (UnaryFuncInfo(sqrt, Other), ""));
|
||||||
m.insert("√", (UnaryFuncInfo(sqrt, Other), ""));
|
m.insert("√", (UnaryFuncInfo(sqrt, Other), ""));
|
||||||
m.insert("trunc", (UnaryFuncInfo(trunc, Other), ""));
|
m.insert("trunc", (UnaryFuncInfo(trunc, Other), ""));
|
||||||
|
m.insert("bitcmp", (UnaryFuncInfo(bitcmp, Other), ""));
|
||||||
m
|
m
|
||||||
};
|
};
|
||||||
pub static ref BINARY_FUNCS: HashMap<&'static str, (BinaryFuncInfo, &'static str)> = {
|
pub static ref BINARY_FUNCS: HashMap<&'static str, (BinaryFuncInfo, &'static str)> = {
|
||||||
let mut m = HashMap::new();
|
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("max", (BinaryFuncInfo(max, Other), ""));
|
||||||
m.insert("min", (BinaryFuncInfo(min, Other), ""));
|
m.insert("min", (BinaryFuncInfo(min, Other), ""));
|
||||||
m.insert("hypot", (BinaryFuncInfo(hypot, 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
|
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 {
|
pub fn hypot(x: KalkNum, y: KalkNum) -> KalkNum {
|
||||||
if x.has_imaginary() || y.has_imaginary() {
|
if x.has_imaginary() || y.has_imaginary() {
|
||||||
let abs_x = abs(x);
|
let abs_x = abs(x);
|
||||||
|
@ -18,6 +18,41 @@ pub(crate) mod funcs {
|
|||||||
KalkNum::new(x.value.gamma(), &x.unit)
|
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 {
|
pub fn hypot(x: KalkNum, y: KalkNum) -> KalkNum {
|
||||||
if x.has_imaginary() || y.has_imaginary() {
|
if x.has_imaginary() || y.has_imaginary() {
|
||||||
let abs_x = abs(x);
|
let abs_x = abs(x);
|
||||||
|
Loading…
Reference in New Issue
Block a user