-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add pure FixedPoint implementations and tests for sin(), cos(), and tan() #1773
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bytegrrrl
wants to merge
4
commits into
SFTtech:master
Choose a base branch
from
bytegrrrl:trig
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
112e423
Add pure FixedPoint implementations and tests for sin(), cos(), and t…
bytegrrrl bec85cd
Implement pure FixedPoint round() function and helpers
bytegrrrl 78ef08e
Implement precision-preserving multiplication and division
bytegrrrl 37ec5dc
Optimize FixedPoint multiplication
bytegrrrl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -203,6 +203,73 @@ void fixed_point() { | |
| TESTNOEXCEPT((FixedPoint<int64_t, 32>::from_float(3.25).sqrt())); | ||
| TESTNOEXCEPT((FixedPoint<uint64_t, 32>::from_float(-3.25).sqrt())); | ||
| } | ||
|
|
||
| // Pure FixedPoint trig tests | ||
| { | ||
| using TrigType = FixedPoint<int64_t, 32, __int128>; | ||
heinezen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Testing sin() and cos() | ||
| for (int i = -100'000; i <= 100'000; i++) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the loop runs for a very long time. Can we go with lower precision for speeding these up a bit? |
||
| TrigType x = TrigType::pi() * (0.001 * i); | ||
|
|
||
| // Test std overloads | ||
| TESTEQUALS(x.sin(), std::sin(x)); | ||
| TESTEQUALS(x.cos(), std::cos(x)); | ||
|
|
||
| // Test vs standard library implementation for doubles | ||
| TESTEQUALS_FLOAT(std::sin(x), std::sin(x.to_double()), 1e-7); | ||
| TESTEQUALS_FLOAT(std::cos(x), std::cos(x.to_double()), 1e-7); | ||
|
|
||
| // Test some trig identities | ||
| TESTEQUALS_FLOAT(std::cos(x), std::sin(TrigType::pi_2() - x), 1e-7); | ||
| TESTEQUALS_FLOAT(std::cos(TrigType::pi_2() - x), std::sin(x), 1e-7); | ||
| TESTEQUALS_FLOAT(std::sin(x) * std::sin(x) + std::cos(x) * std::cos(x), 1.0, 1e-7); | ||
| TESTEQUALS_FLOAT(std::sin(x * 2), std::sin(x) * std::cos(x) * 2, 1e-7); | ||
| TESTEQUALS_FLOAT(std::cos(x * 2), -std::sin(x) * std::sin(x) * 2 + 1, 1e-7); | ||
| TESTEQUALS_FLOAT(std::cos(x * 2), std::cos(x) * std::cos(x) * 2 - 1, 1e-7); | ||
| } | ||
|
|
||
| // Testing tan(), note the reduced precision | ||
| for (int i = -100'000; i <= 100'000; i++) { | ||
| TrigType x = TrigType::pi_2() * (0.001 * i); | ||
|
|
||
| // Skip values where tan(x) trends to infinity | ||
| if (i % 1000 == 0 and i % 2000 != 0) [[unlikely]] { | ||
| continue; | ||
| } | ||
|
|
||
| int asymptote_distance = abs(1000 - abs(i % 2000)); | ||
| if (asymptote_distance <= 1) [[unlikely]] { | ||
| TESTEQUALS_FLOAT(std::tan(x), std::tan(x.to_double()), 1e-2); | ||
| } | ||
| else if (asymptote_distance <= 5) [[unlikely]] { | ||
| TESTEQUALS_FLOAT(std::tan(x), std::tan(x.to_double()), 1e-3); | ||
| } | ||
| else if (asymptote_distance <= 16) [[unlikely]] { | ||
| TESTEQUALS_FLOAT(std::tan(x), std::tan(x.to_double()), 1e-4); | ||
| } | ||
| else if (asymptote_distance <= 60) [[unlikely]] { | ||
| TESTEQUALS_FLOAT(std::tan(x), std::tan(x.to_double()), 1e-5); | ||
| } | ||
| else { | ||
| TESTEQUALS_FLOAT(std::tan(x), std::tan(x.to_double()), 1e-6); | ||
| } | ||
| } | ||
|
|
||
| // Testing tan() at asymptotes | ||
| TESTTHROWS(TrigType::pi_2().tan()); | ||
| TESTTHROWS((-TrigType::pi_2()).tan()); | ||
|
|
||
| // testing tan() vs atan2() | ||
| for (int i = 1; i <= 500; i++) { | ||
| TrigType x(0.01 * std::numbers::pi * i); | ||
| for (int j = -1000; j <= 1000; j++) { | ||
| TrigType y(j * 0.01); | ||
| TESTEQUALS_FLOAT(TrigType(y.atan2(x)).tan() * x, y, 1e-5); | ||
| TESTEQUALS_FLOAT(TrigType(y.atan2(-x)).tan() * -x, y, 1e-5); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| }}} // openage::util::tests | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.