ieee 754 conversion function
Has some bugs
// Some code
# 'dataset' holds the input data for this script
import pandas as pd
def ieee_754_conversion(n, sgn_len=1, exp_len=8, mant_len=23):
if n >= 2 ** (sgn_len + exp_len + mant_len):
raise ValueError("Number n is longer than prescribed parameters allows")
sign = (n & (2 ** sgn_len - 1) * (2 ** (exp_len + mant_len))) >> (exp_len + mant_len)
exponent_raw = (n & ((2 ** exp_len - 1) * (2 ** mant_len))) >> mant_len
mantissa = n & (2 ** mant_len - 1)
sign_mult = 1
if sign == 1:
sign_mult = -1
if exponent_raw == 2 ** exp_len - 1: # Could be Inf or NaN
if mantissa == 2 ** mant_len - 1:
return float('nan') # NaN
return sign_mult * float('inf') # Inf
exponent = exponent_raw - (2 ** (exp_len - 1) - 1)
if exponent_raw == 0:
mant_mult = 0 # Gradual Underflow
else:
mant_mult = 1
for b in range(mant_len - 1, -1, -1):
if mantissa & (2 ** b):
mant_mult += 1 / (2 ** (mant_len - b))
return sign_mult * (2 ** exponent) * mant_mult
## DEC = high * 65536 + low
for i in range(0, len(dataset)):
dataset.loc[i,'HumidityValue'] = ieee_754_conversion(dataset.loc[i,'HumidityDEC'], exp_len=8, mant_len=23)
Last updated