0

Situation

  • I need to read data from a file using fits from astropy.io, which uses in numpy.
  • Some of the values I get when reading are very small negative float32 numbers, when there actually shouldn't exist negative values on the data (because of the data characteristics).

Questions

  • Can it be that those numbers were very small float64, that when read and casted to float32 became negative? If yes, how small do they have to be?
  • Is there a way to rewind the process, i.e., to get the original positive very small float64 value?
3
  • Most likely, the error was introduced at some point before the file was written, not when the file was read. Commented Nov 19, 2015 at 22:17
  • @user2357112 Either way, lets suppose they were casted into float32 when written and not then read. Could I get the original float64 value back from the float32, knowing that it should be an extremely small value? Commented Nov 19, 2015 at 22:25
  • No, and neither cast would change a number's sign anyway. Commented Nov 19, 2015 at 22:26

1 Answer 1

1
  • Can it be that those numbers were very small float64, that when read and casted to float32 became negative? If yes, how small do they have to be?

No - if the original float64 value was smaller than the smallest representable float32 number then it would simply be equal to zero after casting:

tiny = np.finfo(np.float64).tiny    # smallest representable float64 value
print(tiny)
# 2.22507385851e-308
print(tiny == 0)
# False
print(np.float32(tiny))
# 0.0
print(np.float32(tiny) == 0)
# True

Casting from one signed representation to another always preserves the sign bit.

  • Is there a way to rewind the process, i.e., to get the original positive very small float64 value?

No - casting down from 64 to 32 bit means you are effectively throwing away half of the information in the original representation, and once it's gone there's no magic way to recover it.

A much more plausible explanation for the negative values is that they result from rounding errors on calculations performed on the data before it was stored.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the explanation :)

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.