likebike / fasteval

Fast and safe evaluation of algebraic expressions
https://crates.io/crates/fasteval/
MIT License
267 stars 26 forks source link

Support `int` and `bool` types #11

Closed optozorax closed 3 years ago

optozorax commented 3 years ago

It would be very nice if this crate can support these types along with their specific operations like bitwise operations (for int) or logic and/or (for bool), and equal/greater/less (for float + int → bool). And bool and float combination in rust-style: if my_bool { 1.0 } else { 0.0 } + sin(x).

optozorax commented 3 years ago

Hmm, I managed to solve this problem by introducing this functions:

let mut cb = |name: &str, args: Vec<f64>| -> Option<f64> {
    Some(match name {
        "if" =>  if *args.get(0)? == 1.0 { *args.get(1)? } else { *args.get(2)? }
        "and" => if *args.get(0)? == 1.0 && *args.get(1)? == 1.0 { 1.0 } else { 0.0 }
        "or" =>  if *args.get(0)? == 1.0 || *args.get(1)? == 1.0 { 1.0 } else { 0.0 }
        "not" => if *args.get(0)? == 1.0 { 0.0 } else { 1.0 }

        // Other user code
        _ => // ...
    })
};

So, personally, I have no more need in int and bool types in fasteval :thinking:

optozorax commented 3 years ago

Oh, there are also the && and || operators already in fasteval. Yes, this support is not needed, maybe add if function.