Error-checking for integers, MATLAB in Mathematics

Error-Checking for Integers:

As MATLAB uses the type double by default for all the values, to check to make confirm that the user has entered an integer, the program have to convert the input value to an integer type (e.g., int32) and then check to see whether it is equivalent to the original input. The following illustrations illustrate the concept.

When the value of the variable num is a real number, converting it to the type int32 will round it; therefore the result is not similar as the original value.

 

>> num = 3.3;

>> inum = int32(num)

inum =

3

>> num == inum

ans =

0

 

If, on another hand, the value of the variable num is an integer, convert it to an integer type will not change the value.

>> num = 4;

>> inum = int32(num)

inum =

4

>> num == inum

ans =

1

The script below uses this idea to error-check for integer data; it loops until the user properly enters an integer.

741_Error-Checking for Integers.png

Posted Date: 10/19/2012 6:15:24 AM | Location : United States







Related Discussions:- Error-checking for integers, Assignment Help, Ask Question on Error-checking for integers, Get Answer, Expert's Help, Error-checking for integers Discussions

Write discussion on Error-checking for integers
Your posts are moderated
Related Questions
Error-Checking for Integers: As MATLAB uses the type double by default for all the values, to check to make confirm that the user has entered an integer, the program have to c

Example of Recursive functions: This definition is recursive as a factorial is defined in terms of the other factorial. There are two parts to any recursive definition: the co

Illustration of a recursive function: illustration is of a recursive function which does not return anything, but simply prints. The below function prtwords receives a sentenc

Concatenation: The String concatenation means to join the strings altogether. Of course, as strings are merely vectors of the characters, the technique of concatenating vector

Standard Deviation The standard deviation is the square root of variance: The built-in function in a MATLAB for the standard deviation is known as std; the standard dev

Nested IF-ELSE statements: The if-else statement is used to select between the two statements. In order to select from more than two statements, the if-else statements can als

fprintf function - File function: The fprintf function really returns the number of bytes which was written to the file, therefore if you do not want to see that number, suppr

Types of User-defined Functions: We know how to write a user-defined function, stored in an M-file, which computes and returns one value. This is merely one type of the functi

Customizing Plots : There are numerous ways to customize figures in the Figure Window. On clicking the Plot Tools icon will bring up the Property Editor & Plot Browser, with ma

Function isreal - complex numbers: The function isreal returns 1 for logical true when there is no imaginary part of the argument, or 0 for false when the argument does have a