Page 1 of 1

%binomial and fix

Posted: Thu Dec 15, 2011 4:01 am
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

Re: %binomial and fix

Posted: Thu Dec 15, 2011 9:22 am
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

Re: %binomial and fix

Posted: Sat Dec 17, 2011 5:15 am
by MMandler
Thanks for the hint.

Regards,

Martin