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

Skip to content

Converting numpy float64 of python float to Decimal is not the same (get rounded in numpy case) #1833

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

Closed
seb5g opened this issue Jun 24, 2022 · 2 comments

Comments

@seb5g
Copy link

seb5g commented Jun 24, 2022

Environment

  • Pythonnet version: '2.5.1'
  • Python version: 3.8.12
  • Operating System: Win10
  • .NET Runtime: 4.8.4470.0
  • numpy version: 1.21.4

Details

  • Describe what you were trying to get done.

I had to convert floats to Decimal but somehow the current Double I got out of Decimal when looking at it using ToDouble would give me only the round part of the double. I figured that my floats were not native python but numpy float64 . This issue happens only with numpy floats..

    import clr
    from System import Decimal
    import numpy as np

    d= 45.5
    assert d == np.array([d])[0]  # should be fine
    assert Decimal(d) == Decimal(np.array([d])[0])  # assertion error 
    
@seb5g seb5g changed the title Converting numpy float64 of python float to Decimal is rounding it Converting numpy float64 of python float to Decimal is not the same (get rounded in numpy case) Jun 24, 2022
@lostmsu
Copy link
Member

lostmsu commented Jun 24, 2022

Python.NET 2.x permits all sorts of conversions in attempts to match the function arguments, some of which permit data loss. Please, try 3.0 previews of Python.NET. I believe 3.0 will tell you your Decimal(np.float64) call has no matching constructor overloads forcing you to explicitly convert np.float64 to either Python float or to System.Double first.

Also, to save time please add info about investigation from your side. E.g. in the example the equality of two decimals fails. What are their values at the time of comparison? Surely you checked before opening the bug.

@filmor
Copy link
Member

filmor commented Sep 20, 2022

This is fixed in rc6.

@filmor filmor closed this as completed Sep 20, 2022
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

No branches or pull requests

3 participants