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

Check overflow in multinomial #172

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

inkydragon
Copy link
Member

@inkydragon inkydragon commented Dec 13, 2024

Fix #99

  • Check overflow in multinomial. For large input, it will throw a OverflowError
  • Add new tests for large input and special case.
  • Update docs (Maybe it should be split into another pr)
julia> using Combinatorics

julia> using BenchmarkTools

julia> @benchmark multinomial(10,10,10,5) evals=2000
BenchmarkTools.Trial: 10000 samples with 2000 evaluations.
 Range (min  max):  170.350 ns  514.200 ns  ┊ GC (min  max): 0.00%  0.00%
 Time  (median):     178.400 ns               ┊ GC (median):    0.00%
 Time  (mean ± σ):   182.391 ns ±  17.635 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

  ▆▅▄▃▆█▇▅▄▄▄▃▃▂▁▁▁▁                                            ▂
  ███████████████████▇█▇▇▇▆▇▆▅▇▆▇▅▆▅▆▅▆▆▅▄▅▅▄▆▅▆▅▅▅▅▆▆▅▅▆▅▄▄▅▄▃ █
  170 ns        Histogram: log(frequency) by time        263 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.

julia> @benchmark multinomial_pr(10,10,10,5) evals=2000
BenchmarkTools.Trial: 10000 samples with 2000 evaluations.
 Range (min  max):  170.500 ns  557.450 ns  ┊ GC (min  max): 0.00%  0.00%
 Time  (median):     178.500 ns               ┊ GC (median):    0.00%
 Time  (mean ± σ):   183.319 ns ±  21.158 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

  █▇▅▇█▇▆▅▅▄▄▃▂▂▂▁▁▁▁                                           ▂
  ████████████████████▇▇█▇█▆▆█▇▇▇▆▆▇▆▆▇▇▆▇▇▆▅▆▇▇▇▆▆▅▆▇▆▅▆▆▅▄▃▄▄ █
  170 ns        Histogram: log(frequency) by time        273 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.

@@ -95,14 +95,42 @@ end
"""
multinomial(k...)

Multinomial coefficient where `n = sum(k)`.
Compute the multinomial coefficient
``\\binom{n}{k_1,k_2,...,k_i} = \\frac{n!}{k_1!k_2! \\cdots k_i!}, n = \\sum{k_i}``.
Copy link
Member Author

Choose a reason for hiding this comment

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

This pr mainly addresses overflow issues, perhaps I should split the document and update it to another pr?

image

Copy link

codecov bot commented Dec 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.89%. Comparing base (85c7193) to head (bc12ba1).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #172   +/-   ##
=======================================
  Coverage   96.88%   96.89%           
=======================================
  Files           7        7           
  Lines         803      804    +1     
=======================================
+ Hits          778      779    +1     
  Misses         25       25           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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

Successfully merging this pull request may close these issues.

multinomial() may lead to hidden overflow
1 participant