• Skip to main content
  • Skip to search
  • Skip to footer
Cadence Home
  • This search text may be transcribed, used, stored, or accessed by our third-party service providers per our Cookie Policy and Privacy Policy.

  1. Community Forums
  2. Custom IC SKILL
  3. Variable overflow

Stats

  • Locked Locked
  • Replies 3
  • Subscribers 143
  • Views 13767
  • Members are here 0
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Variable overflow

ksek
ksek over 6 years ago

Hi,

I wish to code the below equation:

nCr = (n!) / (r! * (n-r)!)

 

When i use the factorial function (n!)  as below:

procedure( factorial( n )
if( zerop( n ) then
1
else
n*factorial( n-1)
) ; if
) ; procedure

factorial(17) already make the data overflow. That means the max of n that i can use is only 16.

I would like to do factorial of until 5000.

Please let me know if you have any alternatives to do that.

Thanks,

Kean Sek.

  • Cancel
  • mbracht
    mbracht over 6 years ago

    Hi Kean,

    the problem is that you are using (32bit) integers which are (naturally) limited in their size.
    Change your code so as it uses floats and off you go...

    (defun factorial (n)
       if( zerop( n ) then
          1.0
       else
          n*factorial( n-1)
       ) ; if
    )

    But - a factorial of 5000 is such an incredible number that even floats won't do anymore - 170 is the last one that works, after that you get 'inf'
    (factorial 170)
    7.257416e+306
    1>
    (factorial 171)
    inf
    1>

    Just outta curiosity,- why would anybody want to compute the factorial of 5000???

    Max

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • henker
    henker over 6 years ago in reply to mbracht

    You can just unroll the factorials and reduce the formula to get the operations down to r (resp. n-r, whichever is smaller) multiplications and divisions (r-1 if you also skip the last division by 1). With this you can calculate the number of combinations with n=5000 and r<=161 (or >=4839) before reaching number overflow. For n<1030 it will not overflow for any r.

    procedure( combinations_r_outof_n( n r )
        let( (res)
            ;C(n,r) = (n!) / (r! * (n-r)!)
            ;       = Prod((n-r+1)..n) / Prod(1..r)
            cond(
                ( n<r  || n<0   nil )
                ( n==r || n<=1  1   )
                ( t
                    ; swap sides
                    when( 2*r > n
                        r = n-r
                    ); when

                    res = 1.0
                    for(x n-r+1 n
                        res = res * x / r
                        r--
                    ); for
                    when(res<2147483647  res=fix(res))
                    res
                )
            )
        ); let
    ); procedure

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • ksek
    ksek over 6 years ago in reply to henker

    Hi Max,

    I was doing something to count the possible combinations of 2 standard cells abutment in a pool of 5000 standard cells. 
    This require me to code the equation nCr.

    Hi Henker,

    Your solution works well for me. 

    Thanks so much for the simplified algorithm.

    KS.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel

Community Guidelines

The Cadence Design Communities support Cadence users and technologists interacting to exchange ideas, news, technical information, and best practices to solve problems and get the most from Cadence technology. The community is open to everyone, and to provide the most value, we require participants to follow our Community Guidelines that facilitate a quality exchange of ideas and information. By accessing, contributing, using or downloading any materials from the site, you agree to be bound by the full Community Guidelines.

© 2025 Cadence Design Systems, Inc. All Rights Reserved.

  • Terms of Use
  • Privacy
  • Cookie Policy
  • US Trademarks
  • Do Not Sell or Share My Personal Information