%binomial and fix

For questions and discussion related to reading in and working with data.

%binomial and fix

Postby MMandler » Thu Dec 15, 2011 5:01 am

Hi,

I want to construct a n-vector containing the integer binomial coefficients (n,i) where i is the row of the vector. I found that transforming the real binomial coefficients into integers by using the FIX command leads to changes in the coefficient's values.

Here is an example code. The problem seems to occur only for n<=5.

declare integer n
compute n=5

declare vector[integer] bincoeff(n)

do i=1,n
compute bincoeff(i)=fix(%binomial(n,i))
display %binomial(n,i) fix(%binomial(n,i))
end do i



Regards,

Martin
MMandler
 
Posts: 11
Joined: Tue Sep 01, 2009 1:57 am

Re: %binomial and fix

Postby TomDoan » Thu Dec 15, 2011 10:22 am

Use %round(...,0). %fix(x) gives the largest integer less than x, so if the calculation of the binomial coefficient (which is done using gammas and thus isn't exact except for small values) comes in just below the true integer, you get 4 rather than 5. %round(...,0) gives the closest integer.

Code: Select all
do i=1,n
compute bincoeff(i)=%round(%binomial(n,i),0)
display %binomial(n,i) %round(%binomial(n,i),0)
end do i
TomDoan
 
Posts: 2720
Joined: Wed Nov 01, 2006 5:36 pm

Re: %binomial and fix

Postby MMandler » Sat Dec 17, 2011 6:15 am

Thanks for the hint.

Regards,

Martin
MMandler
 
Posts: 11
Joined: Tue Sep 01, 2009 1:57 am


Return to Data: Reading, Writing, Transforming

Who is online

Users browsing this forum: No registered users and 0 guests