1

I noticed, that with Python 3.12 and a numpy.float64 when I simply do a

x = numpy.sqrt(3) # type is <class 'numpy.float64'> now
print(f"{x = }")

# this is printed
>>>x = np.float64(1.7320508075688772)

# in Python 3.11 this was printed
>>>x = 1.7320508075688772

I find this rather annoying, is there a cure for this behaviour?

Thanks a lot!

6
  • Is it the same numpy version? Commented Jul 16, 2024 at 6:47
  • 1
    Oh, my bad. I didn't check that on my old machine. With Python 3.11 I had numpy 1.23.5 and now with 3.12 it's 2.0.0. Commented Jul 16, 2024 at 6:51
  • Ahhh, I think I found it: numpy.org/devdocs/release/2.0.0-notes.html. I will use np.set_printoptions(legacy="1.25") for now. I understand why they are doing it, but I don't like it. Commented Jul 16, 2024 at 6:58
  • See stackoverflow.com/a/78634132. Commented Jul 16, 2024 at 7:28
  • 1
    This question is similar to: How to stop numpy floats being displayed as "np.float64"?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Jul 16, 2024 at 8:11

1 Answer 1

0

This is due to numpy 2 as mentionned in the comments. You could specify the print format:

>>>print(f"{x = :f}")
x = 1.732051
>>>print(f"{x = :.16f}")
x = 1.7320508075688772
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.