Troubleshooting Integers in Matlab: Why Does 1019/250*250-1019 Not Result in 0?

In summary, integers in Matlab are whole numbers without any decimal or fractional parts, represented as data type 'int'. Matlab has built-in functions for performing arithmetic operations with integers and can convert decimal numbers to integers using the 'int32' function. The range of integers in Matlab depends on the data type, with 'int8' holding values from -128 to 127, 'int16' holding values from -32,768 to 32,767, and 'int32' holding values from -2,147,483,648 to 2,147,483,647. To create a vector of integers in Matlab, the colon operator can be used, such as '1:10' to create a row vector from 1
  • #1
gerben
511
1
Does anyone have an idea why this expression in Matlab with integers does not give zero as an answer:

>> 1019 / 250 * 250 - 1019
ans = -1.1369e-013
 
Physics news on Phys.org
  • #2
AFAIK, MATLAB doesn't specifically store an integer variable type.

If you need to be sure you have an integer output, use either:

fix(input)

or

floor(input)

to round it to the nearest or down to the next lowest integer.

Functions such as int2str automatically have a fix() in the command definition.
 
  • #3


There are a few possible reasons why this expression does not result in 0. One possibility is that Matlab is using floating-point arithmetic, which means that the numbers are not being represented exactly. This can lead to small rounding errors, which can result in a non-zero answer. Another possibility is that the order of operations is causing the expression to be evaluated in a way that produces a non-zero answer. In this case, the division and multiplication would be performed first, resulting in a non-zero answer, and then the subtraction would be performed. To get a more accurate result, you could try using the "format long" command to increase the precision of the output or use the "round" function to round the answer to the nearest integer. It's also important to make sure that the numbers being used are actually integers and not stored as floating-point numbers.
 

1. What are integers in Matlab?

Integers in Matlab refer to whole numbers, both positive and negative, without any decimal or fractional parts. They are represented as data type 'int' in Matlab.

2. How can I perform arithmetic operations with integers in Matlab?

Matlab has built-in functions for performing arithmetic operations with integers, such as addition, subtraction, multiplication, and division. These functions can be used with the 'int' data type to perform calculations on integers.

3. Can I convert a decimal number to an integer in Matlab?

Yes, you can use the 'int32' function in Matlab to convert a decimal number to an integer. This function will truncate the decimal part of the number and return the integer value.

4. What is the range of integers in Matlab?

The range of integers in Matlab depends on the data type being used. The 'int8' data type can hold values from -128 to 127, 'int16' can hold values from -32,768 to 32,767, and 'int32' can hold values from -2,147,483,648 to 2,147,483,647.

5. How can I create a vector of integers in Matlab?

To create a vector of integers in Matlab, you can use the colon operator. For example, to create a vector of integers from 1 to 10, you can use the expression '1:10'. This will create a row vector with values 1, 2, 3, ..., 10.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
752
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • Introductory Physics Homework Help
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
1K
Back
Top