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

Skip to content

Fix conversion of 'float' and 'double' values #487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 14, 2017

Conversation

Konstantin-Posudevskiy
Copy link
Contributor

@Konstantin-Posudevskiy Konstantin-Posudevskiy commented Jun 12, 2017

Fix problem of conversion 'float' and 'double' values in converter.cs.

As there was a range check both for float and double values, which are less or greater than its MinValue and MaxValue accordingly,
several values like float.NegativeInfinity, float.PositiveInfinity and the same 'double' values cannot be converted from Python to .NET values.

Add error check after PyFloat_AsDouble call

Due to Python C API documentation, method PyFloat_AsDouble can return -1.0 upon failure. So it requires error check. This rule forces to check for error and throw exception in case of error.

Add tests, which cover problem of conversion float and double values.

Resolves: #486

Fix problem of conversion 'float' and 'double' values in converter.cs.
As there was a range check both for 'float' and 'double' values, which
are less or greater than its 'MinValue' and 'MaxValue' accordingly,
several values like 'float.NegativeInfinity', 'float.PositiveInfinity'
and the same 'double' values cannot be converted from Python to .NET
values.

Add error check after 'PyFloat_AsDouble' call.
Due to Python C API documentation, method 'PyFloat_AsDouble' can return
'-1.0' upon failure. So it requires error check. This rule forces to
check for error and throw exception in case of error.

Add tests, which cover problem of conversion 'float' and 'double'
values.

Resolves: pythonnet#486.
@den-run-ai
Copy link
Contributor

@Konstantin-Posudevskiy in .NET I get overflow exception, but not in python. Also precision in .NET is much less for some reason:

Python 2.7.13 |Anaconda custom (32-bit)| (default, Dec 19 2016, 13:36:02) [MSC v
.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> dmax = 1.7976931348623159e308
>>> dmax
inf
>>> dmax = 1.7976931348623156e308
>>> dmax
1.7976931348623155e+308
>>> dmax = 1.7976931348623154e308
>>> dmax
1.7976931348623153e+308
>>> dmax = 1.7976931348624159e308
>>> dmax
inf
>>> dmax+1e308
inf
scriptcs (ctrl-c to exit or :help for help)

> double dmax = 1.7976931348623159e308d
* ;
(2,15): error CS0594: Floating-point constant is outside the range of type 'doub
le'
> double dmax = 1.7976931348623156e308d;
> dmax
1.79769313486232E+308
> dmax = 1.7976931348623146e308d;
1.79769313486231E+308

@Konstantin-Posudevskiy
Copy link
Contributor Author

Konstantin-Posudevskiy commented Jun 13, 2017

@denfromufa, I absolutely agree with you.

This pull request addresses another issues. It only

  1. fixes invalid usages of Python C API method 'PyFloat_AsDouble';
  2. supports conversion from Python 'inf' and '-inf' to PositiveInfinity and NegativeInfinity for System.Double (using method 'PyFloat_AsDouble')
  3. supports conversion from Python float value to PositiveInfinity and NegativeInfinity for System.Single. As I am concerned, Python float values, which are in range of 4 bytes can be represented with the similar byte representation of System.Single in .NET, even if they are values of different precision.
    OverflowException is thrown for values, that are represented with 5-8 bytes
  4. adds tests for paragraphs (2) and (3).

I also checked failing Python tests. Please, take a look at method test_double_conversion.
Python tests expects for .NET overflow exception, in case when it passes 'inf' value. IMO, it is incorrect, as infinity is valid double value.

Python output for test values from test method test_double_conversion:

>>> dmax = 1.7976931348623159e308
>>> dmax
inf
>>> dmin = -1.7976931348623159e308
>>> dmin
-inf

@den-run-ai
Copy link
Contributor

@Konstantin-Posudevskiy can you fix this failing test?

        with pytest.raises(OverflowError):
>           ConversionTest().DoubleField = 1.7976931348623159e308
E           Failed: DID NOT RAISE <type 'exceptions.OverflowError'>

OverflowError is not .NET exception, it is Python exception and it should not be expected in this case.

@Konstantin-Posudevskiy
Copy link
Contributor Author

@denfromufa sure, I will do.

Konstantin-Posudevskiy added a commit to Konstantin-Posudevskiy/pythonnet that referenced this pull request Jun 13, 2017
Fix incorrect part of 'test_double_conversion' test in test_conversion.py.
An 'OverflowError' was expected for valid values, which represent Python
'inf' and '-inf'.
The problem was identified with support of conversion for Python 'inf'
and '-inf' to .NET System.Double PositiveInfinity and NegativeInfinity.

See also: pythonnet#487.
@codecov
Copy link

codecov bot commented Jun 13, 2017

Codecov Report

Merging #487 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #487      +/-   ##
==========================================
+ Coverage   74.23%   74.24%   +<.01%     
==========================================
  Files          64       64              
  Lines        5562     5564       +2     
  Branches      894      894              
==========================================
+ Hits         4129     4131       +2     
  Misses       1147     1147              
  Partials      286      286
Flag Coverage Δ
#setup_linux 75.71% <ø> (ø) ⬆️
#setup_windows 73.59% <100%> (ø) ⬆️
Impacted Files Coverage Δ
src/runtime/converter.cs 82.02% <100%> (+0.09%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update bfce5e2...315d2d1. Read the comment docs.

Fix incorrect part of 'test_double_conversion' test in test_conversion.py.
An 'OverflowError' was expected for valid values, which represent Python
'inf' and '-inf'.
The problem was identified with support of conversion for Python 'inf'
and '-inf' to .NET System.Double PositiveInfinity and NegativeInfinity.

See also: pythonnet#487.
@den-run-ai den-run-ai merged commit d39f9f6 into pythonnet:master Jun 14, 2017
testrunner123 pushed a commit to testrunner123/pythonnet that referenced this pull request Sep 22, 2017
* Fix conversion of 'float' and 'double' values

Fix problem of conversion 'float' and 'double' values in converter.cs.
As there was a range check both for 'float' and 'double' values, which
are less or greater than its 'MinValue' and 'MaxValue' accordingly,
several values like 'float.NegativeInfinity', 'float.PositiveInfinity'
and the same 'double' values cannot be converted from Python to .NET
values.

Add error check after 'PyFloat_AsDouble' call.
Due to Python C API documentation, method 'PyFloat_AsDouble' can return
'-1.0' upon failure. So it requires error check. This rule forces to
check for error and throw exception in case of error.

Add tests, which cover problem of conversion 'float' and 'double'
values.

Resolves: pythonnet#486.

* Fix failing 'test_double_conversion' test

Fix incorrect part of 'test_double_conversion' test in test_conversion.py.
An 'OverflowError' was expected for valid values, which represent Python
'inf' and '-inf'.
The problem was identified with support of conversion for Python 'inf'
and '-inf' to .NET System.Double PositiveInfinity and NegativeInfinity.

See also: pythonnet#487.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants