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

  • Context: MATLAB 
  • Thread starter Thread starter gerben
  • Start date Start date
  • Tags Tags
    Integers Matlab
Click For Summary
SUMMARY

The expression 1019 / 250 * 250 - 1019 in MATLAB does not yield zero due to floating-point arithmetic precision issues. MATLAB does not have a dedicated integer variable type, which leads to small rounding errors in calculations. To ensure integer outputs, users should utilize the fix() or floor() functions to round results appropriately. The int2str function inherently applies fix() to ensure integer representation.

PREREQUISITES
  • Understanding of MATLAB syntax and operations
  • Familiarity with floating-point arithmetic
  • Knowledge of MATLAB functions: fix() and floor()
  • Basic concepts of numerical precision in programming
NEXT STEPS
  • Research MATLAB floating-point arithmetic and precision issues
  • Learn how to use the fix() and floor() functions in MATLAB
  • Explore the int2str function and its applications in MATLAB
  • Investigate best practices for handling numerical precision in programming
USEFUL FOR

MATLAB users, engineers, and data scientists who require accurate integer calculations and wish to understand floating-point precision issues in their computations.

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
4K
  • · 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
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K