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

Added sqrt functions #11

Merged
merged 6 commits into from
Dec 31, 2017
Merged

Added sqrt functions #11

merged 6 commits into from
Dec 31, 2017

Conversation

arvindvs
Copy link
Contributor

@arvindvs arvindvs commented Dec 29, 2017

I have made updates to the math.hpp and math.test.cpp files, including 3 new test cases for the sqrt function.

Relates to #5.

@faheel faheel added the functions: math Math functions label Dec 29, 2017
@faheel
Copy link
Owner

faheel commented Dec 29, 2017

Can you tell me which OS and compiler you use?

@arvindvs
Copy link
Contributor Author

I use Windows OS with QT Creator compiler

}
}
} else {
throw std::invalid_argument("Expected a non-negative BigInt");
Copy link
Owner

Choose a reason for hiding this comment

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

This should be at the beginning of the function:

if (num < 0)
    throw std::invalid_argument("Cannot compute square root of a negative integer");

Then you wouldn't need to enclose the rest of the body inside if (num >= 0) { ... }.

BigInt big_num1 = "3479680743635798438602889954485038919559460388450";
REQUIRE(sqrt(big_num1) == "1865390238967653234193278");

BigInt big_num2 = "471336517784520212796937741396686403167942977773121645803479339647737161";
Copy link
Owner

Choose a reason for hiding this comment

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

Create a single BigInt object and reuse it:

BigInt num = 12345;

...

num = 78910;

REQUIRE(sqrt(big_num2) == "686539523832765372419381874316283619");

BigInt big_num3 = "304953007708768585645827645127232631942308013843965557450677671087391006548030000";
REQUIRE(sqrt(big_num3) == "17462903759362833573945713140457169848646");
Copy link
Owner

Choose a reason for hiding this comment

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

  • Please add at least 5 more cases with numbers of different magnitudes - such as 5 digits, 20 digits, 100 digits, 500 digits etc.
  • Verify base cases in a separate test case (as in case of pow()). The base cases will check the square root of 0, 1 and -1.

@@ -173,3 +173,15 @@ TEST_CASE("pow with string base", "[functions][math][pow][string]") {
"08862990907940498760592041482791966715057053380053636634477979263"
"4601932346249911644260403539535769743430516736");
}

TEST_CASE("Sqrt of big integers", "[functions][math][sqrt][big]") {
BigInt big_num1 = "3479680743635798438602889954485038919559460388450";
Copy link
Owner

Choose a reason for hiding this comment

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

I think this is where you're getting compilation errors. Please see #3 for workarounds.

@faheel
Copy link
Owner

faheel commented Dec 29, 2017

I use Windows OS with QT Creator compiler

Since I don't frequently use Windows (except for gaming 👾), I haven't yet tried compiling the library on it. But I'm logged on to Windows at the moment and will figure out a simple way to compile and will update you regarding it. Until then please make the changes requested in the review.

@arvindvs
Copy link
Contributor Author

Thanks Faheel, I've made the changes you requested.

@faheel
Copy link
Owner

faheel commented Dec 30, 2017

I tried using CMake for cross-platform building, and though it compiles with CLion on Windows, it may not work with other IDEs/compilers on Windows. If you have an IDE that supports CMake, then you can add CMakeLists.txt to the project's root folder and can compile on Windows from that IDE.

If not, then what you can do for now is that when you feel that your changes look good, push them to GitHub and check out the Travis build log for any compile errors or test failures.

Also, there was a problem with the test script that made Travis think that the tests had passed even if some test had failed. So the build was passing even though all the test cases for sqrt() were failing. sqrt() currently always returns 1 for every positive integer as input. So please look into it.

@faheel
Copy link
Owner

faheel commented Dec 30, 2017

The test script is fixed now, so please update your fork (by pulling from upstream).

@codecov-io
Copy link

codecov-io commented Dec 30, 2017

Codecov Report

Merging #11 into master will decrease coverage by 0.03%.
The diff coverage is 91.66%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #11      +/-   ##
==========================================
- Coverage    96.5%   96.47%   -0.04%     
==========================================
  Files          12       12              
  Lines         458      482      +24     
==========================================
+ Hits          442      465      +23     
- Misses         16       17       +1
Impacted Files Coverage Δ
include/functions/math.hpp 95.83% <91.66%> (-4.17%) ⬇️
include/operators/binary_arithmetic.hpp 98.14% <0%> (+0.46%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 91a7f56...9ff7469. Read the comment docs.

@faheel faheel merged commit 6bc7625 into faheel:master Dec 31, 2017
@faheel
Copy link
Owner

faheel commented Dec 31, 2017

@arvindvs Thanks a lot for contributing to the project!

And sorry for the inconvenience caused by Windows builds not being supported. It will be supported soon (hopefully!).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
functions: math Math functions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants