Error-checking for integers, MATLAB in Mathematics

Assignment Help:

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


Related Discussions:- Error-checking for integers

Symbolic expression, Symbolic Expression The solve function solves an e...

Symbolic Expression The solve function solves an equation and returns the solution(s) as symbolic expressions. The answer can be converted to numbers by using any numeric funct

Logical vectors, Logical Vectors: The relational operators can also be...

Logical Vectors: The relational operators can also be used with the vectors and matrices. For illustration, let's say that there is a vector, and we want to compare each eleme

Illustration of variance, Illustration of Variance For illustration, fo...

Illustration of Variance For illustration, for the vector [4, 6, 1, 5], there are n = 4 values therefore n - 1 is 3. The mean of this data set is also 4. The variance will be

Counting in a while loop, Counting in a While Loop: Whenever it is not...

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

Illustrations of variable number of output arguments, Illustrations of Vari...

Illustrations of Variable number of output arguments: In the illustrations shown here, the user should actually know the type of the argument in order to establish how many va

Empty vectors, Empty Vectors: An empty vector or in another words, a v...

Empty Vectors: An empty vector or in another words, a vector which stores no values, can be generated using the empty square brackets: >> evec = [] evec = [] >> lengt

Refer the subset of a matrix, Refer the subset of a matrix: It is also...

Refer the subset of a matrix: It is also possible to refer to the subset of a matrix. For illustration, this refers to the first & second rows, second & third columns: >> m

Sprintf function, sprintf function: The sprintf function works precise...

sprintf function: The sprintf function works precisely like the fprintf function, but rather than printing it generates a string. Here are some illustrations in which the outp

Variance, The variance is generally defined in terms of the arithmetic me...

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

Illustration of a recursive function, Illustration of a recursive function:...

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

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd