Reputation: 3156
e = realmin;
x = 1 - e;
fprintf('x is %g',x);
Produces x is 1
. However, I am trying to set x
to 1 minus a small epsilon, which is equal to realmin
.
How do I stop the rounding up?
Upvotes: 1
Views: 1805
Reputation: 78316
Consider the difference between the exponents of realmin
and of 1
-- the difference is about 308
. This means that Matlab would need about 308 digits (all of them 9 apart from the last one perhaps) to represent 1-realmin
but Matlab, of course, works natively with double precision floating point numbers which provide only 15 or 16 decimal digits.
The 'problem' is one of failing to understand one of the subtleties of floating-point arithemtic. The nearest representable double precision number to 1 - realmin
is 1
.
The function you want is Matlab's eps
. While 1 - eps(1)
is represented (with format long
set) as 1.000000000000000
evaluation of 1 - eps(1) == 1
returns 0
or false
if you prefer. Here it is the representation that is a bit confusing.
Upvotes: 5