Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a4fac36

Browse files
committed
Merged revisions 65069 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r65069 | eric.smith | 2008-07-17 13:48:39 -0400 (Thu, 17 Jul 2008) | 1 line Issue 3382: Make '%F' and float.__format__('F') convert results to upper case. ........
1 parent ddc5669 commit a4fac36

7 files changed

Lines changed: 44 additions & 18 deletions

File tree

Doc/library/stdtypes.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,9 +1223,9 @@ The conversion types are:
12231223
+------------+-----------------------------------------------------+-------+
12241224
| ``'E'`` | Floating point exponential format (uppercase). | \(3) |
12251225
+------------+-----------------------------------------------------+-------+
1226-
| ``'f'`` | Floating point decimal format. | \(3) |
1226+
| ``'f'`` | Floating point decimal format (lowercase). | \(3) |
12271227
+------------+-----------------------------------------------------+-------+
1228-
| ``'F'`` | Floating point decimal format. | \(3) |
1228+
| ``'F'`` | Floating point decimal format (uppercase). | \(3) |
12291229
+------------+-----------------------------------------------------+-------+
12301230
| ``'g'`` | Floating point format. Uses lowercase exponential | \(4) |
12311231
| | format if exponent is less than -4 or not less than | |

Doc/library/string.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -402,13 +402,14 @@ The available presentation types for floating point and decimal values are:
402402
| ``'e'`` | Exponent notation. Prints the number in scientific |
403403
| | notation using the letter 'e' to indicate the exponent. |
404404
+---------+----------------------------------------------------------+
405-
| ``'E'`` | Exponent notation. Same as ``'e'`` except it uses an |
406-
| | upper case 'E' as the separator character. |
405+
| ``'E'`` | Exponent notation. Same as ``'e'`` except it converts |
406+
| | the number to upper case. |
407407
+---------+----------------------------------------------------------+
408408
| ``'f'`` | Fixed point. Displays the number as a fixed-point |
409409
| | number. |
410410
+---------+----------------------------------------------------------+
411-
| ``'F'`` | Fixed point. Same as ``'f'``. |
411+
| ``'F'`` | Fixed point. Same as ``'f'`` except it converts the |
412+
| | number to upper case. |
412413
+---------+----------------------------------------------------------+
413414
| ``'g'`` | General format. This prints the number as a fixed-point |
414415
| | number, unless the number is too large, in which case |

Lib/test/test_format.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,17 @@ def test_format(self):
7979
testformat("%#.*f", (110, -1.e+100/3.))
8080
testformat("%#.*F", (110, -1.e+100/3.))
8181
overflowrequired = 0
82+
# check for %f and %F
83+
testformat("%f", (1.0,), "1.000000")
84+
testformat("%F", (1.0,), "1.000000")
85+
testformat("%f", (1e100,), "1e+100")
86+
testformat("%F", (1e100,), "1E+100")
87+
testformat("%f", (1e100,), "1e+100")
88+
testformat("%F", (1e100,), "1E+100")
89+
testformat("%f", (float('nan'),), "nan")
90+
testformat("%F", (float('nan'),), "NAN")
91+
testformat("%f", (float('inf'),), "inf")
92+
testformat("%F", (float('inf'),), "INF")
8293
# Formatting of integers. Overflow is not ok
8394
overflowok = 0
8495
testformat("%x", 10, "a")

Lib/test/test_types.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -514,9 +514,17 @@ def test(f, format_spec, result):
514514
test( 1.0, '+f', '+1.000000')
515515
test(-1.0, '+f', '-1.000000')
516516
test(1.1234e90, 'f', '1.1234e+90')
517-
test(1.1234e90, 'F', '1.1234e+90')
517+
test(1.1234e90, 'F', '1.1234E+90')
518518
test(1.1234e200, 'f', '1.1234e+200')
519-
test(1.1234e200, 'F', '1.1234e+200')
519+
test(1.1234e200, 'F', '1.1234E+200')
520+
test(1e100, 'x<20f', '1e+100xxxxxxxxxxxxxx')
521+
test(1e100, 'x<20F', '1E+100xxxxxxxxxxxxxx')
522+
test(float('nan'), 'f', 'nan')
523+
test(float('nan'), 'F', 'NAN')
524+
test(float('inf'), 'f', 'inf')
525+
test(float('inf'), 'F', 'INF')
526+
test(float('-inf'), 'f', '-inf')
527+
test(float('-inf'), 'F', '-INF')
520528

521529
test( 1.0, 'e', '1.000000e+00')
522530
test(-1.0, 'e', '-1.000000e+00')

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ What's new in Python 3.0b2?
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #3382: Make '%F' and float.__format__('F') convert results
16+
to uppercase.
17+
1518
- Issue #3008: the float type has a new instance method 'float.hex'
1619
and a new class method 'float.fromhex' to convert floating-point
1720
numbers to and from hexadecimal strings, respectively.

Objects/stringlib/formatter.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -741,10 +741,6 @@ format_float_internal(PyObject *value,
741741
/* first, do the conversion as 8-bit chars, using the platform's
742742
snprintf. then, if needed, convert to unicode. */
743743

744-
/* 'F' is the same as 'f', per the PEP */
745-
if (type == 'F')
746-
type = 'f';
747-
748744
x = PyFloat_AsDouble(value);
749745

750746
if (x == -1.0 && PyErr_Occurred())
@@ -758,8 +754,12 @@ format_float_internal(PyObject *value,
758754

759755
if (precision < 0)
760756
precision = 6;
761-
if (type == 'f' && (fabs(x) / 1e25) >= 1e25)
762-
type = 'g';
757+
if ((type == 'f' || type == 'F') && (fabs(x) / 1e25) >= 1e25) {
758+
if (type == 'f')
759+
type = 'g';
760+
else
761+
type = 'G';
762+
}
763763

764764
/* cast "type", because if we're in unicode we need to pass a
765765
8-bit char. this is safe, because we've restricted what "type"

Objects/unicodeobject.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8588,8 +8588,12 @@ formatfloat(Py_UNICODE *buf,
85888588
return -1;
85898589
if (prec < 0)
85908590
prec = 6;
8591-
if (type == 'f' && (fabs(x) / 1e25) >= 1e25)
8592-
type = 'g';
8591+
if ((type == 'f' || type == 'F') && (fabs(x) / 1e25) >= 1e25) {
8592+
if (type == 'f')
8593+
type = 'g';
8594+
else
8595+
type = 'G';
8596+
}
85938597
/* Worst case length calc to ensure no buffer overrun:
85948598
85958599
'g' formats:
@@ -8608,7 +8612,8 @@ formatfloat(Py_UNICODE *buf,
86088612
*/
86098613
if (((type == 'g' || type == 'G') &&
86108614
buflen <= (size_t)10 + (size_t)prec) ||
8611-
(type == 'f' && buflen <= (size_t)53 + (size_t)prec)) {
8615+
((type == 'f' || type == 'F') &&
8616+
buflen <= (size_t)53 + (size_t)prec)) {
86128617
PyErr_SetString(PyExc_OverflowError,
86138618
"formatted float is too long (precision too large?)");
86148619
return -1;
@@ -9091,8 +9096,6 @@ PyObject *PyUnicode_Format(PyObject *format,
90919096
case 'F':
90929097
case 'g':
90939098
case 'G':
9094-
if (c == 'F')
9095-
c = 'f';
90969099
pbuf = formatbuf;
90979100
len = formatfloat(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE),
90989101
flags, prec, c, v);

0 commit comments

Comments
 (0)