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

Fix -1 for BigInt factorial - Update gmp.jl #51497

Merged
merged 3 commits into from
Oct 29, 2023

Conversation

PallHaraldsson
Copy link
Contributor

@PallHaraldsson PallHaraldsson commented Sep 28, 2023

Fixes #51488

@PallHaraldsson
Copy link
Contributor Author

I noticed also this line can be eliminated:

n == 0 && return one(n)

i.e. 2 checks (branches) or even 1 (not 3), by adding 1 (or zero-based indexing; that will in effect happen, +1 optimized out), if thought to be worth it for one more entry in the lookup table.

@jmkuhn
Copy link
Contributor

jmkuhn commented Sep 28, 2023

Add test for factorial(BigInt(-1)) at

julia/test/mpfr.jl

Lines 695 to 704 in 66fe51f

@testset "factorial" begin
setprecision(256) do
x = BigFloat(42)
@test factorial(x) == factorial(BigInt(42))
x = BigFloat(10)
@test factorial(x) == factorial(10)
@test_throws DomainError factorial(BigFloat(-1))
@test_throws DomainError factorial(BigFloat(331.3))
end
end

@stevengj stevengj added test This change adds or pertains to unit tests kind:bugfix This change fixes an existing bug needs tests Unit tests are required for this change and removed test This change adds or pertains to unit tests labels Sep 28, 2023
@stevengj
Copy link
Member

LGTM, other than the need for a @test_throws DomainError factorial(BigInt(-1)).

@giordano giordano added the domain:bignums BigInt and BigFloat label Sep 29, 2023
@PallHaraldsson
Copy link
Contributor Author

@jmkuhn those tests are maybe in the wrong place. I added at test/gmp.jl, since I kind of want MPFR gone. Actually both.

JeffreySarnoff/ArbNumerics.jl#63 (comment)

@stevengj stevengj removed the needs tests Unit tests are required for this change label Oct 1, 2023
@KshitijThareja
Copy link

Is this issue still open? I also had the same approach, changing the factorial function in base/gmp.jl and the writing tests for it in test/gmp.jl. Its passed all checks when I tested it locally. Should I make a PR for this issue?

test/gmp.jl Show resolved Hide resolved
@rfourquet
Copy link
Member

Should I make a PR for this issue?

Thanks for offering your help @KshitijThareja , but in this case this PR is already open for solving the issue, and we should rather move forward with it.

@longemen3000
Copy link
Contributor

bump? test fails seem unrelated

@oscardssmith oscardssmith merged commit d5c30d8 into JuliaLang:master Oct 29, 2023
4 of 6 checks passed
@PallHaraldsson PallHaraldsson deleted the patch-16 branch October 29, 2023 13:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:bignums BigInt and BigFloat kind:bugfix This change fixes an existing bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

factorial(big(-1)) falsely evaluates to 0 instead of raising error
8 participants