Skip to content

Commit

Permalink
Merge pull request #33 from tekkac/fix/sin-fast-bugfix
Browse files Browse the repository at this point in the history
Fix: sin fast bugfix
  • Loading branch information
clexmond authored Apr 15, 2024
2 parents adf7008 + 484b2a5 commit 4e6bd52
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/f128/math/lut.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
23 changes: 22 additions & 1 deletion src/f128/math/trig.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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() {
Expand Down
6 changes: 3 additions & 3 deletions src/f64/math/lut.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
24 changes: 23 additions & 1 deletion src/f64/math/trig.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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() {
Expand Down
6 changes: 3 additions & 3 deletions src/math/lut.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 4e6bd52

Please sign in to comment.