[issue13889] str(float) and round(float) issues with FPU precision
New submission from Samuel Iseli : We are using python as an embedded scripting environment in our ERP-product. Since upgrading to python2.7 we have serious issues with floats: >>> 28710.0 '2870:.0' >>> round(28710.0) 2870.0 We are embedding Python in a Delphi-application. The problem was already discussed in issue9980 and has to do with Delphi setting the FPU precision to 64bit (and relying on this setting) while the standard with Microsoft Tools is 53bits. The routines _Py_dg_dtoa and _Py_dg_strtod in dtoa.c rely on the FPU precision set to 53bits. Issue9980 was closed as "won't fix" but I propose to reconsider this decision for the following reasons: - Delphi is still an important development environment for native win32 applications and has excellent Python embedding support through PythonForDelphi (http://code.google.com/p/python4delphi). - Ensuring 53bit before calling python code is not practical in an embedded python environment with extensions in delphi (python may also call code that is implemented in delphi). - The changes needed in pythoncore are minimal. Tests documented in issue9980 found no measurable performance impact. - FPU precision switching is needed and already (partially) implemented for linx, where 64bit prec is standard. Fixing this issues is absolutely vital for us, so we needed to compile a custom version of pythoncore. I appended a .diff file detailling the patch. Changes are needed in 2 places: - pyport.h, defining the _PY_SET_53_BIT_PRECISION macros for MS visual c compiler - floatobject.c, insert precision switching macros in _Py_double_round function. In pystrtod.c the precision switching macros are already used. pystrtod.c and floatobject.c are the only places in CPython where the dtoa.c functions are called. The macros for visual-c are activated by defining HAVE_VC_FUNC_FOR_X87 preprocessor-symbol. Hoping for inclusion of this patch. Cheers Samuel -- components: Interpreter Core files: 120127_float_dtoa_fix_py2.7.2.diff keywords: patch messages: 152099 nosy: samuel.iseli priority: normal severity: normal status: open title: str(float) and round(float) issues with FPU precision versions: Python 2.7, Python 3.1 Added file: http://bugs.python.org/file24341/120127_float_dtoa_fix_py2.7.2.diff ___ Python tracker <http://bugs.python.org/issue13889> ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue13889] str(float) and round(float) issues with FPU precision
Samuel Iseli added the comment: Hi Marc, the changes to the pythoncore.vcproj Visual-Studio file define the HAVE_VC_FUNC_FOR_X87 symbol. I use this symbol to enable the precision-setting macros in pyport.h. I made this similar to the existing code for gcc (linux). You can change this but currently this symbol has to be defined somewhere for the macros to have an effect. -- ___ Python tracker <http://bugs.python.org/issue13889> ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue13889] str(float) and round(float) issues with FPU precision
Samuel Iseli added the comment: I would definitely classify this as a bug in Python 2.7 as it breaks backwards-compatibility for embedding environments that default to 64bit FPU precision (e.g. Delphi). Additionally the bug is very hard to detect and leads to wrong values with possibly disastrous effects. Appended a patch with a new implementation of the Py_SET_53BIT_PRECISION_* macros for win32: - precision is set only when needed - setting and restoring only the x87 controlword (using __control87_2 function). - macros are not used for WIN64 as there's no x87 there - there's no need for a custom symbol in the vc project anymore, as I'm using the predefined _WIN32 symbol. -- Added file: http://bugs.python.org/file24408/74745.patch ___ Python tracker <http://bugs.python.org/issue13889> ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue13889] str(float) and round(float) issues with FPU precision
Samuel Iseli added the comment: There's an excess space after a line continuation backslash in my last patch, so it doesn't compile (pyport.h, line 567). Here's an additional patch that removes the space. Didn't find out how to combine the 2 revisions in one patch-file... Sorry about that. -- Added file: http://bugs.python.org/file24409/74747.patch ___ Python tracker <http://bugs.python.org/issue13889> ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue13889] str(float) and round(float) issues with FPU precision
Samuel Iseli added the comment: I can run the tests on 32bit. Would be nice if somebody else could do this on 64bit (my VS2008 machine is currently on 32bit-Windows). -- ___ Python tracker <http://bugs.python.org/issue13889> ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue13889] str(float) and round(float) issues with FPU precision
Samuel Iseli added the comment: Completed the patch by adding the rounding-control bits to the mask (_MCW_RC) and making sure the macros only get defined for MS-VC compiler (#ifdef _MSC_VER). Ran the tests (python_d -m test.autotest) on win32. Seems OK. The tests that were skipped or failed don't seem to be connected to this patch: - test_repr failed on trying to import a very long package and module name - test_popen failed with SyntaxError: unexpected EOF while parsing. Here's the summary: 323 tests OK. 2 tests failed: test_popen test_repr 2 tests altered the execution environment: test_distutils test_site 62 tests skipped: test_aepack test_al test_applesingle test_bsddb test_bsddb185 test_bsddb3 test_bz2 test_cd test_cl test_codecmaps_cn test_codecmaps_hk test_codecmaps_jp test_codecmaps_kr test_codecmaps_tw test_commands test_crypt test_curses test_dbm test_dl test_epoll test_fcntl test_fork1 test_gdb test_gdbm test_gl test_grp test_imgfile test_ioctl test_kqueue test_largefile test_linuxaudiodev test_macos test_macostools test_mhlib test_nis test_openpty test_ossaudiodev test_pipes test_poll test_posix test_pty test_pwd test_py3kwarn test_readline test_resource test_scriptpackages test_smtpnet test_socketserver test_sqlite test_ssl test_sunaudiodev test_tcl test_threadsignals test_timeout test_tk test_ttk_guionly test_ttk_textonly test_urllib2net test_urllibnet test_wait3 test_wait4 test_zipfile64 10 skips unexpected on win32: test_bsddb test_bz2 test_gdb test_readline test_sqlite test_ssl test_tcl test_tk test_ttk_guionly test_ttk_textonly [43048 refs] -- Added file: http://bugs.python.org/file24441/120206_set_53bit_precision.patch ___ Python tracker <http://bugs.python.org/issue13889> ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com