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

bc freezes evaluating cbrt(0.01) #64

Closed
ikolesnikes opened this issue Mar 13, 2023 · 4 comments
Closed

bc freezes evaluating cbrt(0.01) #64

ikolesnikes opened this issue Mar 13, 2023 · 4 comments

Comments

@ikolesnikes
Copy link

bin/bc enters an infinite loop while evaluating the cube-root of 0.01

bin/bc -l -e 'cbrt(0.1)'
returns .46415888336127788924 (which is expected)

bin/bc -l -e 'cbrt(0.01)'
freezes

Looking at the definition of root(x,n) in lib2.bc the following loop never exits

while(r!=q){
	r=q
	q=(p*r+x/r^p)/n
}

A simple hack makes the cbrt function working.

while(abs(r-q)>0.000001){
	r=q
	q=(p*r+x/r^p)/n
}
@gavinhoward
Copy link
Owner

Yes, this is a bug. I have reproduced it.

Your fix pointed right to the problem, which I suspected: it ran into a pathological case where the two values, r and q, never equaled each other because they would keep switching, i.e., r would equal x, and q would equal y, then on the next iteration, r would equal y, and q would equal x.

This is the reason I hate algorithms that are calculated by series. They can easily have problems like this.

I tried to fix it with 01230fc. It increases the precision, but then does the comparison at a smaller precision.

Can you pull and test it for me? I'll start my release process in the meantime.

@ikolesnikes
Copy link
Author

ikolesnikes commented Mar 13, 2023

I tested your solution and it worked!
Maybe it's worth to add a test for this case.

Never mind, you already did it.

@gavinhoward
Copy link
Owner

Yes, but you are right that if I did not, I should have!

I will put out a release with the fix as soon as I can. I'll close this issue at that time.

@gavinhoward
Copy link
Owner

Release 6.5.0 is out! Thank you for the report!

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

No branches or pull requests

2 participants