Crates.io | deterministic-trigonometry |
lib.rs | deterministic-trigonometry |
version | 0.1.1 |
source | src |
created_at | 2024-07-21 16:11:35.124633 |
updated_at | 2024-07-22 13:36:45.339774 |
description | Trigonometry without floating point variables to achieve deterministic results across hardware and compilers |
homepage | |
repository | https://github.com/james-simkins-pittsburgh/deterministic-trigonometry |
max_upload_size | |
id | 1310418 |
size | 506,540 |
This library provides basic trigonometry functions without using any floating point arithmetic.
This library is intended to be useful for games that use lockstep determinism and therefore wish to avoid the indeterminism that comes with using floating point arithmetic across different hardware or compilers. This library avoids these tiny inconsistencies by using only integer data types internally. Therefore, this library should produce exactly reproducible results regardless of the compiler or hardware used. However, this comes at the cost of imprecision because of compounded rounding errors. (Note: This imprecision is still 100% consistent and reproducible so it will not break determinism.)
Trigonometry is accomplished by using pre-baked tables of trigonometry results that are written into the code itself and then written into memory with an initialize() function. This library supports sine, cosine, tangent, arcsine, arccosine, and arctangent.
Provide input as a (i32, i32) tuple corresponding to the numerator and denominator of the input represented as a fraction. All angle measurements are in radians. Output is returned as a tuple (i32,i32) representing a fractional output. The output denominator is always 1000 to allow easy conversion to fixed point decimals.
cargo add deterministic-trigonometry
use deterministic_trigonometry::DTrig;
fn main (){
let d_trig = DTrig::initialize();
let arctangent_of_one_half = d_trig.arctangent((500,1000));
println!("The arctangent of 500/1000 radians is {}/{}.", arctangent_of_one_half.0, arctangent_of_one_half.1);
}
For a more complex example, see the examples folder in the source code.
The library has an initialize() function that must be run when the DTrig struct is instantiated and the six trigonometry and inverse trigonometry functions:
let d_trig = DTrig::initialize();
let sine_of_one_half = d.trig.sine((500,1000));
let cosine_of_one_half = d.trig.cosine((500,1000));
let tangent_of_one_half = d.trig.tangent((500,1000));
let arcsine_of_one_half = d.trig.arcsine((500,1000));
let arccosine_of_one_half = d.trig.arccosine((500,1000));
let arctangent_of_one_half = d.trig.arctangent((500,1000));
Licensed under either of
at your option.
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.