Skip to content
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

some trigonometric algorithm #2000

Open
wants to merge 12 commits into
base: master
from
Open

some trigonometric algorithm #2000

wants to merge 12 commits into from

Conversation

@zhbi98
Copy link

zhbi98 commented May 18, 2020

Describe your change:

Created a new file maths.py
Internally implemented some trigonometric functions, root root, factorial, power algorithm

zhbi98 added 3 commits May 20, 2020
zhbi98 added 7 commits May 20, 2020
@zhbi98 This is a cool submission.  Thanks for doing this.  Please check out the tests that I have added.
These algorithms must agree with the corresponding Python builtins and standard library functions.
@TheAlgorithms TheAlgorithms deleted a comment from zhbi98 Jun 11, 2020
@TravisBuddy
Copy link

TravisBuddy commented Jun 11, 2020

Travis tests have failed

Hey @zhbi98,
Please read the following log in order to understand the failure reason.
It'll be awesome if you fix what's wrong and commit the changes.

TravisBuddy Request Identifier: 0fbe89b0-abbf-11ea-a7dd-93c9ffbfb007
@mquevill
Copy link
Contributor

mquevill commented Jun 15, 2020

All of the trigonometric functions in the standard math library are in radians, which is why the tests are failing that try to compare sin() and _sin() (and other trig functions). Further more, even if we convert to radians, we will not get the exact same answer, due to float operations.

For example:

maths._sin(720)
7.153372857113005e-13
>>> math.sin(math.radians(720))
-4.898587196589413e-16

In your test for _power, you end up with 0 ** -100, which throws ZeroDivisionError: 0.0 cannot be raised to a negative power. However, there are larger issues with _power... You use range(n) to do repeat multiplication, but this will generate [] for a negative n, so any number raised to a negative n will return 1.

In general, for checking floats, you need to check against a tolerance instead of just using ==. For instance, math.sqrt(95) = 9.746794344808963 but maths._sqrt(95) = 9.746794344808965. These two values are not equal with ==, but are within 10^-14 of each other, which is often as good as you can do with floats.

@cclauss
Copy link
Member

cclauss commented Jun 15, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

4 participants
You can’t perform that action at this time.