Skip to content

#fmt uses wrong default precision for float #1375

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
brson opened this issue Dec 22, 2011 · 10 comments
Closed

#fmt uses wrong default precision for float #1375

brson opened this issue Dec 22, 2011 · 10 comments
Labels
E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

Comments

@brson
Copy link
Contributor

brson commented Dec 22, 2011

fmt("%f", 5.82) yields "5.82". Other printf's yield "5.820000".

@nikomatsakis
Copy link
Contributor

Not sure that's a bug.null

@lht
Copy link
Contributor

lht commented Dec 24, 2011

The default precision is 6. Looks like the trailing zeros are chopped.

input Rust C
5.821010 5.82101 5.821010
5.8210 5.820999 5.821000
5.821 5.820999 5.821000
5.82 5.82 5.820000
5.0 5 5.000000

@boggle
Copy link
Contributor

boggle commented Dec 26, 2011

float::to_str needs an overhaul. There is no support for NaN, +inf(inity), -inf(inity) and probably should use float::frexp instead of substraction and casting around (and perhaps get rid of that weird pow_uint_to_uint in the process). This likewise extends to from_str (don't know, needs checking).

Additionally it would be nice to write this in a f32, f64 agnostic way, or at least move it from float into two copies in f32, f64. Probably this should wait until we have typeclasses in which case this may better be done using a "float number builder" interface which would be passed to "float::to_str" to build a number of the correct type (alternative would be to use bigints when they get implemented)

In general, fmt probably should be rewritten for use with typeclasses so I'd say this ticket should remain on hold until they land but then there's a lot to do here.

killerswan added a commit to killerswan/rust that referenced this issue Jun 2, 2012
This seems to fix issue rust-lang#1876, and some of the superficial parts of
issue rust-lang#1375.  The #fmt macro and the to_str functions will round,
rather than truncate, floats as strings.

Other issues remain, and I wrote more code here than intended, but the
following should pass now.

```
fn x() {
   assert "3.1416"      == #fmt["%.4f", 3.14159];
   assert "3"           == #fmt["%.0f", 3.14159];
   assert "99"          == #fmt["%.0f", 98.5];
   assert "7.0000"      == #fmt["%.4f", 6.999999999];
   assert "3.141590000" == #fmt["%.9f", 3.14159];
}
```
@brson
Copy link
Contributor Author

brson commented Jun 2, 2012

@nikomatsakis Did your first comment indicate that you think we should not follow printf here? If so, I am starting to agree. printf's behavior is not really what I would expect.

brson pushed a commit that referenced this issue Jun 2, 2012
This seems to fix issue #1876, and some of the superficial parts of
issue #1375.  The #fmt macro and the to_str functions will round,
rather than truncate, floats as strings.

Other issues remain, and I wrote more code here than intended, but the
following should pass now.

```
fn x() {
   assert "3.1416"      == #fmt["%.4f", 3.14159];
   assert "3"           == #fmt["%.0f", 3.14159];
   assert "99"          == #fmt["%.0f", 98.5];
   assert "7.0000"      == #fmt["%.4f", 6.999999999];
   assert "3.141590000" == #fmt["%.9f", 3.14159];
}
```
@emberian
Copy link
Member

emberian commented Aug 5, 2013

Still relevant, although it does handle inf and NaN now.

@catamorphism
Copy link
Contributor

Sad, but not a milestone blocker.

@bstrie
Copy link
Contributor

bstrie commented Oct 17, 2013

Not sure I agree that we need to exactly imitate C-style format string defaults, especially since we no longer use C-style format strings and also because we've now actually implemented the ability to specify the precision when printing:

print!("{:f}", 100.000000004f64);  // 100
print!("{:.9f}", 100.000000004f64);  // 100.000000004

That first one might be a little worrying... but that's probably a different bug anyway.

@huonw
Copy link
Member

huonw commented Oct 26, 2013

cc #6220

@brson
Copy link
Contributor Author

brson commented Oct 31, 2013

I haven't followed this in a long time and don't know what the status is, but now that we're not explicitly copying printf, if our current behavior is sane, then I think that's just fine.

@alexcrichton
Copy link
Member

I agree with @brson that we're different enough from C that I don't think that this matters that much. We have different semantics, but that just means that we have our own semantics.

We're not losing functionality, you can still specify precisions that you want. For this reason, I'm going to close this. New bugs can be opened for any shortcomings found in our float formatting code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
Projects
None yet
Development

No branches or pull requests

9 participants