prec.py

Created by andreanx

Created on August 11, 2024

443 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.


from math import tan

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]
  
def precm2(b):
  n = 0
  dold = 0
  while 1:
   d = tan(355/226 + b**-n) - tan(355/226)
   if d == dold or d < 0:
     break
   dold = d
   n += 1
  return n

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.