1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
#![warn(missing_docs)]
//! Deterministic trigonometry across architectures without using floating point arithmetic.
//!
//! - Uses (i32, i32) tuples to represent fractions.
//! - Uses pre-baked arrays for trigonometry results.
//! - Deterministic across compilers and computer architectures.
//! - Introduces imprecision due to rounding errors.
//! - Most likely to be useful for games that depend on lockstep determinism.
//!
//! # Example
//!
//! ```
//! use deterministic_trigonometry::DTrig;
//!
//! fn main (){
//!
//! let d_trig = DTrig::initialize();
//!
//! let sine_of_pi_over_three = d_trig.sine((1047,1000));
//!
//! println!("The sine of 1047/1000 radians is {}/{}.", sine_of_pi_over_three.0, sine_of_pi_over_three.1);
//!
//! }
//! ```
/// Main struct through which trig functions are implemented.
///
/// Once this struct is initialized, it holds arrays with pre-baked trig functions.
/// Trig functions are called as methods with the input as (i32 , i32) tuples with
/// the first i32 representing the numerator an the second i32 representing the denominator.
///
/// The output is also a (i32 , i32) tuple with the first i32 representing the numerator
/// and the second i32 representing the denominator. The output denominator will always be 1000.
///
/// # Example
///
/// ```
/// use deterministic_trigonometry::DTrig;
///
/// fn main (){
///
/// let d_trig = DTrig::initialize();
///
/// let sine_of_pi_over_four = d_trig.sine((785,1000));
///
/// println!("The sine of 785/1000 radians is {}/{}.", sine_of_pi_over_four.0, sine_of_pi_over_four.1);
///
/// }
/// ```
// Array sizes are set to balance accuracy with memory usage.
pub struct DTrig {
sine_array: [i16; 6283],
cosine_array: [i16; 6283],
tangent_array: [i32; 6283],
arcsine_array: [i16; 2001],
arccosine_array: [i16; 2001],
arctangent_thousandths: [i16; 8001],
arctangent_hundredths: [i16; 4001],
arctangent_tenths: [i16; 2001],
arctangent_ones: [i16; 2001],
}
/// This module contains the code that sets the values for the arrays from the pre-baked tables.
pub mod initialize;
/// This module contains utility functions.
pub (self) mod utility;
// These functions pull the appropriate results out of the arrays.
impl DTrig {
/// Calculates the sine of an angle in radians.
///
/// - The input tuple represents the angle as a numerator and denominator.
/// - The output tuple represents the sine result as a numerator and denominator.
/// - Most accurate between 0 and 2 PI with a factor of 1000 as denominator.
/// - See README for limitations on accuracy.
///
/// # Panics
///
/// - A zero as the input for the denominator.
///
/// # Example
///
/// ```
/// use deterministic_trigonometry::DTrig;
///
/// fn main (){
///
/// let d_trig = DTrig::initialize();
///
/// let sine_of_pi_over_four = d_trig.sine((785,1000));
///
/// println!("The sine of 785/1000 radians is {}/{}.", sine_of_pi_over_four.0, sine_of_pi_over_four.1);
///
/// }
/// ```
pub fn sine(&self, argument_fraction: (i32, i32)) -> (i32, i32) {
return (
i32::from(
self.sine_array
[
utility::normalize_angle(
utility::denominator_to_1000(argument_fraction)
) as usize
]
),
1000,
);
}
/// Calculates the cosine of an angle in radians.
///
/// - The input tuple represents the input angle as a numerator and denominator.
/// - The output tuple represents the cosine result as a numerator and denominator.
/// - Most accurate between 0 and 2 PI with a factor of 1000 as denominator.
/// - See README for limitations on accuracy.
///
/// # Panics
///
/// - A zero as the input for the denominator.
///
/// # Example
///
/// ```
/// use deterministic_trigonometry::DTrig;
///
/// fn main (){
///
/// let d_trig = DTrig::initialize();
///
/// let cosine_of_pi_over_four = d_trig.cosine((785,1000));
///
/// println!("The cosine of 785/1000 radians is {}/{}.", cosine_of_pi_over_four.0, cosine_of_pi_over_four.1);
///
/// }
///
/// ```
pub fn cosine(&self, argument_fraction: (i32, i32)) -> (i32, i32) {
return (
i32::from(
self.cosine_array
[
utility::normalize_angle(
utility::denominator_to_1000(argument_fraction)
) as usize
]
),
1000,
);
}
/// Calculates the tangent of an angle in radians.
///
/// - The input tuple represents the input angle as a numerator and denominator.
/// - The output tuple represents the tangent result as a numerator and denominator.
/// - Most accurate between 0 and 2 PI with a factor of 1000 as denominator.
/// - Can have large errors around asymptote lines for the tangent function.
/// - See README for limitations on accuracy.
///
/// # Panics
///
/// - A zero as the input for the denominator.
///
/// # Example
///
/// ```
/// use deterministic_trigonometry::DTrig;
///
/// fn main (){
///
/// let d_trig = DTrig::initialize();
///
/// let tangent_of_pi_over_four = d_trig.tangent((785,1000));
///
/// println!("The tangent of 785/1000 radians is {}/{}.", tangent_of_pi_over_four.0, tangent_of_pi_over_four.1);
///
/// }
/// ```
pub fn tangent(&self, argument_fraction: (i32, i32)) -> (i32, i32) {
return (
self.tangent_array
[
utility::normalize_angle(
utility::denominator_to_1000(argument_fraction)
) as usize
],
1000,
);
}
/// Performs arcsine on a value to produce the measure of the corresponding angle in radians.
///
/// - The input tuple represents the input value as a numerator and denominator.
/// - The output tuple represents the angle result in radians as a numerator and denominator.
/// - Most accurate with a factor of 1000 as denominator.
/// - See README for detailed limitations on accuracy.
///
/// # Panics
///
/// - A zero as the input for the denominator.
/// - Inputs representing a fractions with a value greater than 1 or less than -1.
/// - This is out of the mathematically defined denominator the arcsine function.
///
/// # Example
///
/// ```
/// use deterministic_trigonometry::DTrig;
///
/// fn main (){
///
/// let d_trig = DTrig::initialize();
///
/// let arcsine_of_one_half = d_trig.arcsine((500,1000));
///
/// println!("The arcsine of 500/1000 radians is {}/{}.", arcsine_of_one_half.0, arcsine_of_one_half.1);
///
/// }
/// ```
pub fn arcsine(&self, argument_fraction: (i32, i32)) -> (i32, i32) {
if utility::denominator_to_1000(argument_fraction) < -1000 {
panic!("Arcsine input less than 1.");
} else if utility::denominator_to_1000(argument_fraction) > 1000 {
panic!("Arcsine input greater than 1.");
} else {
return (
i32::from(
self.arcsine_array
[(utility::denominator_to_1000(argument_fraction) + 1000) as usize]
),
1000,
);
}
}
/// Performs arccosine on a value to produce the measure of the corresponding angle in radians
///
/// - The input tuple represents the input value as a numerator and denominator.
/// - The output tuple represents the angle result in radians as a numerator and denominator.
/// - Most accurate with a factor of 1000 as denominator.
/// - See README for detailed limitations on accuracy.
///
/// # Panics
///
/// - A zero as the input for the denominator.
/// - Inputs representing a fractions with a value greater than 1 or less than -1.
/// - This is out of the mathematically defined domain for the arccosine function.
///
/// # Example
///
/// ```
/// use deterministic_trigonometry::DTrig;
///
/// fn main (){
///
/// let d_trig = DTrig::initialize();
///
/// let arccosine_of_one_half = d_trig.arccosine((500,1000));
///
/// println!("The arccosine of 500/1000 radians is {}/{}.", arccosine_of_one_half.0, arccosine_of_one_half.1);
///
/// }
/// ```
pub fn arccosine(&self, argument_fraction: (i32, i32)) -> (i32, i32) {
if utility::denominator_to_1000(argument_fraction) < -1000 {
panic!("Arccosine input less than 1, which is undefined.");
} else if utility::denominator_to_1000(argument_fraction) > 1000 {
panic!("Arccosine input greater than 1, which is undefined.");
} else {
return (
i32::from(
self.arccosine_array
[(utility::denominator_to_1000(argument_fraction) + 1000) as usize]
),
1000,
);
}
}
/// Performs arctangent on a value to produce the measure of the corresponding angle in radians
///
/// - The input tuple represents the input value as a numerator and denominator.
/// - The output tuple represents the angle result in radians as a numerator and denominator.
/// - Most accurate with a factor of 1000 as denominator.
/// - See README for detailed limitations on accuracy.
///
/// # Panics
///
/// - A zero as the input for the denominator.
///
/// # Example
///
/// ```
/// 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);
///
/// }
///
/// ```
pub fn arctangent(&self, argument_fraction: (i32, i32)) -> (i32, i32) {
// Converts the numerator to what it would be out of 1000.
let numerator_out_of_1000 = utility::denominator_to_1000(argument_fraction);
if numerator_out_of_1000 >= -4000 && numerator_out_of_1000 <= 4000 {
// Handles from -4 to 4.
return (
i32::from(self.arctangent_thousandths[(numerator_out_of_1000 + 4000) as usize]),
1000,
);
} else if numerator_out_of_1000 >= -20000 && numerator_out_of_1000 <= 20000 {
// Handles from -20 to 20.
if (numerator_out_of_1000 % 10).abs() < 5 {
return (
i32::from(
self.arctangent_hundredths[(numerator_out_of_1000 / 10 + 2000) as usize]
),
1000,
);
} else {
if numerator_out_of_1000 > 0 {
return (
i32::from(
self.arctangent_hundredths
[(numerator_out_of_1000 / 10 + 1 + 2000) as usize]
),
1000,
);
} else {
return (
i32::from(
self.arctangent_hundredths
[(numerator_out_of_1000 / 10 - 1 + 2000) as usize]
),
1000,
);
}
}
} else if numerator_out_of_1000 >= -100000 && numerator_out_of_1000 <= 100000 {
// Handles from -100 to 100
if (numerator_out_of_1000 % 100).abs() < 50 {
return (
i32::from(
self.arctangent_tenths[(numerator_out_of_1000 / 100 + 1000) as usize]
),
1000,
);
} else {
if numerator_out_of_1000 > 0 {
return (
i32::from(
self.arctangent_tenths
[(numerator_out_of_1000 / 100 + 1 + 1000) as usize]
),
1000,
);
} else {
return (
i32::from(
self.arctangent_tenths
[(numerator_out_of_1000 / 100 - 1 + 1000) as usize]
),
1000,
);
}
}
} else if numerator_out_of_1000 >= -1000000 && numerator_out_of_1000 <= 1000000 {
// Handles from -1000 to 1000.
if numerator_out_of_1000 % 1000 < 500 {
return (
i32::from(self.arctangent_ones[(numerator_out_of_1000 / 1000 + 1000) as usize]),
1000,
);
} else {
if numerator_out_of_1000 > 0 {
return (
i32::from(
self.arctangent_ones[(numerator_out_of_1000 / 1000 + 1 + 1000) as usize]
),
1000,
);
} else {
return (
i32::from(
self.arctangent_ones[(numerator_out_of_1000 / 1000 - 1 + 1000) as usize]
),
1000,
);
}
}
} else {
// Handles lower than -1000 and higher than 1000.
if numerator_out_of_1000 < -1000000 && numerator_out_of_1000 > -3374653 {
return (-1570, 1000);
} else if numerator_out_of_1000 > 1000000 && numerator_out_of_1000 < 3374653 {
return (1570, 1000);
} else if numerator_out_of_1000 <= -3374653 {
return (-1571, 1000);
} else {
return (1571, 1000);
}
}
}
}
#[cfg(test)]
mod tests {
use crate::DTrig;
#[test]
fn test_sine() {
let dtrig = DTrig::initialize();
let mut result: bool;
for a in 0..6283 {
if ((((a as f64) / 1000.0).sin() * 1000.0).round() as i32) == dtrig.sine((a, 1000)).0 {
result = true;
} else {
result = false;
}
assert_eq!(result, true);
}
for a in -1000000000..1000000001 {
if
(
((((a as f64) / 1000.0).sin() * 1000.0).round() as i32) -
dtrig.sine((a, 1000)).0
).abs() <= 1
{
result = true;
} else {
result = false;
}
assert_eq!(result, true);
}
}
#[test]
fn test_cosine() {
let dtrig = DTrig::initialize();
let mut result: bool;
for a in 0..6283 {
if ((((a as f64) / 1000.0).cos() * 1000.0).round() as i32) == dtrig.cosine((a, 1000)).0 {
result = true;
} else {
result = false;
}
assert_eq!(result, true);
}
for a in -1000000000..1000000001 {
if
(
((((a as f64) / 1000.0).cos() * 1000.0).round() as i32) -
dtrig.cosine((a, 1000)).0
).abs() <= 1
{
result = true;
} else {
result = false;
}
assert_eq!(result, true);
}
}
#[test]
fn test_tangent() {
let dtrig = DTrig::initialize();
let mut result: bool;
for a in 0..6283 {
if
((((a as f64) / 1000.0).tan() * 1000.0).round() as i32) ==
dtrig.tangent((a, 1000)).0
{
result = true;
} else {
result = false;
}
assert_eq!(result, true);
}
for a in -1000000000..1000000001 {
if
// Off by no more than .01.
((((a as f64) / 1000.0).tan() * 1000.0).round() as i64) -
(dtrig.tangent((a, 1000)).0 as i64) <= 1 ||
// Or off by no more than 2%.
(
(((a as f64) / 1000.0).tan() * 1000.0 - (dtrig.tangent((a, 1000)).0 as f64)) /
(((a as f64) / 1000.0).tan() * 1000.0)
).abs() <= 0.02 ||
// Or if greater than 10000 off by no more than 10%.
(((a as f64) / 1000.0).tan().abs() * 1000.0 > 10000.0 &&
(
(((a as f64) / 1000.0).tan() * 1000.0 -
(dtrig.tangent((a, 1000)).0 as f64)) /
(((a as f64) / 1000.0).tan() * 1000.0)
).abs() <= 0.1) ||
// Or if greater than 100000 just ignore it.
(((a as f64) / 1000.0).tan() * 1000.0).abs() > 100000.0
{
result = true;
} else {
result = false;
}
assert_eq!(result, true);
}
}
#[test]
fn test_arcsine() {
let dtrig = DTrig::initialize();
let mut result: bool;
for a in -1000..1001 {
if
((((a as f64) / 1000.0).asin() * 1000.0).round() as i32) ==
dtrig.arcsine((a, 1000)).0
{
result = true;
} else {
result = false;
}
assert_eq!(result, true);
}
}
#[test]
fn test_arccosine() {
let dtrig = DTrig::initialize();
let mut result: bool;
for a in -1000..1001 {
if
((((a as f64) / 1000.0).acos() * 1000.0).round() as i32) ==
dtrig.arccosine((a, 1000)).0
{
result = true;
} else {
result = false;
}
assert_eq!(result, true);
}
}
#[test]
fn test_arctangent() {
let dtrig = DTrig::initialize();
let mut result: bool;
for a in -2000..2001 {
if
((((a as f64) / 1000.0).atan() * 1000.0).round() as i32) ==
dtrig.arctangent((a, 1000)).0
{
result = true;
} else {
result = false;
}
assert_eq!(result, true);
}
for a in -10000000..10000001 {
if
((((a as f64) / 1000.0).atan() * 1000.0).round() as i32) -
dtrig.arctangent((a, 1000)).0 <= 1
{
result = true;
} else {
result = false;
}
assert_eq!(result, true);
}
for a in -10000..10001 {
for b in 1..10001 {
if
((((a as f64) / (b as f64)).atan() * 1000.0).round() as i32) -
dtrig.arctangent((a, b)).0 <= 1
{
result = true;
} else {
result = false;
}
assert_eq!(result, true);
}
}
}
}