0
x=20;
LHS1=(sind(x)+cosd(x))^2
RHS1=1+2*sind(x)*cosd(x)
LHS1==RHS1
LHS2=(1-2*cosd(x)-3*(cosd(x))^2)/(sind(x))^2
RHS2=(1-3*cosd(x))/(1-cosd(x))
LHS2==RHS2

I am getting this answer as

LHS1 =
   1.642787609686539
RHS1 =
   1.642787609686539
ans =
     1
LHS2 =
 -30.163437477526365
RHS2 =
 -30.163437477526383
ans =
     0
Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
  • 4
    [There is no general solution for comparing floating-point numbers.](https://stackoverflow.com/questions/21260776/how-to-compare-double-numbers/21261885#21261885). What ought to be done depends on the context of the problem, what you are trying to accomplish, what consequences falsely accepting unequal numbers as equal will have, and more. Why are you trying to compare these numbers? What do you need to do? – Eric Postpischil Jul 19 '20 at 20:39

2 Answers2

1

That depends on how close you want the two values to be, to be considered "true". Can LHS2 and RHS2 be different by 0.1 and be considered equal? 0.001? 0.0000001? etc.

Decide on how close you want them to be, then check if the distance between the two are below some absolute amount.

threshold = 0.00001; % You decide this
difference = abs(LHS2 - RHS2); % Absolute distance between the two numbers

if (difference <= threshold)
    % Do something
end

Note that you should not make the threshold too small, because computers have limits to how precise a decimal number can be (machine epsilon).

mimocha
  • 1,041
  • 8
  • 18
0

You may use ismembertol(x,y,tol) built-in function. In this case, x and y are your numbers while tol is your tolerance gap. You can make your tolerance really small to be able to sure about your equality.

For example,

x=1.000000001;
y=1;

tol=eps(1);

tol =

     2.220446049250313e-16

ismembertol(x,y,tol)

ans =

  logical

   0

This means x and y are not equal.

harman
  • 5
  • 3