[nginx] svn commit: r4605 - trunk/src/core

igor at sysoev.ru igor at sysoev.ru
Mon Apr 23 11:11:32 UTC 2012


Author: is
Date: 2012-04-23 11:11:32 +0000 (Mon, 23 Apr 2012)
New Revision: 4605
URL: http://trac.nginx.org/nginx/changeset/4605/nginx

Log:
Fix of "%f" format handling.

ngx_sprintf("%.2f", 0.999) incorrectly resulted in "0.100" instead of "1.00".


Modified:
   trunk/src/core/ngx_string.c

Modified: trunk/src/core/ngx_string.c
===================================================================
--- trunk/src/core/ngx_string.c	2012-04-23 11:05:21 UTC (rev 4604)
+++ trunk/src/core/ngx_string.c	2012-04-23 11:11:32 UTC (rev 4605)
@@ -146,12 +146,12 @@
 {
     u_char                *p, zero;
     int                    d;
-    double                 f, scale;
+    double                 f;
     size_t                 len, slen;
     int64_t                i64;
-    uint64_t               ui64;
+    uint64_t               ui64, frac;
     ngx_msec_t             ms;
-    ngx_uint_t             width, sign, hex, max_width, frac_width, n;
+    ngx_uint_t             width, sign, hex, max_width, frac_width, scale, n;
     ngx_str_t             *v;
     ngx_variable_value_t  *vv;
 
@@ -365,28 +365,31 @@
                 }
 
                 ui64 = (int64_t) f;
+                frac = 0;
 
-                buf = ngx_sprintf_num(buf, last, ui64, zero, 0, width);
-
                 if (frac_width) {
 
-                    if (buf < last) {
-                        *buf++ = '.';
+                    scale = 1;
+                    for (n = frac_width; n; n--) {
+                        scale *= 10;
                     }
 
-                    scale = 1.0;
+                    frac = (uint64_t) ((f - (double) ui64) * scale + 0.5);
 
-                    for (n = frac_width; n; n--) {
-                        scale *= 10.0;
+                    if (frac == scale) {
+                        ui64++;
+                        frac = 0;
                     }
+                }
 
-                    /*
-                     * (int64_t) cast is required for msvc6:
-                     * it cannot convert uint64_t to double
-                     */
-                    ui64 = (uint64_t) ((f - (int64_t) ui64) * scale + 0.5);
+                buf = ngx_sprintf_num(buf, last, ui64, zero, 0, width);
 
-                    buf = ngx_sprintf_num(buf, last, ui64, '0', 0, frac_width);
+                if (frac_width) {
+                    if (buf < last) {
+                        *buf++ = '.';
+                    }
+
+                    buf = ngx_sprintf_num(buf, last, frac, '0', 0, frac_width);
                 }
 
                 fmt++;



More information about the nginx-devel mailing list