# Floating-point pitfalls¶

## Precision and accuracy¶

For some operations, Numba may use a different algorithm than Python or Numpy. The results may not be bit-by-bit compatible. The difference should generally be small and within reasonable expectations. However, small accumulated differences might produce large differences at the end, especially if a divergent function is involved.

### Math library implementations¶

Numba supports a variety of platforms and operating systems, each of which has its own math library implementation (referred to as `libm` from here in). The majority of math functions included in `libm` have specific requirements as set out by the IEEE 754 standard (like `sin()`, `exp()` etc.), but each implementation may have bugs. Thus, on some platforms Numba has to exercise special care in order to workaround known `libm` issues.

Another typical problem is when an operating system’s `libm` function set is incomplete and needs to be supplemented by additional functions. These are provided with reference to the IEEE 754 and C99 standards and are often implemented in Numba in a manner similar to equivalent CPython functions.

### Linear algebra¶

Numpy forces some linear algebra operations to run in double-precision mode even when a `float32` input is given. Numba will always observe the input’s precision, and invoke single-precision linear algebra routines when all inputs are `float32` or `complex64`.

The implementations of the `numpy.linalg` routines in Numba only support the floating point types that are used in the LAPACK functions that provide the underlying core functionality. As a result only `float32`, `float64`, `complex64` and `complex128` types are supported. If a user has e.g. an `int32` type, an appropriate type conversion must be performed to a floating point type prior to its use in these routines. The reason for this decision is to essentially avoid having to replicate type conversion choices made in Numpy and to also encourage the user to choose the optimal floating point type for the operation they are undertaking.

### Mixed-types operations¶

Numpy will most often return a `float64` as a result of a computation with mixed integer and floating-point operands (a typical example is the power operator `**`). Numba by contrast will select the highest precision amongst the floating-point operands, so for example `float32 ** int32` will return a `float32`, regardless of the input values. This makes performance characteristics easier to predict, but you should explicitly cast the input to `float64` if you need the extra precision.

## Warnings and errors¶

When calling a ufunc created with `vectorize()`, Numpy will determine whether an error occurred by examining the FPU error word. It may then print out a warning or raise an exception (such as `RuntimeWarning: divide by zero encountered`), depending on the current error handling settings.

Depending on how LLVM optimized the ufunc’s code, however, some spurious warnings or errors may appear. If you get caught by this issue, we recommend you call `numpy.seterr()` to change Numpy’s error handling settings, or the `numpy.errstate` context manager to switch them temporarily:

```with np.errstate(all='ignore'):
x = my_ufunc(y)
```
{"ft_lang_label":"__label__en","ft_lang_prob":0.86733174,"math_prob":0.90697706,"size":3178,"snap":"2022-27-2022-33","text_gpt3_token_len":679,"char_repetition_ratio":0.10302457,"word_repetition_ratio":0.0,"special_character_ratio":0.19414726,"punctuation_ratio":0.09123435,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95178133,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-06-29T22:19:07Z\",\"WARC-Record-ID\":\"<urn:uuid:88846d87-aff7-407a-9755-744b250978d5>\",\"Content-Length\":\"126820\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:1e89ebc9-56d9-45ca-9793-322ffa4a23b6>\",\"WARC-Concurrent-To\":\"<urn:uuid:b38f14ef-6037-4fcb-b514-088284f9f321>\",\"WARC-IP-Address\":\"104.17.33.82\",\"WARC-Target-URI\":\"https://numba.readthedocs.io/en/0.55.1/reference/fpsemantics.html\",\"WARC-Payload-Digest\":\"sha1:FCW2G6WA6SIKFJIW4LB2VXN23C2UBMZX\",\"WARC-Block-Digest\":\"sha1:CWJ3OGHJBWXX3PAQFLBDV2UJY3GP4XMK\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-27/CC-MAIN-2022-27_segments_1656103645173.39_warc_CC-MAIN-20220629211420-20220630001420-00730.warc.gz\"}"}