Weird results while coding using an m-file
Show older comments
Hi,
When using the following code, I expect A = B. The difference (Differ) is however not zero. Any clue?
Thanks
clear all, clc
Vs = 10;
R = 10;
L = 10e-3;
C = 400e-6;
A = (R/(2*L))^2
B = 1/(L*C)
%B = 2.5000e+05
Differ = A-B
Answers (1)
Note that Differ == eps(A), which means that A and B are as close as they can be without being exactly equal.
Vs = 10;
R = 10;
L = 10e-3;
C = 400e-6;
A = (R/(2*L))^2
B = 1/(L*C)
Differ = A-B
eps(A)
eps(A) == Differ
2 Comments
Don't expect Differ to be exactly zero.
When comparing two floating point numbers, don't check for exact equality, use a small tolerance, e.g., abs(A-B) <= 10*eps(A)
or ismembertol
Or use the Symbolic Math Toolbox, e.g. vpa.
Categories
Find more on Special Values 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!