How do I test for integers in MATLAB?

前端 未结 7 1403
遥遥无期
遥遥无期 2021-01-04 00:55

I\'m writing a program that will calculate factorials of integers. However, the part I\'m stuck on is if someone enters a non-integer such as 1.3, I\'d like to

7条回答
  •  醉话见心
    2021-01-04 01:39

    You can use the mod function, which returns the remainder after division. All integers are divisible by 1. So a good test for non-integer would be

    integerTest=~mod(value,1);
    

    This returns 0 if value is not an integer and 1 if it is. You can then use this as a conditional to reject non-integer user inputs.

提交回复
热议问题