%binomial and fix

For questions and discussion related to reading in and working with data.
MMandler
Posts: 11
Joined: Tue Sep 01, 2009 1:57 am

%binomial and fix

Unread post by MMandler »

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
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: %binomial and fix

Unread post by TomDoan »

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
MMandler
Posts: 11
Joined: Tue Sep 01, 2009 1:57 am

Re: %binomial and fix

Unread post by MMandler »

Thanks for the hint.

Regards,

Martin
Post Reply