-
Notifications
You must be signed in to change notification settings - Fork 50
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
lib/util: Implement %f to print floating point numbers #74
Comments
I've got an implementation working but need some input:
|
Great that you are working in this! I haven't quite thought through all of the issues here, so happy to have your input.
Given that, I think it'd be great if
IIUC your question (2) is what to do if |
Another thought is that having control over the number of decimal places printed is very useful too, so it might make sense to support the printf-style |
In the light of your comments there's something I just don't understand: if $ cat t.v3
def main() {
var b = StringBuilder.new();
b.putd(16777217f).ln(); System.puts(b.extract());
}
$ v3i t.v3 $VIRGIL_LOC/lib/util/*.v3
16777216 |
What's going on with
And the output:
|
Thanks for clearing that up. |
For reference on how this might be implemented, here is the Scala port of the relatively-new Ryu algorithm: scala-native/scala-native#1436 For anyone willing to pitch in on this issue, that's the "swinging for the fences" version. I think we could settle for something that is shorter and easier to read that gives the same results but might be ~4x slower, thus leaving the Ryu port for future work. |
Just after my last post around a month ago I dug a little deeper and realised that my "implementation" was naive to the point of embarrassment, then the World intruded and I laid to to one side. A real implementation is above my pay-grade and I just don't have the motivation or time to wrangle IEEE 754. Here's my code plus tests plus Makefile.
The tests:
Makefile:
|
Since adding floating point back in 2020, the Virgil utility libraries have not yet added support for printing/rendering floating point numbers (in decimal). In C, the
printf
format string supports%f
for specifying an argument is a floating point number. The analogous place to add code to handle this is in StringBuilder, which can deal with floating point strings.Slight differences with C printf and Virgil
StringBuffer
:StringBuilder
uses%d
(for decimal) for specifying decimal output of integer values. I think it'd be nice to just extendStringBuilder
doesn't yet support specifying the width (in characters) of the output item, nor left or right-justifyingI could use some help on this, and it might make a good starter project for someone new wanting to kick the tires with Virgil and contribute.
The text was updated successfully, but these errors were encountered: