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
strvcat function - concatenation: The function strvcat will concatenate it vertically, that means that it will generate a column vector of the strings.   >> strvcat(fir

Pie chart - plot functions: The MATLAB also has a function pie which will generate a pie chart. Calling the function with the form pie (vec) draws a pie chart, by using the pe

Illustration of Standard Deviation The less spread out the numbers are, therefore smaller the standard deviation will be, as it is a way of determining the spread of the data.

The variance is generally defined in terms of the arithmetic mean as: At times, though, the denominator is defined as n instead of n - 1. The default definition used by t

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 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 =

User-defined functions: Therefore, although many functions compute and return values, some do not. A few functions rather merely accomplish a task. Regardless of what type of

Fliplr function: The fliplr function "flips" the matrix from left to right (in another words the left-most column, the first column, become the last column and so on), and the

Write a MATLAB function [d1, u1, l1, c1, r1] = NaiveGaussArrow(d, u, l, c, r) that takes as input the 5 vectors de ned above representing A. This function performs Naive Gauss redu

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