ISibboI / evalexpr

A powerful expression evaluation crate 🦀.
GNU Affero General Public License v3.0
320 stars 52 forks source link

Add support for scientific notation of the form `<coefficient>e{+,-,}<exponent>` #87

Closed twelho closed 3 years ago

twelho commented 3 years ago

Rust's number parsing supports parsing floats in the scientific notation format (playground example). This is useful for dealing with values of electronic components (resistance, capacitance), that are often very small/large.

Right now evalexpr correctly recognizes input of the form 10e3 as it's parsed as a single PartialToken::Literal. However, adding a + or - for negative values i.e. 10e-3 will cause str_to_partial_tokens to output [Literal("10e"), Minus, Literal("3")] due to the parsing logic not handling e in any special way. This causes evalexpr to not find a variable with the identifier 10e and fail. The proposed fix for this issue doesn't alter the partial token logic, but instead adds some extra handling when resolving partial tokens to full tokens. This is the smallest change I could come up with to implement this, let me know if it would be better to do it another way (the current architecture doesn't adapt to supporting this very easily). I've also added some additional cases to the integration tests to cover the scientific notation parsing.

ISibboI commented 3 years ago

Nice, thank you very much!

This looks good. I think you found the best way to implement this into the parser already. Let me handle the formatting issues.