oridb / mc

Myrddin Compiler
MIT License
387 stars 34 forks source link

Support precision width for formatting floats #181

Open wasamasa opened 6 years ago

wasamasa commented 6 years ago

std.fmt does currently only offer the "w" and "p" flags for ensuring a minimum width and padding to that width. Something equivalent to "%.2f" in C would be nice to have, such a flag would set the exact width of the digits after the decimal point. There's a bit of complication in C mandating that rounding must happen and that a precision of zero is handled specially by not printing a decimal point at all.

oridb commented 6 years ago

Sorry, been pretty busy, haven't had a chance to get to this yet. However, for reference, the Ryu algorithm (https://github.com/ulfjack/ryu) also came out recently. TODO: Implement it and replace dragon4.

wasamasa commented 6 years ago

Note that I still don't have any idea or preference on what to name the precision flag. The obvious choice of "p" is already taken. Modula-3 uses a "prec" keyword, but that would look odd next to the single-letter ones. "f" would be easy to remember.