GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-40780: Fix failure of _Py_dg_dtoa to remove trailing zeros (GH-20… · python/cpython@ad088ca

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • Lib/test
    • test_format.py
  • Misc/NEWS.d/next/Core and Builtins
    • 2020-05-26-17-43-58.bpo-40780.3Ckdgm.rst
  • Python
    • dtoa.c
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎Lib/test/test_format.py‎

Copy file name to clipboardExpand all lines: Lib/test/test_format.py
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,17 @@ def test_precision_c_limits(self):
484484
with self.assertRaises(ValueError) as cm:
485485
format(c, ".%sf" % (INT_MAX + 1))
486486

487+
def test_g_format_has_no_trailing_zeros(self):
488+
# regression test for bugs.python.org/issue40780
489+
self.assertEqual("%.3g" % 1505.0, "1.5e+03")
490+
self.assertEqual("%#.3g" % 1505.0, "1.50e+03")
491+
492+
self.assertEqual(format(1505.0, ".3g"), "1.5e+03")
493+
self.assertEqual(format(1505.0, "#.3g"), "1.50e+03")
494+
495+
self.assertEqual(format(12300050.0, ".6g"), "1.23e+07")
496+
self.assertEqual(format(12300050.0, "#.6g"), "1.23000e+07")
497+
487498

488499
if __name__ == "__main__":
489500
unittest.main()
Collapse file

‎Misc/NEWS.d/next/Core and Builtins/2020-05-26-17-43-58.bpo-40780.3Ckdgm.rst‎

Copy file name to clipboard
+2Lines changed: 2 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a corner case where g-style string formatting of a float failed to
2+
remove trailing zeros.
Collapse file

‎Python/dtoa.c‎

Copy file name to clipboardExpand all lines: Python/dtoa.c
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@
6464
* 7. _Py_dg_strtod has been modified so that it doesn't accept strings with
6565
* leading whitespace.
6666
*
67+
* 8. A corner case where _Py_dg_dtoa didn't strip trailing zeros has been
68+
* fixed. (bugs.python.org/issue40780)
69+
*
6770
***************************************************************/
6871

6972
/* Please send bug reports for the original dtoa.c code to David M. Gay (dmg
@@ -2563,6 +2566,14 @@ _Py_dg_dtoa(double dd, int mode, int ndigits,
25632566
}
25642567
++*s++;
25652568
}
2569+
else {
2570+
/* Strip trailing zeros. This branch was missing from the
2571+
original dtoa.c, leading to surplus trailing zeros in
2572+
some cases. See bugs.python.org/issue40780. */
2573+
while (s > s0 && s[-1] == '0') {
2574+
--s;
2575+
}
2576+
}
25662577
break;
25672578
}
25682579
}

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.