### 3 releases

0.1.2 | Mar 29, 2023 |
---|---|

0.1.1 | Jan 17, 2023 |

0.1.0 | Jan 17, 2023 |

#**253** in Math

**MIT/Apache**

57KB

1K
SLoC

# Fixed Math

This library implements analytic/trigonometric functions for fixed point numbers.

Implemented functions:

from trait`sqrt``FixedSqrt`

is not implemented for fixed numbers with less than 2 integer bits, but there are functions that work on those types:`FixedSqrt`

, ...`sqrt_i1`

,`sin_cos`

,`sin`

,`cos`

from trait`tan``FixedSinCos`

is not implemented for fixed numbers with less than 7 integer bits, but there are functions that work on some of those types:`FixedSinCos`

, ...`sin_cos_i7`- All calculations are made in degrees
- except that there is a

function which is very imprecise; check source code for why, feel free to fix it (its not a priority for me)`sin_cos_rad`

- except that there is a

## Examples

There are traits and standalone functions, see examples on how to use them.

### Errors

Check the examples to see about how much error this implementation produces.

Usually

has an error of around 1-2 Delta.`sqrt`

(Delta = the distance to the next representable number)

may produce bigger errors, around 1-2 decimal places.`sin_cos`

## Benchmarks

You can check or run the benchmarks in

.`benches`

Here are some conclusions I've got to:

### SinCos

*2022-09-28*

Calculation time for sin_cos varies with the fixed number's byte size.

- I10F6: ~ 8ns
- I16F16: ~ 9ns
- I32F32: ~ 18ns
- I32F96: ~ 210ns

Notes:

- there are many different int bit / frac bit combinations; I did not test them

(int bits must be >= 10 (but maybe I can do something to relax that further)) - these are all calculations in degrees
- code was compiled with native cpu features
- go for FixedI32 instead of FixedI16 unless you are limited by memory much
- I did a benchmark in the same style on

's`cordic`

on FixedI64`sin_cos`- keep in mind that

works with radians, I used the same angle values`cordic` - so they can take

of a lot bigger angle on the same number representation size`sin_cos` - this crate was about 1.5-2 times faster on same angle sizes

- keep in mind that

## License

All code in this repository is dual-licensed under either:

- MIT License (LICENSE-MIT or http://opensource.org/licenses/MIT)
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)

at your option.

We use code modified from cordic, licensed as BSD-3-Clause:

## Contributing

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

#### Dependencies

~2MB

~42K SLoC