likebike / fasteval

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

Bitwise && logical operations #14

Open hanusek opened 2 years ago

hanusek commented 2 years ago

Hello! I have two problems with expressions.

  1. Library did not recognize booleans.
  2. The function - ez_eval() returns an incorrect value. The correct value is 2.

@lnicola @likebike Can you help me?

use fasteval::{EmptyNamespace, ez_eval};

let mut ns = EmptyNamespace;

// OK
let value = ez_eval("1 == 1", &mut ns).unwrap();
println!("1 == 1 -> fast_eval: {:?}", value);
assert_eq!(value, 1.0);

// 1.
// PROBLEM
let value = ez_eval("true == true", &mut ns).unwrap();
println!("true == true -> fast_eval: {:?}", value);
assert_eq!(value, 1.0);

// 2.
// PROBLEM, result = 3 ??
let bitand_text = format!("{} && {}",  b_value, b_mask);
let value = ez_eval(bitand_text.as_str(), &mut ns).unwrap();
println!("bitand_text {} -> fast_eval: {:?}", bitand_text, value);
assert_eq!(value, 2.0);
lnicola commented 2 years ago

Sorry, I've never used this crate and it hasn't changed in about two years. You might want to look for an alternative, or you could look into the implementation and see what it's doing for boolean and bit ops.