| Crates.io | string_calculator |
| lib.rs | string_calculator |
| version | 0.4.0 |
| created_at | 2024-06-29 20:33:48.199006+00 |
| updated_at | 2024-07-22 22:30:54.796206+00 |
| description | A string calculator to compute formulas inside strings. |
| homepage | |
| repository | https://github.com/FriquetLuca/string_calculator |
| max_upload_size | |
| id | 1287621 |
| size | 347,880 |
A small package containing eval methods to compute formulas inside strings.
Simply import the eval you need and use it.
use string_calculator::eval_f64;
fn main() {
println!("{}", eval_f64("(2+3) / 2".to_string(), 0.0)); // 2.5
}
By default, all features are enabled. If you only want a specific eval method, use the feature associated with it's name in the list:
eval_complexeval_decimaleval_f64eval_i64eval_numberExample:
[dependencies]
string_calculator = { version = "0.4", default-features = false, features = ["eval_decimal"] }
Since there's a lot of things that could be simplified visually, here's the full list of operators implemented.
1 + 2
= 32 - 1
= 13 * 2
= 64 / 2
= 2eval_decimal, eval_number, eval_f64 and eval_i64)
This operator handle the rest of the euclidian division between two number.
Example:
4 % 2
= 0eval_i64)
This operator shift the bits of x to y positions to the left (it's the same as multiplying by 2^y).
Example:
1 << 2
= 4eval_i64)
This operator shift the bits of x to y positions to the right (it's the same as dividing by 2^y).
Example:
4 >> 2
= 1x by y, x and y being both numbers.
Example:
3^3
= 27x by using the superscript notation (such as ²) for integers, x being a number.
Example:
5²
= 25eval_decimal, eval_number, eval_f64 and eval_i64)
This operator handle the factorial of a real x.
Example:
5!
= 120eval_complex, eval_number and eval_f64)
This operator handle the conversion from degree to radian. You should note that it's priority is the same as multiplication.
Example:
3°
= 0.05235987755982989eval_complex, eval_number and eval_f64)
This operator handle the conversion from radian to degree. You should note that it's priority is the same as multiplication.
Example:
3 rad
= 171.8873385393eval_complex, eval_number, eval_decimal and eval_f64)
Pi is available as pi or π.eval_complex, eval_number, eval_decimal and eval_f64)
E is available as e.Some function can be written purely using their original mathematical notation if wanted.
eval_f64, eval_number and eval_decimal)
This function gives the greatest integer less than or equal to x.
Example:
⌊2.4⌋
= 2eval_f64, eval_number and eval_decimal)
This function gives the smallest integer greater or equal to x.
Example:
⌈2.4⌉
= 3eval_decimal, eval_f64, eval_number and eval_i64)eval_decimal, eval_number, eval_f64 and eval_i64)eval_decimal, eval_number, eval_f64 and eval_i64)eval_decimal, eval_number, eval_f64 and eval_i64)eval_decimal, eval_number, eval_f64 and eval_i64)eval_f64, eval_number and eval_decimal)eval_f64, eval_number and eval_decimal)eval_f64, eval_number and eval_decimal)eval_f64, eval_number and eval_decimal)eval_f64, eval_number and eval_decimal)eval_f64, eval_number and eval_decimal)eval_complex, eval_number and eval_f64)eval_complex, eval_number and eval_f64)eval_complex, eval_number and eval_f64)eval_complex, eval_number and eval_f64)eval_complex, eval_number and eval_f64)eval_complex, eval_number and eval_f64)eval_complex, eval_number and eval_f64)eval_complex, eval_number and eval_f64)eval_complex, eval_number and eval_f64)eval_complex, eval_number and eval_f64)eval_complex, eval_number and eval_f64)eval_complex, eval_number and eval_f64)eval_f64 and eval_number)eval_i64)eval_i64)The @ symbol is used here as a placeholder for the value you want to put into the eval_XXXXX.
In the case you're writting a calculator, it might be useful to use your previous answer for example.