Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Multiple problems with calculations and number representation in Python

I’m trying to create a python code, which solves an equation, converts the result to a 16-bit binary representation and slipts the result into two same size strings. What i’ve made so far

import math

if __name__ == '__main__':


  Vref = 3
  offsetCode = 5461
  M = 65535
  C= 32768
  Vout=5
  byte1=0b11001000
  
  dacCode=math.floor(((Vout*(2^14)/Vref)+4*offsetCode))
  inputCode=math.floor((dacCode-C+(2^15))*(2^16)/(M+1))
  print(inputCode)
  inputCodeBin=f'0b{inputCode:16b}'
  print(dacCode)
  print(inputCodeBin)
  halfway = int(len(inputCodeBin)/2)
  byte2=inputCodeBin[:halfway]
  byte3=inputCodeBin[halfway:]
  print(byte2)
  print(byte3)

What i get as a result is -3, 21864, 0b -11, 0b -11 , meaning that python displays the wrong result and the split does not work. I’ve solved this equation on papper and the result should be 49151. I’ve also computed it on matlab and i get the correct result.
My questions are the following:
1)I believe that integers with value less than 2147483647 whould not cause an overflow.Why does an overflow actually happen here?
2)What’s the correct way to repressent the result as a 16-bit binary string and split it in half ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

^ is not the power operator, it is the bitwise xor — use ** instead for powers

dacCode=math.floor(((Vout*(2**14)/Vref)+4*offsetCode))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading