%binomial and fix
%binomial and fix
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
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
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 iRe: %binomial and fix
Thanks for the hint.
Regards,
Martin
Regards,
Martin