paupino / rust-decimal

Decimal number implementation written in pure Rust suitable for financial and fixed-precision calculations.
https://docs.rs/rust_decimal/
MIT License
1.02k stars 183 forks source link

cosine causes stack overflow for this value #584

Closed normano closed 1 year ago

normano commented 1 year ago

I've replicated this stack overflow with this value: 8.639379797371931405772269304. Sin() also overflows, but I'm not sure which value it will overflow for since I haven't printed it out.

println!"{}", rust_decimal_macros::dec!(8.639379797371931405772269304).checked_cos());

paupino commented 1 year ago

Thanks for raising an issue! This seems to be related to pi/4 sending it into a loop.

For this number:

x = 8.639379797371931405772269304
cos(x) 
= cos(x - 2pi)
// = 8.639379797371931405772269304 - 6.2831853071795864769252867666 = 2.3561944901923449288469825374
= sin(pi/2 - (x - 2pi))
// = 1.5707963267948966192313216916 - 2.3561944901923449288469825374 = -0.7853981633974483096156608458

Currently in Rust Decimal QUARTER_PI is set to 0.7853981633974483096156608458 which is the same figure.

Further information on the issue in the linked PR.