Home > other >  Multiple problems with calculations and number representation in Python
Multiple problems with calculations and number representation in Python

Time:02-01

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 ?

CodePudding user response:

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

dacCode=math.floor(((Vout*(2**14)/Vref) 4*offsetCode))
  • Related