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>
Do you think this maybe a candidate of being added to the list of gretl
native functions?
Maybe so. The user-function can, however, be written more
tersely -- particularly if we take returning NA as sufficient
indication that the question is invalid:
function scalar iseven (scalar a)
return a != floor(a) ? NA : a % 2 == 0
end function
Allin