3 ms·
Honest question (has the potential to sound sarcastic): Is there a reason to test if the input is a number before testing if it is an integer? I would expect t
by ksaj 6y ago
Honest question (has the potential to sound sarcastic): Is there a reason to test if the input is a number before testing if it is an integer?
I would expect that it doesn't matter if isInteger receives a number, character, decimal, null, or whatever as long as the isInteger test can in fact detect if the input wasn't an integer, and subsequently let you know it was expecting one. The given code seems to suggest there might be non-numerical integers out there.
I imagine something like this:
isNumber("A") => 'expected a number'
isNumber(2.2) => True
isInteger(2.2) => 'expected an integer'
isInteger(2) => FINALLY YOU GOT IT!
In other words, a high level construct is being treated as if it were a low level construct, which would seemingly create a lot of duplication at run time.
In Lisp, for example, I have only ever directly checked evenp or oddp without using numberp or integerp first, and haven't seen anything instructing that it should be done any other way. There would surely be a speed penalty if those tests were done separately before calling evenp, since evenp itself would obviously test those cases already.
I can't think of any input value that isn't a (numerical) integer that would trip up evenp or oddp, given that only an integer could possibly pass either test.