https://github.com/orsinium-labs/tinymath
The fastest Go math library for constrained environments, like microcontrollers or WebAssembly.
https://github.com/orsinium-labs/tinymath
embedded gamedev go golang math tinygo trigonometry wasm webassembly
Last synced: 7 months ago
JSON representation
The fastest Go math library for constrained environments, like microcontrollers or WebAssembly.
- Host: GitHub
- URL: https://github.com/orsinium-labs/tinymath
- Owner: orsinium-labs
- License: mit
- Created: 2024-05-17T13:26:45.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-05-18T13:28:40.000Z (over 1 year ago)
- Last Synced: 2024-05-19T14:28:17.005Z (over 1 year ago)
- Topics: embedded, gamedev, go, golang, math, tinygo, trigonometry, wasm, webassembly
- Language: Go
- Homepage:
- Size: 61.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-tinygo - tinymath - The fastest and smallest Go math library for constrained environments, like microcontrollers or WebAssembly. (Embedded Systems / General use)
README
# 🧮 tinymath
[ [📚 docs](https://pkg.go.dev/github.com/orsinium-labs/tinymath) ] [ [🐙 github](https://github.com/orsinium-labs/tinymath) ]
The fastest Go math library for constrained environments, like microcontrollers or WebAssembly.
* Optimizes for performance and small code size at the cost of precision.
* Uses float32 because most microcontrollers (like [ESP32](https://en.wikipedia.org/wiki/ESP32)) have much faster computation for float32 than for float64.
* Designed and tested to work with both Go and [TinyGo](https://tinygo.org/), hence the name.
* Most algorithms are ported from [micromath](https://github.com/tarcieri/micromath) Rust library.
* Zero dependency.## 📦 Installation
```bash
go get github.com/orsinium-labs/tinymath
```## 🔧 Usage
```go
fmt.Println(tinymath.Sin(tinymath.Pi))
```## 🔬 Size
Here is a comparison of WebAssembly binary size (built with TinyGo) when using tinymath vs stdlib math:
| function | tinymath | stdlib | ratio |
| ------------ | --------:| ------:| -----:|
| atan | 106 | 367 | 28% |
| atan2 | 167 | 782 | 21% |
| exp | 463 | 2722 | 17% |
| fract | 166 | 154 | 107% |
| hypot | 67 | 203 | 33% |
| ln | 196 | 4892 | 4% |
| powf | 701 | 9167 | 7% |
| round | 129 | 171 | 75% |
| sin | 125 | 1237 | 10% |
| sqrt | 57 | 57 | 100% |
| tan | 138 | 1137 | 12% |
| trunc | 57 | 57 | 100% |To reproduce: `python3 size_bench.py`