0

I have some excerpt code for Matlab. I want to transfer to python3.7 code. But I found out some Matlab function I can not transfer to python 3.7 it, such as function typecast, and single in Matlab. Therefore, How could I write the python code and get the same result? Thank you

Here is the Matlab code

AA= uint32(3249047552)
DPD = typecast(AA,'single');    
Print(DPD)

DPD = -21.0664 <== This is matlab result.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
AAA
  • 9
  • 4
  • Convert `AA` to base 2 then use the IEEE Standard 754 for single precision to compute the corresponding single value. – obchardon Dec 16 '20 at 10:48
  • 1
    Please don’t change the question after you’ve received an answer. Your edit made the answer seem wrong. Instead, accept the answer: https://stackoverflow.com/help/someone-answers – Cris Luengo Dec 24 '20 at 22:02

1 Answers1

3

With numpy you can use view():

# Define your uint32 number
x = np.array(3249047552, dtype=np.uint32)
# Get the equivalent bitwise single number
x.view(np.single)
# output: -21.066406
obchardon
  • 10,614
  • 1
  • 17
  • 33