jjepsuomi
jjepsuomi

Reputation: 4373

Integer value doesn't compare equal to a float in Matlab

I'm trying to compare two values

y = 1
ye = 1.0000

If I compare in Matlab for example y == ye I get 0?!

The data in y is read from a text file...could that have something to do with this?

Upvotes: 0

Views: 644

Answers (1)

Amro
Amro

Reputation: 124553

Try printing in full precision on both vectors:

fprintf('%.15f\n',x(:))

Instead, when you compare floating points, use something like:

abs(x1-x2) < e

Where e is some appropriate small value

Upvotes: 4

Related Questions