Add further documentation about float VS int behavior.

Relates to #89
This commit is contained in:
Sebastian Schmidt 2021-08-01 15:22:10 +03:00
parent fc2f5ac50d
commit 053f3cf877
2 changed files with 22 additions and 4 deletions

View File

@ -132,8 +132,8 @@ Supported binary operators:
|----------|------------|-------------| |----------|------------|-------------|
| ^ | 120 | Exponentiation | | ^ | 120 | Exponentiation |
| * | 100 | Product | | * | 100 | Product |
| / | 100 | Division | | / | 100 | Division (integer if both arguments are integers, otherwise float) |
| % | 100 | Modulo | | % | 100 | Modulo (integer if both arguments are integers, otherwise float) |
| + | 95 | Sum or String Concatenation | | + | 95 | Sum or String Concatenation |
| - | 95 | Difference | | - | 95 | Difference |
| < | 80 | Lower than | | < | 80 | Lower than |
@ -167,6 +167,15 @@ Operators that take numbers as arguments can either take integers or floating po
If one of the arguments is a floating point number, all others are converted to floating point numbers as well, and the resulting value is a floating point number as well. If one of the arguments is a floating point number, all others are converted to floating point numbers as well, and the resulting value is a floating point number as well.
Otherwise, the result is an integer. Otherwise, the result is an integer.
An exception to this is the exponentiation operator that always returns a floating point number. An exception to this is the exponentiation operator that always returns a floating point number.
Example:
```rust
use evalexpr::*;
assert_eq!(eval("1 / 2"), Ok(Value::from(0)));
assert_eq!(eval("1.0 / 2"), Ok(Value::from(0.5)));
assert_eq!(eval("2^2"), Ok(Value::from(4.0)));
```
#### The Aggregation Operator #### The Aggregation Operator

View File

@ -115,8 +115,8 @@
//! |----------|------------|-------------| //! |----------|------------|-------------|
//! | ^ | 120 | Exponentiation | //! | ^ | 120 | Exponentiation |
//! | * | 100 | Product | //! | * | 100 | Product |
//! | / | 100 | Division | //! | / | 100 | Division (integer if both arguments are integers, otherwise float) |
//! | % | 100 | Modulo | //! | % | 100 | Modulo (integer if both arguments are integers, otherwise float) |
//! | + | 95 | Sum or String Concatenation | //! | + | 95 | Sum or String Concatenation |
//! | - | 95 | Difference | //! | - | 95 | Difference |
//! | < | 80 | Lower than | //! | < | 80 | Lower than |
@ -150,6 +150,15 @@
//! If one of the arguments is a floating point number, all others are converted to floating point numbers as well, and the resulting value is a floating point number as well. //! If one of the arguments is a floating point number, all others are converted to floating point numbers as well, and the resulting value is a floating point number as well.
//! Otherwise, the result is an integer. //! Otherwise, the result is an integer.
//! An exception to this is the exponentiation operator that always returns a floating point number. //! An exception to this is the exponentiation operator that always returns a floating point number.
//! Example:
//!
//! ```rust
//! use evalexpr::*;
//!
//! assert_eq!(eval("1 / 2"), Ok(Value::from(0)));
//! assert_eq!(eval("1.0 / 2"), Ok(Value::from(0.5)));
//! assert_eq!(eval("2^2"), Ok(Value::from(4.0)));
//! ```
//! //!
//! #### The Aggregation Operator //! #### The Aggregation Operator
//! //!