faheel / BigInt

Arbitrary-sized integer class for C++
https://faheel.github.io/BigInt
MIT License
390 stars 132 forks source link

sqrt() always returns 1 #56

Closed SJS830 closed 4 years ago

SJS830 commented 4 years ago

Details

Calling sqrt() always returns 1, regardless of the number

System info

faheel commented 4 years ago

That seems very weird. The unit tests check the functionality of sqrt() on various numbers and it seems to be functioning as expected, even though the I've only checked it on Linux locally and also on macOS in the CI builds. Can some with Windows and MinGW check it out?

@SJS830 It would be helpful if you can also specify which version of Windows and MinGW you tested it on. It would help further if you can also check if using a different version of the same compiler, or using a different compiler, fixes the issue.

nihilscire commented 4 years ago

The problem shows up when using the single-include header file: in this sqrt implementation the sqrt_prev is initiliazed as BigInt sqrt_prev = 0, if the function parameter num is a two-digits number sqrt_current is initialized with 1, and the following loop while (abs(sqrt_current - sqrt_prev) > 1) never runs thus returning 1.

This is not happening in the tests because the test FunctionsMathTest is actually linked against a version of math.hpp where sqrt_prev is initialized as BigInt sqrt_prev = -1.

Mikadore commented 4 years ago

I think this issue can be closed. Running release.sh on the current master yields a BigInt.hpp which doesn't have that issue. What remains is to fix the file distributed under release 4.0.0

faheel commented 4 years ago

I've published a new release (v0.5.0-dev) which includes the fix for this.