pyodide / pyodide/pyodide

numpy.longdouble (emscripten `long double`) not actually handled by most math library functions

Open
#4,881 2 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Python
Stars
14.8k
Forks
1k
Avg merge
20h 29m
Merged PRs (30d)
19

Description

## 🐛 Bug

You might be aware of this, but I only recently started experimenting with emscripten and pyodide, and I ran into an issue with the `long double` type. In emscripten, `long double` is IEEE float128, but its math library is based on musl, and musl does not implement float128 algorithms for many of its functions. It looks like the C functions `sinl`, `cosl` and `tanl` handle float128, but for `logl`, `log1pl`, `expl`, `expm1l`, `powl` and others, if it detects float128 (i.e. `LDBL_MANT_DIG == 113`), it downcasts and uses its `double` implementation (silently, with no warning).

For example, `np.exp(np.longdouble(1))` should be `2.71828182845904523536028747135266`, but in pyodide, we get

```
>>> np.exp(np.longdouble(1))
2.718281828459045090795598298427649
```

The following shows that the above is equivalent to doing the calculation with double precision instead of float128:

```
>>> np.longdouble(np.exp(1))
2.718281828459045090795598298427649
```

This is a surprising "gotcha" for the folks who try to use `np.longdouble` in pyodide.

Related emscripten issue: https://github.com/emscripten-core/emscripten/issues/22123

### Environment

- Pyodide Version: whatever is currently running at https://pyodide.org/en/stable/console.html
- Browser version: Firefox 126.0

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.