There are two smallest floats, depending on your criteria. `min`

is the smallest normalized float. The smallest subnormal float is `min * epsilon`

.

>>> sys.float_info.min 2.2250738585072014e-308 >>> sys.float_info.min * sys.float_info.epsilon 5e-324

Note the distinction between normalized and subnormal floats: `min`

is not actually the smallest float, it's just the smallest one with full precision. Subnormal numbers cover the range between `0`

and `min`

, but they lose a lot of precision. Notice that `5e-324`

has only one significant digit. Subnormals are also much slower to work with, up to 100x slower than normalized floats.

>>> (sys.float_info.min * sys.float_info.epsilon) / 2 0.0 >>> 4e-324 5e-324 >>> 5e-325 0.0

Like every answer says, it's the difference between `1`

and the next greatest value that can be represented, if you tried to add half of it to 1, you'll get 1 back

>>> (1 + (sys.float_info.epsilon / 2)) == 1 True

Additionally if you try to add two thirds of it to `1`

, you'll get the same value:

>>> (1 + sys.float_info.epsilon) == (1 + (sys.float_info.epsilon * (2. / 3))) True

Some languages have types with a constant defined called Epsilon which represents the smallest positive float value that is greater than zero.,Often numerical code uses a constant known as machine epsilon, or the square root of machine epsilon, to compensate for the effects of rounding errors. Machine epsilon equals the smallest number such that (1.0 + machine epsilon) != 1.0.,Unlike in many other languages which have a special class (often called BigNumber) to handle very large integer values, Python 3 has built in support with the integer data type.,Most machines today use floating point arithmetic that conform to IEEE 754. They therefore allow the number zero to be positive (+0) or negative (-0). Although a test for equality on +0 and -0 will return true, they can lead to very different results when used in subsequent calculations, as demonstrated in the code sample below.

To illustrate this, see the code below, where we divide a number by zero.

import math import sys print(sys.version) if sys.version_info.major < 3: raise Exception('Python 3+ required') def positive_float_divide_by_zero(): "" " Test that dividing a positive floating point number by zero raises an exception in Python, as opposed to many languages which return positive infinity "" " numerator = 12.0 denominator = 0.0 print(type(numerator)) print(type(denominator)) try: result = numerator / denominator # in other languages such as Java or C #, this will equal + Infinity. except ZeroDivisionError: print('ZeroDivisionError raised') def negative_float_divide_by_zero(): "" " Test that dividing a negative floating point number by zero raises an exception in Python, as opposed to many languages which return negative infinity "" " numerator = -12.0 denominator = 0.0 try: result = numerator / denominator # in other languages such as Java or C #, this will equal - Infinity. except ZeroDivisionError: print('ZeroDivisionError raised') def zero_float_divide_by_zero(): "" " Test that dividing zero by zero using floating point numbers raises an exception in Python, as opposed to many other languages which will return NaN. "" " numerator = 0.0 denominator = 0.0 try: result = numerator / denominator # in other languages such as Java or C #, the result will be NaN except ZeroDivisionError: print('ZeroDivisionError raised') def positive_int_divide_by_zero(): "" " Test that dividing a positive integer by zero raises an exception, as it does by default in many other languages "" " numerator = 12 denominator = 0 print(type(numerator)) print(type(denominator)) try: result = numerator / denominator # in other languages such as Java or C #, this will also raise an exception(unless unchecked) except ZeroDivisionError: print('ZeroDivisionError raised')

This is less of an issue with Python, as it is with many other languages, as divide by zero is not allowed.

import numpy as np def signed_zero(): "" " Demonstrates signed zero. "" " pos_zero = +0.0 neg_zero = -0.0 # or 0.0 / -1.0 print(pos_zero) print(neg_zero) assert 0 == np.sign(neg_zero) assert pos_zero == neg_zero # a test for equality will return true # it is possible in Python to then get different values, although much harder than with the likes of Java and C # where floating point div by zero is allowed pos_val = math.copysign(3.0, pos_zero) neg_val = math.copysign(3.0, neg_zero) print(pos_val) print(neg_val) assert pos_val != neg_val # in some languages, such as Java and C #, the following asserts would both return true. # however, in Python we will get a ZeroDivisionError try: assert np.isposinf(1.0 / pos_zero) except ZeroDivisionError: print('ZeroDivisionError raised') try: assert np.isneginf(1.0 / neg_zero) except ZeroDivisionError: print('ZeroDivisionError raised')

Note that we can test for infinity using

math.isinf(), which will return true for both positive and negative infinity, or we can usenp.isposinf()ornp.isneginf()if the sign is important.

import numpy as np def infinite_values(): "" " Demonstrates how to generate infinite values. "" " assert np.isposinf(float('inf')) assert np.isneginf(float('-inf')) assert np.isposinf(1e250 * 1e250) assert np.isneginf(1e250 * -1e250) # In other languages such as Java and C #, you could also use the following # 1.0 / 0.0 // for positive infinity # - 1.0 / 0.0 // for negative infinity assert np.isposinf(sys.float_info.max * 2.0) import numpy as np def inf_properties(): "" " Demonstrates some properties of infinite values to be aware of . "" " pos_inf = float('inf') neg_inf = float('-inf') assert 0.0 != (pos_inf - pos_inf) assert 0.0 != (pos_inf + neg_inf) assert np.isnan(pos_inf / pos_inf)

Although out of scope of this article, it should be mentioned that in general programming, there are actually two types of NaN. The first is called a signalling NaN (SNaN or 1.#INF) denoting that the operation was invalid, and the second is a quiet NaN (QNaN or â€“1.#IND), denoting that the operation is undefined.

import numpy as np def nan_values(): "" " Demonstrate some different ways of generating Not a Number(NaN) "" " pos_inf = float('inf') assert np.isnan(float('nan')) # assert np.isnan(0.0 / 0.0) // this will raise a ZeroDivisionError in Python, but would be allowed in Java, C# and may other languages # assert np.isnan(math.sqrt(-1.0)) // this will raise a math domain error, but may return NaN in other languages assert np.isnan(pos_inf / pos_inf) assert np.isnan(pos_inf - pos_inf) def nan_equality(): "" " Demonstrate NaN equality and testing for NaN "" " x = float('nan') assert x != float('nan') assert np.isnan(x)

In particular, the relative error corresponding to .5 ulp can vary by a factor of . This factor is called the wobble. Setting = (/2)-p to the largest of the bounds in (2) above, we can say that when a real number is rounded to the closest floating-point number, the relative error is always bounded by e, which is referred to as machine epsilon. , This relative error is equal to 1 + 2 + 3 + 12 + 13 + 23 + 123, which is bounded by 5 + 82. In other words, the maximum relative error is about 5 rounding errors (since e is a small number, e2 is almost negligible). , This rounds to 102, compared with the correct answer of 101.41, for a relative error of .006, which is greater than = .005. In general, the relative error of the result can be only slightly larger than . More precisely, , When only the order of magnitude of rounding error is of interest, ulps and may be used interchangeably, since they differ by at most a factor of . For example, when a floating-point number is in error by n ulps, that means that the number of contaminated digits is log n. If the relative error in a computation is n, then

Last Updated : 31 May, 2022

**Syntax:**

`Number.EPSILON`

**Output:**

```
2.2204460492503130808472633361816E-16
false
true
```