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

  • Thread starter Thread starter gerben
  • Start date Start date
  • Tags Tags
    Integers Matlab
Click For Summary
The discussion centers around a MATLAB expression that yields a result close to zero instead of exactly zero when performing integer division. The expression 1019 / 250 * 250 - 1019 results in -1.1369e-013 due to MATLAB's handling of floating-point arithmetic, as it does not have a specific integer variable type. To ensure integer outputs, users are advised to utilize the fix() or floor() functions to round the results appropriately. Additionally, it is noted that functions like int2str incorporate a fix() operation within their definitions to manage this issue.
gerben
Messages
511
Reaction score
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
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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K