https://github.com/pocketpy/dmath
Deterministic Math Library for C11 Based on Musl
https://github.com/pocketpy/dmath
Last synced: about 1 year ago
JSON representation
Deterministic Math Library for C11 Based on Musl
- Host: GitHub
- URL: https://github.com/pocketpy/dmath
- Owner: pocketpy
- Created: 2025-05-22T17:19:45.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-05-23T03:34:09.000Z (about 1 year ago)
- Last Synced: 2025-05-23T04:28:52.462Z (about 1 year ago)
- Language: C
- Homepage:
- Size: 12.7 KB
- Stars: 2
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
Awesome Lists containing this project
README
# dmath
`dmath` is a deterministic math library for game projects that running on standard mobile (Android/iOS) and desktop platforms (Windows/Linux/MacOS).
`dmath` is a drop-in replacement for the math library in C11, based on [musl-libc v1.2.5](https://musl.libc.org/) with modifications to ensure that all floating-point operations are deterministic across different platforms and compilers. Although musl-libc is primarily designed for Linux, `dmath` can be used on Windows and MacOS.
[pocketpy](https://github.com/pocketpy/dmath) uses `dmath` as its math library when compiled with `-DPK_ENABLE_DETERMINISM=ON`.
> `dmath` is not designed for embedded systems.
## Installation
First clone this repo recursively:
```bash
git clone --recursive https://github.com/pocketpy/dmath
```
Then add the following line to your `CMakeLists.txt`:
```cmake
add_subdirectory()
target_link_libraries( dmath)
if(MSVC)
# Force multiple definitions to be allowed in MSVC.
target_link_options( PRIVATE /FORCE:MULTIPLE)
endif()
```
## Usage
```c
// CMakeLists.txt will redirect to "dmath/include/public/math.h".
// You don't need to modify your existing code.
#include
#include
int main(){
// You can check this macro to ensure that `dmath` is used.
#ifndef _DMATH_H
abort();
#endif
double x = log(10);
printf("log(10) = %f\n", x);
return 0;
}
```
## Tested Platforms
We have tested `dmath` on the following platforms and configurations.
| Job | OS | Arch | Compiler |
| ---------------------- | ------------ | ------- | -------- |
| build_ubuntu | Ubuntu 22.04 | x86_64 | gcc |
| | | | clang |
| build_ubuntu_multiarch | Ubuntu 22.04 | x86 | gcc |
| | | | clang |
| | | aarch64 | gcc |
| | | | clang |
| | | armv7 | gcc |
| | | | clang |
| build_macos | MacOS 14 | x86_64 | clang |
| build_win32 | Windows 2022 | x86_64 | cl |
## References
https://box2d.org/posts/2024/08/determinism/
## License
MIT License