On Fri, 9 Nov 2012, Ignacio Diaz-Emparanza wrote:
Slightly related (but different) to this question: I needed
frequently a
way to determine whether an integer is even or odd, and I finally wrote
this function:
<hansl>
function scalar iseven(scalar A)
if (A==floor(A))
scalar A2=floor(A/2)
scalar A2b=A/2
scalar even = (A2==A2b) ? 1 : 0
else
scalar even = NA
funcerr "Input is not an integer"
endif
return even
end function
# Examples:
scalar a=34
scalar b=iseven(a)
scalar c=67
scalar b=iseven(c)
b
scalar b=iseven($pi)
</hansl>
This one is probably a bit better:
<hansl>
function scalar iseven(scalar a)
return (floor(a)==a) ? !(a%2) : NA
end function
</hansl>
--------------------------------------------------
Riccardo (Jack) Lucchetti
Dipartimento di Economia
Università Politecnica delle Marche
(formerly known as Università di Ancona)
r.lucchetti(a)univpm.it
http://www2.econ.univpm.it/servizi/hpp/lucchetti
--------------------------------------------------