1

In the most upvoted answer to the question Double precision floating values in Python? it is mentioned that we can use np.float128, if we need precision more than standard float. But I couldn't find a way to do this.

Then, in the most upvoted answer to how to declare variable type, C style in python, use of typing is suggested.

However, when I tried to use typing to set the datatype as float for a integer type value, Python still used an int data type.

n : float=10000
print(type(n))

But still, the result is :

<class 'int'>

So, How can I define np.float128 type variable in python?

Bhanuday Sharma
  • 323
  • 3
  • 10
  • 1
    *Variables* don't have types in Python; *values* do, and 10000 is perfectly representable as an `int`. – Scott Hunter Aug 06 '20 at 18:13
  • 1
    variables aren't typed in python. Python is a dynamically typed language. Type hints *are hints to people reading the source code*, or for use by third-party static type checkers. The Python runtime itself ignores them, basically. You want to create a `np.float128` *object*, but are you sure that's what you want? Why not `decimal.Decimal`, which supports arbitrary precision? – juanpa.arrivillaga Aug 06 '20 at 18:13

1 Answers1

2

Have you tried the following?

from numpy import float128

n=float128(10000)

print(type(n))

Print:

<class 'numpy.float128'>

Bear in mind that there are some issues using numpy.float128 on Windows 64-bit (see numpy.float128 doesn't exist in windows, but is called from OpenGL). I have tested this code in an online Python editor.

David Duran
  • 1,786
  • 1
  • 25
  • 36