machine_eps.py

Created by vnap0v

Created on December 25, 2025

555 Bytes

Determine machine epsilon through test

Machine epsilon is smallest value such that (1.0 + epsilon) is still distinguishable from 1.0 by the system

In CPython implementations this value can be found directly using

import sys
print(sys.float_info.epsilon)

However the micropython of Numworks does not have a sys module


# Determine machine epsilon through test
#
# epsilon is smallest value such that (1.0 + epsilon) is still distinguishable from 1.0
# by the system
eps=1.0
x=1.0
exponent=0
while True: # loop until half_eps is too small to change value of x
  half_eps = eps / 2.0
  if x == x + half_eps:
    break
  eps = half_eps
  exponent-=1
print("Determine machine epsilon\nthrough test")
print("epsilon\n= {0}\n= 2**({1:.0f})".format(eps,exponent))
print("1.0 + eps\n= {0:.20f}".format(x+eps))
print("1.0 + eps/2\n= {0:.20f}".format(x+half_eps))

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.