Floating-point values give limitations for true-false?
1 view (last 30 days)
Show older comments
Decide if the equation is mathematically true for the variable values provided below.
Implement the equations in MATLAB, and test if they are true in the floating point arithmetic within MATLAB for the variable values provided. (For example, the equation 3 + 7^2 = 5 in matlab would correspond to the command disp(3+7^2==5) and yields, upon execution of the command, the value 0 corresponding to false. This is expected since the equation is not mathematically true.)
Where the MATLAB result differs from the mathematical expectation explain in one or more full sentences why the MATLAB results are different.
The equations to be considered are, with x = 10^7,
1. x^2 + x = x
2. x + x^5 = x
3. x^21/x^6 = x^15
In regards to 3. how come the value is equal to 0 (false) and not equal 1 (true) unless the input of x is less than 10. should it be true or false?
%Matlab program to check True or false of the statements for given values
%OUTPUT where 1 means True, 0 means False
x = 10^7;
aPart1 = x^2+ x
disp(x^2+ x== x)
aPart2 = x+ x^5
disp(x+ x^5== x)
aPart3 = x^21/ x^6
disp(x^21/x^6== x^15)
1 Comment
Stephen23
on 20 Sep 2021
Edited: Stephen23
on 20 Sep 2021
"In regards to 3. ... should it be true or false?"
That is the entire point of your assignment: to understand that in general floating point arithmetic is not the same as mathematical/symbolic/algebraic arithmetic. Search this forum for "binary floating point", you will find many good explanations (which you can then "explain in one or more full sentences", just as your assignment requests).
Answers (1)
Zuber Khan
on 18 Jul 2024
Hi,
MATLAB has data types for double-precision (double) and single-precision (single) floating-point numbers following IEEE Standard 754. By default, MATLAB represents floating-point numbers in double precision. Further, MATLAB follows the round to nearest, ties to even rounding mode by default.
The double- and single-precision data types have a largest and smallest value that you can represent. Numbers outside of the representable range are assigned positive or negative infinity. However, some numbers within the representable range cannot be stored exactly due to the gaps between consecutive floating-point numbers, and these numbers can have round-off errors.
The accuracy of floating-point data can be affected by several factors:
- Limitations of your computer hardware - For example, hardware with insufficient memory truncates the results of floating-point calculations.
- Gaps between each floating-point number and the next larger floating-point number - These gaps are present on any computer and limit precision.
Almost all operations in MATLAB are performed in double-precision arithmetic conforming to IEEE Standard 754. Because computers represent numbers to a finite precision, some computations can yield mathematically nonintuitive results. Some common issues that can arise while computing with floating-point numbers are round-off error, cancellation, swamping, and intermediate conversions. The unexpected results are not bugs in MATLAB and occur in any software that uses floating-point numbers. For exact rational representations of numbers, consider using the Symbolic Math Toolbox.
Now, in your case, for very large x, such as x = 10^7, the values x^21 and x^15 become extremely large. The floating-point representation may lose precision, resulting in unexpected behavior. When MATLAB performs the division, the result could be slightly off due to rounding errors.
Therefore, even though the equation is mathematically true, it does not hold to true when the same operation is performed in MATLAB.
For more information, you can refer to the following documentation related to Floating Point Numbers in MATLAB.
I hope it addresses your query.
Regards,
Zuber
0 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!