-
Notifications
You must be signed in to change notification settings - Fork 132
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
New pow function #10
New pow function #10
Conversation
I just fall in that the |
UPDATE: I just tested it after including the released file, |
include/functions/math.hpp
Outdated
--- | ||
Returns the absolute value of a BigInt. | ||
*/ | ||
|
||
BigInt abs(const BigInt& num) { | ||
const BigInt big_abs(const BigInt& num) { |
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.
Why return a const BigInt
and not a BigInt
?
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.
To be honest, I was just trying things to resolve a problem.
include/functions/math.hpp
Outdated
Return a BigInt equal to my_big_int^x | ||
NOTE: x could be BigInt, long long, or string | ||
*/ | ||
BigInt pow(BigInt my_big_int, const BigInt x) { |
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.
- I made a mistake while mentioning the parameters for
pow
(sorry!). I've updated them now. It should bewhere the type(const T& base, const long long& exponent)
T
isBigInt
,long long
andstd::string
. - The exponent should only be an integer (
long long
), as I can't seem to think of situations where aBigInt
would need to be raised to an exponent greater than 263-1 (it would require > 86 EiB of RAM!).
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.
Sure, on it
Hmmmm. about the cmath library, maybe I goofed somewhere, but wouldn't it still be better if they were member functions, that way an user can use them without the need to additionally add the respective library and it would be easier to know about them without reading all the doc. |
|
I have changed the
Gives me an error when compiling. |
What is the error that you get? |
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.
All non-integral parameters (including integers greater than int
) should be passed as constant references (const Type&)
.
include/functions/math.hpp
Outdated
Return a BigInt equal to my_big_int^x | ||
NOTE: x could be BigInt, long long, or string | ||
*/ | ||
BigInt pow(BigInt my_big_int, long long x) { |
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.
The parameters should be passed as constant references (const Type&
).
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.
Done
TEST_CASE("abs", "[conversion][string]") { | ||
BigInt a("-12"); | ||
BigInt b("32"); | ||
BigInt c("-50"); |
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.
This file should be test/functions/math.test.cpp
. It already exists. You can add just the test cases for pow()
.
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.
Alright.
Ok, I think I found the problem, when testing the pow function I get this error:
Which is because I have not added the library
|
@abelmarm Have you been able to fix the issue you were facing? If not, please push your changes so that I can take a look and help. |
I haven't, I have pushed the changes |
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.
These changes fix the problem
test/functions/math.test.cpp
Outdated
@@ -6,6 +6,8 @@ | |||
#include "functions/conversion.hpp" | |||
#include "functions/math.hpp" | |||
#include "operators/io_stream.hpp" | |||
#include "operators/relational.hpp" | |||
#include "operators/unary_arithmetic.hpp" |
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.
Add:
#include "operators/binary_arithmetic.hpp"
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.
conversion.hpp
and unary_arithmetic.hpp
are not needed here.
include/functions/math.hpp
Outdated
|
||
#include "operators/relational.hpp" | ||
#include "operators/unary_arithmetic.hpp" | ||
|
||
#include "operators/binary_arithmetic.hpp" |
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.
Remove all operator/
includes.
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.
- Try different values in different test cases.
test/functions/math.test.cpp
Outdated
@@ -6,6 +6,8 @@ | |||
#include "functions/conversion.hpp" | |||
#include "functions/math.hpp" | |||
#include "operators/io_stream.hpp" | |||
#include "operators/relational.hpp" | |||
#include "operators/unary_arithmetic.hpp" |
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.
conversion.hpp
and unary_arithmetic.hpp
are not needed here.
test/functions/math.test.cpp
Outdated
|
||
long long a1 = 12; | ||
long long b1 = 32; | ||
long long d1 = 23; |
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.
Directly pass these values to the function if they're not to be reused.
test/functions/math.test.cpp
Outdated
TEST_CASE("Pow with BigInt", "[conversion][string]") { | ||
BigInt a("12"); | ||
BigInt b("32"); | ||
BigInt c("50"); |
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.
Just one BigInt
object is enough. You can reassign it values as required:
BigInt big_num = 12;
...
big_num = 32;
Also, its better to check for values of different orders of magnitude. Eg. 2 digit, 8 digit, 20 digit, 100 digit. You can use WolframAlpha or Python to easily generate numbers of a certain size.
test/functions/math.test.cpp
Outdated
|
||
REQUIRE(pow(a, b1).to_string() == "34182189187166852111368841966125056"); | ||
REQUIRE(pow(b, a1).to_string() == "1152921504606846976"); | ||
REQUIRE(pow(c, d1).to_string() == "1192092895507812500000000000000000000000"); |
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.
No need of .to_string()
. Simply do:
REQUIRE(pow(big_num, 12) == "34182189187166852111368841966125056");
Changes done, don't mind the sqrt merge I was working on that when i did what you said. |
test/functions/math.test.cpp
Outdated
a = 32; | ||
REQUIRE(pow(a, (long long)12) == "1152921504606846976"); | ||
a = 50; | ||
REQUIRE(pow(a, (long long)23) == "1192092895507812500000000000000000000000"); |
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.
No need to explicitly cast to long long
.
test/functions/math.test.cpp
Outdated
REQUIRE(pow("33", (long long)27) == "99971538734896047460249499950752967950177"); | ||
REQUIRE(pow("21", (long long)59) == "1025506433613486607375777617584133309366191904729927960524981845743709132117581"); | ||
REQUIRE(pow("42", (long long)59) == "591164210212831306083214062268562882225191490961799646040575789354982868342002274605481253142528"); | ||
REQUIRE(pow("50", (long long)51) == "444089209850062616169452667236328125000000000000000000000000000000000000000000000000000"); |
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.
No need to explicitly cast to long long
.
test/functions/math.test.cpp
Outdated
REQUIRE(pow((long long)20, (long long)40) == "10995116277760000000000000000000000000000000000000000"); | ||
REQUIRE(pow((long long)30, (long long)45) == "2954312706550833698643000000000000000000000000000000000000000000000"); | ||
REQUIRE(pow((long long)45, (long long)30) == "39479842665806602234295041487552225589752197265625"); | ||
REQUIRE(pow((long long)40, (long long)20) == "109951162777600000000000000000000"); |
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.
No need to explicitly cast to long long
.
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.
It doesn't compile otherwise.
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.
I think only pow(long long, long long)
causes problems. Try casting just the base
as long long
if that's the case.
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.
The workaround would be to write ll
at the end.
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.
Use uppercase LL
though since its easier to differentiate from 11
.
Once the changes are done please squash the commits into a single commit. Then it'll be good to merge 👍 |
Everything is done and committed to a final commit. |
I see, but the commits haven't been squashed into a single commit. Try the following:
|
4b4348e
to
c3fe8b7
Compare
Resolved conflicts
I had a lot of problems doing that, but I think it kinda worked at last, the conflicting files are the README because you worked on it after my last fetch, but I just hard copied it, and both math files which had the problematic includes we had talked about before. |
Thanks a lot for contributing @abelmarm! |
pow(my_big_int, x) has been added along with a change to the name of the function abs -> big_abs to avoid conflict with cmath.
relates to #5