8000 Numerical accuracy when converting from Single to float · Issue #1896 · pythonnet/pythonnet · GitHub
[go: up one dir, main page]

Skip to content 6479
Numerical accuracy when converting from Single to float #1896
Closed
@m-rossi

Description

@m-rossi

Environment

  • Pythonnet version: 3.0.0rc4
  • Python version: 3.10.5
  • Operating System: Windows 10 20H2
  • .NET Runtime: 6.0.6

Details

  • Describe what you were trying to get done.

    I'm sorry it's me again with some weird low-level issue. I use a DLL where a function returns a System.Single. Its return value is 0.01. Before pythonnet version 3.0 this got converted to a Python float also of value 0.01. With Pythonnet 3.0 I get a Python float 0.009999999776482582 instead. I can reproduce this issue by creating System.Single objects.

  • What commands did you run to trigger this issue? If you can provide a
    Minimal, Complete, and Verifiable example
    this will help us understand the issue.

    import clr
    import System
    
    # Parse a string to avoid conversion from Python float to dotnet
    print(System.Single.Parse('1e-2'))
    # Create a pure dotnet object without conversion back to Python (I think .ToString() gets called here?)
    print(System.Single(0.01))
    # Create a pure dotnet object without conversion back to Python and convert back to Python
    print(System.Single(0.01).MemberwiseClone())
    0.009999999776482582
    0.01
    0.009999999776482582
    

    With pythonnet 2.5.2 this will return

    0.01
    0.01
    0.01
    

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0