I'm debugging my python program.

I have np.cos(param_t_10[0]) returns 5.9168e-06 where param_t_10[0] == tensor(-1.5708)

If I print np.cos(-1.5708) I get -3.673205103346574e-06 which is the correct value.

Can anyone explain me what am I doing wrong?

  • 1
    $\begingroup$ Make a reproducible example. $\endgroup$ Commented Oct 3, 2023 at 7:12
  • $\begingroup$ I would always rather use the made in tensorflow / pytorch cosine function rather than numpy one when working with tensors. Maybe trying them will give a different result. $\endgroup$
    – Ubikuity
    Commented Oct 3, 2023 at 13:22
  • $\begingroup$ Tensorflow often uses 32-bit floats by default. That's about 7 significant figures in terms of precision en.wikipedia.org/wiki/Single-precision_floating-point_format, so you should be worried when you get down to comparing such small quantities. $\endgroup$
    – Cryo
    Commented Oct 5, 2023 at 19:31

1 Answer 1


It is not a good idea to compare floating point numbers by ==. E.g., this may give different result depending on your number representation. Additionally, the conversion from a tensor to a float/numpy object can have side effects (you could print(param_t_10[0]) and print(float(param_t_10[0])) for a first check).

  • $\begingroup$ This is probably the source of the problem. Thank you. $\endgroup$
    – dsb
    Commented Nov 4, 2023 at 5:45

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.