|
"Paul " <palex71@hotmail.com> wrote in message <itufrk$gc3$1@newscl01ah.mathworks.com>...
> Right... what I need is to convert the output to 9.7 so that it is equal to a 9.7 value read from another file (so conditional=true). Any ideas?
- - - - - - - -
There needs to be a careful distinction made between two concepts concerning a numerical result in matlab. There is the way the number is displayed to the user and there is the binary number actually stored in the computer's memory, and these are not necessarily the same. (What you see is not always exactly what you get.) It is only the stored number that is used for computations, with the displayed value being provided purely for the benefit of the user.
In the case of a number entered as 9.7, the computer will actually store this binary number:
1001.1011001100110011001100110011001100110011001100110
It is the binary double precision's closest approximation to 9.7 and as you can see from the repeating pattern, no matter how many bits we were to provide the computer it would always be in error by some non-zero amount with the pattern continuing out indefinitely far.
As for how such a number is displayed to the user, that all depends on how the display format is set. With 'format short' it appears as:
9.7000
With (my) 'format long' it is:
9.70000000000000
With fprintf('%18.16f\n',9.7) it is:
9.6999999999999993
With 'format hex' it is:
4023666666666666
This last, (in IEEE 754 hexadecimal format,) is the only one that gives the true value of the binary quantity that is stored.
If as you stated earlier the number is the result of some computation, it is quite likely that it will have suffered a few bits worth of round off error at the low end, in which case the last three formats might be slightly different than above. In the case of the 'format short' if the correct answer is 9.7 this format would continue to give 9.7000 unless some very large errors had occurred in the computation. On the other hand, if the exact answer is supposed to be, say, 9.7000125, then the format short would continue to give you what would then be an erroneous 9.7000 .
I hope that somewhere in the above meandering discussion there lies an answer to your question about how to "convert" the output to match another value - or rather, an explanation for why it would be almost impossible for such a conversion attempt to always succeed. I think you will have to give up the notion of always getting exact matches with numbers that have undergone different procedures and settle for only approximate matches. This is inherent in the very nature of numerical computations performed by digital computers.
Roger Stafford
|