[Python-checkins] r71155 - in python/branches/py3k-short-float-repr: Lib/test/formatfloat_testcases.txt Python/pystrtod.c

mark.dickinson python-checkins at python.org
Sat Apr 4 17:09:28 CEST 2009


Author: mark.dickinson
Date: Sat Apr  4 17:09:28 2009
New Revision: 71155

Log:
Sort out repr formatting


Modified:
   python/branches/py3k-short-float-repr/Lib/test/formatfloat_testcases.txt
   python/branches/py3k-short-float-repr/Python/pystrtod.c

Modified: python/branches/py3k-short-float-repr/Lib/test/formatfloat_testcases.txt
==============================================================================
--- python/branches/py3k-short-float-repr/Lib/test/formatfloat_testcases.txt	(original)
+++ python/branches/py3k-short-float-repr/Lib/test/formatfloat_testcases.txt	Sat Apr  4 17:09:28 2009
@@ -245,3 +245,33 @@
 %#.4g 234.56 -> 234.6
 %#.5g 234.56 -> 234.56
 %#.6g 234.56 -> 234.560
+
+-- repr formatting.  Should always include a decimal point or an exponent.
+%r 0 -> 0.0
+%r 1 -> 1.0
+
+-- short repr:  should produce shortest string that rounds correctly
+%r 0.01 -> 0.01
+%r 0.02 -> 0.02
+%r 0.03 -> 0.03
+%r 0.04 -> 0.04
+%r 0.05 -> 0.05
+%r 1.23456789 -> 1.23456789
+%r 10 -> 10.0
+%r 100 -> 100.0
+
+-- values >= 1e16 get an exponent
+%r 1e15 -> 1000000000000000.0
+%r 0.999999999999e16 -> 9999999999990000.0
+%r 1e16 -> 1e+16
+%r 1.000000000001e16 -> 1.000000000001e+16
+%r 1e17 -> 1e+17
+
+-- as do values < 1e-4
+%r 1e-3 -> 0.001
+%r 1.001e-4 -> 0.0001001
+%r 1.000000000001e-4 -> 0.0001000000000001
+%r 1e-4 -> 0.0001
+%r 0.999999999999e-4 -> 9.99999999999e-05
+%r 0.999e-4 -> 9.99e-05
+%r 1e-5 -> 1e-05

Modified: python/branches/py3k-short-float-repr/Python/pystrtod.c
==============================================================================
--- python/branches/py3k-short-float-repr/Python/pystrtod.c	(original)
+++ python/branches/py3k-short-float-repr/Python/pystrtod.c	Sat Apr  4 17:09:28 2009
@@ -609,6 +609,14 @@
 		else
 			trailing_zeros = 0;
 		break;
+	case 'r':
+		/* use exponent for values >= 1e16 or < 1e-4 */
+		if ((-4 < decpt) && (decpt <= 16))
+			use_exp = 0;
+		else
+			use_exp = 1;
+		trailing_zeros = 0;
+		break;
 	}
 
 	/* Always add a negative sign, and a plus sign if always_add_sign. */
@@ -736,7 +744,6 @@
 		break;
 	case 'r':
 		/* "repr" pseudo-mode */
-		lc_format_code = 'g';
 		mode = 0;
 		break;
 	}


More information about the Python-checkins mailing list