You can't exactly represent that number in double precision floating point. When you call theeps function with a numeric input, it returns the distance to the next largest floating-point number. In your case:

>> format longg

>> eps(4451685225093714772084598273548427)

ans =

5.76460752303423e+17

So representable numbers are widely spaced when you get to that large a magnitude, and there's no guarantee that the particular number you want is one of the representable numbers. See the picture on the first page ofthis Cleve's Corner article, but imagine if the numbers on the X axis were MUCH larger and the red lines MUCH more widely spaced.

So how could you do this? Use Symbolic Math Toolbox and be very careful to use symbolic arithmetic, not double precision arithmetic, when converting your string of hex digits into a symbolic value.

Thesprintf is totally superfluous, it does nothing useful at all here, just slows down the code. In any case, using a propersprintf format string would be quicker than callingnum2str, and provide more control over the number of digits, so why not do that?

format long g gives you at most 15 significant figures. It turns out that is not enough in practice to be unique. There are 24 distinct representable values in unique(pi-37*eps:eps:pi+9*eps), all of which display as 3.14159265358979 under format long g. If the goal is to output enough digits to be able to transfer the values exactly in text form, then format long g is not sufficient.

People get caught by this all the time!

format long g

T = 0.3 - 0.2

T == 0.1

T - 0.1

T =

0.1

ans =

logical

0

ans =

-2.77555756156289e-17

People have difficulty understanding why a value that shows up as 0.1 does not compare as equal to 0.1: the limits of format long g have real effects.

This website uses cookies to improve your user experience, personalize content and ads, and analyze website traffic. By continuing to use this website, you consent to our use of cookies. Please see our Privacy Policy to learn more about cookies and how to change your settings.