Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
Pyston
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
Pyston
Commits
14aa0f0a
Commit
14aa0f0a
authored
Nov 11, 2015
by
Marius Wachtler
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1006 from Daetalus/master
add float_repr_style to sys module and enable test_strtod
parents
e912efed
69d515d2
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
33 additions
and
143 deletions
+33
-143
from_cpython/Include/pyport.h
from_cpython/Include/pyport.h
+25
-0
from_cpython/Lib/test/test_strtod.py
from_cpython/Lib/test/test_strtod.py
+0
-1
from_cpython/Objects/floatobject.c
from_cpython/Objects/floatobject.c
+0
-3
src/runtime/builtin_modules/sys.cpp
src/runtime/builtin_modules/sys.cpp
+8
-0
src/runtime/float.cpp
src/runtime/float.cpp
+0
-138
test/CPYTHON_TEST_NOTES.md
test/CPYTHON_TEST_NOTES.md
+0
-1
No files found.
from_cpython/Include/pyport.h
View file @
14aa0f0a
...
...
@@ -458,3 +458,28 @@ typedef PY_LONG_LONG Py_intptr_t;
else if (errno == ERANGE) \
errno = 0; \
} while(0)
/* If we can't guarantee 53-bit precision, don't use the code
in Python/dtoa.c, but fall back to standard code. This
means that repr of a float will be long (17 sig digits).
Realistically, there are two things that could go wrong:
(1) doubles aren't IEEE 754 doubles, or
(2) we're on x86 with the rounding precision set to 64-bits
(extended precision), and we don't know how to change
the rounding precision.
*/
#if !defined(DOUBLE_IS_LITTLE_ENDIAN_IEEE754) && \
!defined(DOUBLE_IS_BIG_ENDIAN_IEEE754) && \
!defined(DOUBLE_IS_ARM_MIXED_ENDIAN_IEEE754)
#define PY_NO_SHORT_FLOAT_REPR
#endif
/* double rounding is symptomatic of use of extended precision on x86. If
we're seeing double rounding, and we don't have any mechanism available for
changing the FPU rounding precision, then don't use Python/dtoa.c. */
#if defined(X87_DOUBLE_ROUNDING) && !defined(HAVE_PY_SET_53BIT_PRECISION)
#define PY_NO_SHORT_FLOAT_REPR
#endif
from_cpython/Lib/test/test_strtod.py
View file @
14aa0f0a
# expected: fail
# Tests for the correctly-rounded string -> float conversions
# introduced in Python 2.7 and 3.1.
...
...
from_cpython/Objects/floatobject.c
View file @
14aa0f0a
...
...
@@ -1120,8 +1120,6 @@ float_long(PyObject *v)
#error "C doubles do not appear to be IEEE 754 binary64 format"
#endif
// pyston change: comment this out
#if 0
PyObject
*
_Py_double_round
(
double
x
,
int
ndigits
)
{
...
...
@@ -1258,7 +1256,6 @@ _Py_double_round(double x, int ndigits) {
_Py_dg_freedtoa
(
buf
);
return
result
;
}
#endif
#undef FIVE_POW_LIMIT
...
...
src/runtime/builtin_modules/sys.cpp
View file @
14aa0f0a
...
...
@@ -721,6 +721,14 @@ void setupSys() {
#ifdef Py_USING_UNICODE
SET_SYS_FROM_STRING
(
"maxunicode"
,
PyInt_FromLong
(
PyUnicode_GetMax
()));
#endif
/* float repr style: 0.03 (short) vs 0.029999999999999999 (legacy) */
#ifndef PY_NO_SHORT_FLOAT_REPR
SET_SYS_FROM_STRING
(
"float_repr_style"
,
PyString_FromString
(
"short"
));
#else
SET_SYS_FROM_STRING
(
"float_repr_style"
,
PyString_FromString
(
"legacy"
));
#endif
sys_flags_cls
->
tp_mro
=
BoxedTuple
::
create
({
sys_flags_cls
,
object_cls
});
sys_flags_cls
->
freeze
();
...
...
src/runtime/float.cpp
View file @
14aa0f0a
...
...
@@ -1565,144 +1565,6 @@ extern "C" double _PyFloat_Unpack8(const unsigned char* p, int le) noexcept {
}
}
#if DBL_MANT_DIG == 53
#define FIVE_POW_LIMIT 22
#else
#error "C doubles do not appear to be IEEE 754 binary64 format"
#endif
extern
"C"
PyObject
*
_Py_double_round
(
double
x
,
int
ndigits
)
noexcept
{
double
rounded
,
m
;
Py_ssize_t
buflen
,
mybuflen
=
100
;
char
*
buf
,
*
buf_end
,
shortbuf
[
100
],
*
mybuf
=
shortbuf
;
int
decpt
,
sign
,
val
,
halfway_case
;
PyObject
*
result
=
NULL
;
_Py_SET_53BIT_PRECISION_HEADER
;
/* Easy path for the common case ndigits == 0. */
if
(
ndigits
==
0
)
{
rounded
=
round
(
x
);
if
(
fabs
(
rounded
-
x
)
==
0.5
)
/* halfway between two integers; use round-away-from-zero */
rounded
=
x
+
(
x
>
0.0
?
0.5
:
-
0.5
);
return
PyFloat_FromDouble
(
rounded
);
}
/* The basic idea is very simple: convert and round the double to a
decimal string using _Py_dg_dtoa, then convert that decimal string
back to a double with _Py_dg_strtod. There's one minor difficulty:
Python 2.x expects round to do round-half-away-from-zero, while
_Py_dg_dtoa does round-half-to-even. So we need some way to detect
and correct the halfway cases.
Detection: a halfway value has the form k * 0.5 * 10**-ndigits for
some odd integer k. Or in other words, a rational number x is
exactly halfway between two multiples of 10**-ndigits if its
2-valuation is exactly -ndigits-1 and its 5-valuation is at least
-ndigits. For ndigits >= 0 the latter condition is automatically
satisfied for a binary float x, since any such float has
nonnegative 5-valuation. For 0 > ndigits >= -22, x needs to be an
integral multiple of 5**-ndigits; we can check this using fmod.
For -22 > ndigits, there are no halfway cases: 5**23 takes 54 bits
to represent exactly, so any odd multiple of 0.5 * 10**n for n >=
23 takes at least 54 bits of precision to represent exactly.
Correction: a simple strategy for dealing with halfway cases is to
(for the halfway cases only) call _Py_dg_dtoa with an argument of
ndigits+1 instead of ndigits (thus doing an exact conversion to
decimal), round the resulting string manually, and then convert
back using _Py_dg_strtod.
*/
/* nans, infinities and zeros should have already been dealt
with by the caller (in this case, builtin_round) */
assert
(
std
::
isfinite
(
x
)
&&
x
!=
0.0
);
/* find 2-valuation val of x */
m
=
frexp
(
x
,
&
val
);
while
(
m
!=
floor
(
m
))
{
m
*=
2.0
;
val
--
;
}
/* determine whether this is a halfway case */
if
(
val
==
-
ndigits
-
1
)
{
if
(
ndigits
>=
0
)
halfway_case
=
1
;
else
if
(
ndigits
>=
-
FIVE_POW_LIMIT
)
{
double
five_pow
=
1.0
;
int
i
;
for
(
i
=
0
;
i
<
-
ndigits
;
i
++
)
five_pow
*=
5.0
;
halfway_case
=
fmod
(
x
,
five_pow
)
==
0.0
;
}
else
halfway_case
=
0
;
}
else
halfway_case
=
0
;
/* round to a decimal string; use an extra place for halfway case */
_Py_SET_53BIT_PRECISION_START
;
buf
=
_Py_dg_dtoa
(
x
,
3
,
ndigits
+
halfway_case
,
&
decpt
,
&
sign
,
&
buf_end
);
_Py_SET_53BIT_PRECISION_END
;
if
(
buf
==
NULL
)
{
PyErr_NoMemory
();
return
NULL
;
}
buflen
=
buf_end
-
buf
;
/* in halfway case, do the round-half-away-from-zero manually */
if
(
halfway_case
)
{
int
i
,
carry
;
/* sanity check: _Py_dg_dtoa should not have stripped
any zeros from the result: there should be exactly
ndigits+1 places following the decimal point, and
the last digit in the buffer should be a '5'.*/
assert
(
buflen
-
decpt
==
ndigits
+
1
);
assert
(
buf
[
buflen
-
1
]
==
'5'
);
/* increment and shift right at the same time. */
decpt
+=
1
;
carry
=
1
;
for
(
i
=
buflen
-
1
;
i
--
>
0
;)
{
carry
+=
buf
[
i
]
-
'0'
;
buf
[
i
+
1
]
=
carry
%
10
+
'0'
;
carry
/=
10
;
}
buf
[
0
]
=
carry
+
'0'
;
}
/* Get new buffer if shortbuf is too small. Space needed <= buf_end -
buf + 8: (1 extra for '0', 1 for sign, 5 for exp, 1 for '\0'). */
if
(
buflen
+
8
>
mybuflen
)
{
mybuflen
=
buflen
+
8
;
mybuf
=
(
char
*
)
PyMem_Malloc
(
mybuflen
);
if
(
mybuf
==
NULL
)
{
PyErr_NoMemory
();
goto
exit
;
}
}
/* copy buf to mybuf, adding exponent, sign and leading 0 */
PyOS_snprintf
(
mybuf
,
mybuflen
,
"%s0%se%d"
,
(
sign
?
"-"
:
""
),
buf
,
decpt
-
(
int
)
buflen
);
/* and convert the resulting string back to a double */
errno
=
0
;
_Py_SET_53BIT_PRECISION_START
;
rounded
=
_Py_dg_strtod
(
mybuf
,
NULL
);
_Py_SET_53BIT_PRECISION_END
;
if
(
errno
==
ERANGE
&&
fabs
(
rounded
)
>=
1.
)
PyErr_SetString
(
PyExc_OverflowError
,
"rounded value too large to represent"
);
else
result
=
PyFloat_FromDouble
(
rounded
);
/* done computing value; now clean up */
if
(
mybuf
!=
shortbuf
)
PyMem_Free
(
mybuf
);
exit:
_Py_dg_freedtoa
(
buf
);
return
result
;
}
static
PyObject
*
float_getnewargs
(
PyFloatObject
*
v
)
noexcept
{
return
Py_BuildValue
(
"(d)"
,
v
->
ob_fval
);
}
...
...
test/CPYTHON_TEST_NOTES.md
View file @
14aa0f0a
...
...
@@ -198,7 +198,6 @@ test_sqlite [unknown]
test_ssl [unknown]
test_startfile [unknown]
test_str memory leak?
test_strtod [unknown]
test_structmembers [unknown]
test_struct [unknown]
test_subprocess exit code 141 [sigpipe?], no error message
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment