prec.py

Created by andreanx

Created on April 10, 2019

260 Bytes

Check floats precision.

Syntax :

precm(b) for the mantissa precision where b is the base.

prece(b) for the exponent precision where b is the base.

Examples :

precm(2) for the maximum number of bits used for the mantissa

precm(10) for the maximum number of significative digits used for the mantissa

prece() for the minimum and maximum values for the exponent.


def precm(b):
  k,b=0,float(b)
  while 1+b**-k-1>0:
    k+=1
  return k

def prece():
  a=-1
  while 2.**a>0:
    a*=2
  while 2.**a==0:
    a+=1
  b=1
  while str(2.**b)[0:3]!='inf':
    b*=2
  while str(2.**b)[0:3]=='inf':
    b-=1
  return [a,b]