koolhazz / stringencoders

Automatically exported from code.google.com/p/stringencoders
Other
0 stars 0 forks source link

Another variant of modp_dtoa which removes trailing zeros from output #9

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi Nick,

This is Ilya Martynov, perhaps you still remember me :)

We are you using your library at iponweb with a small patch to modp_dtoa
which changes it to remove trailing zeros from its output. The patch is
trivial:

--- modp_numtoa.c.orig  2010-03-18 12:24:40.000000000 +0300
+++ modp_numtoa.c   2010-03-18 12:27:59.000000000 +0300
@@ -141,9 +141,14 @@
             /* 1.5 -> 2, but 2.5 -> 2 */
             ++whole;
         }
-    } else {
+    } else if (frac) {
         int count = prec;
         // now do fractional part, as an unsigned number
+        // we know it is not 0 but we can have leading zeros, these should
be removed
+        while (!(frac % 10)) {
+            --count;
+            frac /= 10;
+        }
         do {
             --count;
             *wstr++ = (char)(48 + (frac % 10));

It would be nice if it was integrated into your library in some form.
Perhaps modp_dtoa() should have a boolean flag to let user choose between
removing or keeping trailing zeros? I'm not sure on the best API to support
both current and this logic.

Original issue reported on code.google.com by ilya.m...@gmail.com on 18 Mar 2010 at 9:42

GoogleCodeExporter commented 9 years ago
Oh course I remember!  Hello!

Can you provide some sample values or a test case, so I can get this in faster?

I can't think of any reason why we would want to preserve the trailing zeros -- 
people can uses printf if they 
want that level of control.

Original comment by nickgsup...@gmail.com on 18 Mar 2010 at 1:59

GoogleCodeExporter commented 9 years ago
The use case for this is that we need a faster replacement for sprintf(s, 
"%.14g",
n). We are using Lua for scripting and this is exactly how it internally 
converts
numbers to strings. In our testing we found that in some our applications we are
spending quite a lot of time in number to string conversions so we just wanted 
to
optimize it by replacing sprintf with something faster. Not removing trailing 
zeros
generally doesn't work well for Lua as it only has double as a native numeric 
type.
So you have to store integers as doubles as well and if you don't strip trailing
zeros they look ugly when printing them say to log files.

As for a test case you can just compare the result with sprintf(s, "%.XXg", n) 
output
where XX is precision.

Original comment by ilya.m...@gmail.com on 18 Mar 2010 at 5:21

GoogleCodeExporter commented 9 years ago
thanks for the background.

I'll take a look tonight.

Original comment by nickgsup...@gmail.com on 18 Mar 2010 at 6:38

GoogleCodeExporter commented 9 years ago
Interesting.  

Instead of flags, I just made another function modp_dtoa2.  Testing turned out 
to be tricky since this format-
style does not exist within printf!  However, it's in, with tests.

svn ci -m 'Issue 9: dtoa2, same as dtoa2 but with no trailing zeros after the 
decimal point'
Sending        src/modp_numtoa.c
Sending        src/modp_numtoa.h
Sending        test/modp_numtoa_test.c
Transmitting file data ...
Committed revision 222.

I also noticed that there are no tests for Inf/Nan. I'll make a new release 
once those work correctly.

Original comment by nickgsup...@gmail.com on 19 Mar 2010 at 2:05