Skip to content

Add scientific notation format specifier for floating point values #10843

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
SiegeLord opened this issue Dec 7, 2013 · 1 comment
Closed

Comments

@SiegeLord
Copy link
Contributor

This would constitute an addition of two new formatting flags, {:e} and {:E} and corresponding traits (e.g LowerSci and UpperSci). Here's the output I envision based on how Rust handles floating point and signs today. Notably, this differs from how C does it (Rust already differs from C, see #1375):

format!("{:e}", 1.0) == "1e0";
format!("{:e}", 10.0) == "1e1";
format!("{:+e}", 10.0) == "+1e+1";
format!("{:e}", 1.1234567) == "1.123457e0";
format!("{:e}", 1.3e-1) == "1.3e-1";
format!("{:.2e}", 1.3e-1) == "1.30e-1";
format!("{:E}", 1.3e-1) == "1.3E-1";
@huonw
Copy link
Member

huonw commented Dec 7, 2013

Duplicate of #6593. (FWIW, I'm personally not keen doing this with new format specifiers, when we can just use newtype structs; others may differ in this opinion.)

@huonw huonw closed this as completed Dec 7, 2013
flip1995 pushed a commit to flip1995/rust that referenced this issue Aug 11, 2023
…sons, r=Centri3

New lints: `impossible_comparisons` and `redundant_comparisons`

Inspired by a bug we had in production, like all good lints ;)

Adds two lints for "double" comparisons, specifically when the same expression is being compared against two different constants.

`impossible_comparisons` checks for expressions that can never be true at all. Example:
```rust
status_code <= 400 && status_code > 500
```
Presumably, the programmer intended to write `status_code >= 400 && status_code < 500` in this case.

`redundant_comparisons` checks for expressions where either half has no effect. Example:
```rust
status_code <= 400 && status_code < 500
```

Works with other literal types like floats and strings, and *some* cases where the constant is more complex than a literal, see the tests for more.

**Limitations and/or future work:**
* Doesn't work if the LHS can have side-effects at all, for example by being a function call
* Only works for exactly two comparison expressions, so `x > y && x > z && x < w` won't get checked
* Doesn't check for comparison expressions combined with `||`. Very similar logic could be applied there.

changelog: New lints [`impossible_comparisons`] and [`redundant_comparisons`]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants