From 71b2f0c9a5a4f9913daeb4d9b5f50da6d95a0cbc Mon Sep 17 00:00:00 2001 From: tekkac Date: Sun, 14 Apr 2024 15:16:28 +0200 Subject: [PATCH 1/3] =?UTF-8?q?=F0=9F=90=9B=20fix=20sin=20tables=20bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/f128/math/lut.cairo | 6 +++--- src/f64/math/lut.cairo | 6 +++--- src/math/lut.cairo | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/f128/math/lut.cairo b/src/f128/math/lut.cairo index 5cf2a25..a1d23fe 100644 --- a/src/f128/math/lut.cairo +++ b/src/f128/math/lut.cairo @@ -917,12 +917,12 @@ fn sin(a: u128) -> (u128, u128, u128) { if slot == 159 { return (17996860841160351744, 15274735031373064192, 15337907143211554816); } - if slot == 160 { - return (18110048645192806400, 15337907143211554816, 15400501791515123712); - } } } else { if slot < 176 { + if slot == 160 { + return (18110048645192806400, 15337907143211554816, 15400501791515123712); + } if slot == 161 { return (18223236449225261056, 15400501791515123712, 15462516619630641152); } diff --git a/src/f64/math/lut.cairo b/src/f64/math/lut.cairo index c3facc1..4c24f3e 100644 --- a/src/f64/math/lut.cairo +++ b/src/f64/math/lut.cairo @@ -719,12 +719,12 @@ fn sin(a: u64) -> (u64, u64, u64) { if slot == 159 { return (4190220693, 3556426389, 3571134792); } - if slot == 160 { - return (4216574283, 3571134792, 3585708745); - } } } else { if slot < 176 { + if slot == 160 { + return (4216574283, 3571134792, 3585708745); + } if slot == 161 { return (4242927872, 3585708745, 3600147697); } diff --git a/src/math/lut.cairo b/src/math/lut.cairo index 5cf2a25..a1d23fe 100644 --- a/src/math/lut.cairo +++ b/src/math/lut.cairo @@ -917,12 +917,12 @@ fn sin(a: u128) -> (u128, u128, u128) { if slot == 159 { return (17996860841160351744, 15274735031373064192, 15337907143211554816); } - if slot == 160 { - return (18110048645192806400, 15337907143211554816, 15400501791515123712); - } } } else { if slot < 176 { + if slot == 160 { + return (18110048645192806400, 15337907143211554816, 15400501791515123712); + } if slot == 161 { return (18223236449225261056, 15400501791515123712, 15462516619630641152); } From 57aaeb7ff22d99200eb1bb87203514b1d046a97f Mon Sep 17 00:00:00 2001 From: tekkac Date: Sun, 14 Apr 2024 15:20:28 +0200 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=90=9B=20fix=20sub=5Foverflow=20in=20?= =?UTF-8?q?computation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/f128/math/trig.cairo | 2 +- src/f64/math/trig.cairo | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/f128/math/trig.cairo b/src/f128/math/trig.cairo index dddfcd6..426421e 100644 --- a/src/f128/math/trig.cairo +++ b/src/f128/math/trig.cairo @@ -169,7 +169,7 @@ fn sin_fast(a: Fixed) -> Fixed { } let (start, low, high) = lut::sin(partial_rem); - let partial_step = FixedTrait::new(partial_rem - start, false) + let partial_step = (FixedTrait::new(partial_rem, false) - FixedTrait::new(start, false)) / FixedTrait::new(113187804032455040, false); let res = partial_step * (FixedTrait::new(high, false) - FixedTrait::new(low, false)) + FixedTrait::new(low, false); diff --git a/src/f64/math/trig.cairo b/src/f64/math/trig.cairo index 1f5dc3d..e2efb43 100644 --- a/src/f64/math/trig.cairo +++ b/src/f64/math/trig.cairo @@ -163,7 +163,7 @@ fn sin_fast(a: Fixed) -> Fixed { } let (start, low, high) = lut::sin(partial_rem); - let partial_step = FixedTrait::new(partial_rem - start, false) + let partial_step = (FixedTrait::new(partial_rem, false) - FixedTrait::new(start, false)) / FixedTrait::new(26353589, false); let res = partial_step * (FixedTrait::new(high, false) - FixedTrait::new(low, false)) + FixedTrait::new(low, false); From 484b2a5fed970c8d74acbe87211f183b8ee05b35 Mon Sep 17 00:00:00 2001 From: tekkac Date: Sun, 14 Apr 2024 15:20:58 +0200 Subject: [PATCH 3/3] =?UTF-8?q?=E2=9C=85=20add=20sin=20comparison=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/f128/math/trig.cairo | 21 +++++++++++++++++++++ src/f64/math/trig.cairo | 22 ++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/src/f128/math/trig.cairo b/src/f128/math/trig.cairo index 426421e..6a20ae0 100644 --- a/src/f128/math/trig.cairo +++ b/src/f128/math/trig.cairo @@ -439,6 +439,27 @@ mod tests { ); // 0.9613974918793389 } + #[test] + #[available_gas(90_000_000_000)] + fn test_compare_sin() { + let error = Option::Some(184467440737095); + + let MAX: u128 = 256 * 4; + let mut n: u128 = 0; + loop { + if n == MAX { + break; + } + let a = FixedTrait::new(n * 113187804032455040 * 256 / MAX + 1, false); + let sin1 = sin_fast(a); + let sin2 = sin(a); + + assert_precise(sin1, sin2.mag.into(), 'invalid sin', error); + + n += 1; + } + } + #[test] #[available_gas(8000000)] fn test_tan() { diff --git a/src/f64/math/trig.cairo b/src/f64/math/trig.cairo index e2efb43..d0d62c2 100644 --- a/src/f64/math/trig.cairo +++ b/src/f64/math/trig.cairo @@ -446,6 +446,28 @@ mod tests { assert_precise(sin_fast(a), 4129170786, 'invalid -17', error); // 0.9613974918793389 } + #[test] + #[available_gas(9_000_000_000)] + fn test_compare_sin() { + let error = Option::Some(42950); // 1e-5 + let pi = FixedTrait::new(PI, false); + + let MAX: u64 = 256 * 4; + let mut n: u64 = 0; + loop { + if n == MAX { + break; + } + let a = FixedTrait::new(n * 26353589 * 256 / MAX + 1, false); + let sin1 = sin_fast(a); + let sin2 = sin(a); + + assert_precise(sin1, sin2.mag.into(), 'invalid sin', error); + + n += 1; + } + } + #[test] #[available_gas(8000000)] fn test_tan() {