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
Polar Form: Any complex number z = a + bi can be thought of as a point (a,b) or vector in the complex plane in which the horizontal axis is the real part of z, and the vertica

Function fopen - file function: The permission string in the call to the fopen function identifies that the file is opened for writing to it. Just as when reading from a file,

Illustration of Labels and prompts: The program below prompts the user for endpoints (x1, y1) and (x2, y2) of a line segment, and computes the midpoint of the line segment, th

Counting in a While Loop: Whenever it is not known ahead of the time how many values will be entered into the script, it is often essential to count the number of values which

Strings as Vectors: The Strings are considered as vectors of characters-or in another words, a vector in which each and every element is a single character-so numerous vector

Three-Dimensional Plots: The MATLAB has many functions which will display three-dimensional plots. Most of these functions have similar name as corresponding two-dimensional p

Animation: In this part we will observe a couple of ways to animate a plot. These are visuals, therefore the outcomes can't really be shown here; it is essential to type these


Writing variables to a File: The save command is used to write variables to a file, or to append the variables to a MAT-file. By the default, save function writes to a MAT-fil

Illustration of Advanced file input and output: For illustration, to refer to the third number in the first element of the cell array: >> subjdata{1}(3) ans =