Skip to content

update complex-numbers solution and add test cases#451

Merged
oxe-i merged 3 commits into
exercism:mainfrom
oxe-i:adjustComplexNumbers
Jun 2, 2026
Merged

update complex-numbers solution and add test cases#451
oxe-i merged 3 commits into
exercism:mainfrom
oxe-i:adjustComplexNumbers

Conversation

@oxe-i

@oxe-i oxe-i commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

I mentioned on #444 that:

While solving prism, I initially tried to use the same implementation of cos and sin that I used in complex-numbers. However, it didn't always work.
After research, I found out that we are supposed to reduce the angle, otherwise the floating-point imprecision accumulates for larger values. The same is true for exp.
Later this week, I'll update the example solution for complex-numbers to use a more correct algorithm for all of those functions.

This PR addresses that by:

  1. using a correct and stable algorithm for sin, cos and exp.
  2. adding new test cases that would fail on the previous solution.
  3. improving the tests to use a relative tolerance.
  4. increasing the difficulty of the exercise, considering the higher level of precision necessary to pass the tests.

About 3: TEST_ASSERT_EQUAL_FLOAT is too strict on a number close to 0 (it requires equality). The reference solution actually passes even if using TEST_ASSERT_EQUAL_FLOAT, but solutions using cexpf or a combination of expf, sinf and cosf would fail. So I used a custom relative tolerance that is at least 1e-05f. I tested it against all three options (reference solution, cexpf and the combination) and they all passed.

@github-actions

github-actions Bot commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

This PR touches files which potentially affect the outcome of the tests of an exercise. This will cause all students' solutions to affected exercises to be re-tested.

If this PR does not affect the result of the test (or, for example, adds an edge case that is not worth rerunning all tests for), please add the following to the merge-commit message which will stops student's tests from re-running. Please copy-paste to avoid typos.

[no important files changed]

For more information, refer to the documentation. If you are unsure whether to add the message or not, please ping @exercism/maintainers-admin in a comment. Thank you!

@kotp kotp left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am liking the confidence that we have done away with floating point error occurrences here.

@oxe-i oxe-i merged commit c8b4629 into exercism:main Jun 2, 2026
8 checks passed
@oxe-i oxe-i deleted the adjustComplexNumbers branch June 2, 2026 00:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants