From: Akshay on 16 Jul 2010 16:49 Hi there, I am trying to convert from Hex to a floating point number. For Hex to IEEE Double precision number I use the hex2num function and wanted to know which function can be used to convert from Hex to a float. Thanks. Akshay "Brian.Farrelly" <Brian.Farrelly(a)nho.hydro.com> wrote in message <3C3AB961.306D101A(a)nho.hydro.com>... > Paul Skoczylas wrote: > > > > What I'm still waiting for is a function which will display, to a > > user-sepcified number of digits, a decimal representation of the actual > > number that is stored in a variable. > > > > For example, we all know that IEEE double precision is incapable of > > accurately storing the number 0.1. When you think you're storing 0.1 in a > > variable, Matlab actually stores 3fb999999999999a, which you can see is > > truncated (much as you can't write 1/3 in decimal without truncating it: > > 0.33333...). What I'd like to know is what 3fb999999999999a actually > > represents. It's not exactly 0.1 (heck, it probably can't be exactly > > written in decimal either), but to 20, 50 or 100 digits, what is it's > > decimal representation? > > > > I looked at this a while back and I think it can be done fairly easily with > > the symbolic toolbox, but I don't have that, and besides it would be more > > valuable if it didn't need any add-on toolboxes. > > > Paul, > > your particular example is fairly easy but the general case is > trickier without the toolbox. I once made a multiprecision calculation > in basic doing the arithmetic directly on the ascii strings. I will > now probably waste some time doing a similar thing in Matlab! > > To get back to the particular case. > > Using log2 we find 0.1 = 0.8 * 2^(-3). Well, we could have managed > that without log2. > > Normalise the fractional part so that 0.1 = 1.6 * 2^(-4). > > The 1 is in the hidden bit of the IEEE representation, the exponent > is taken care of, so we need only worry about the 0.6. > > The hex sequence 999999999... would be the infinite precision version > of 9/16 + 9/16^2 + ... or (9/16)*(1 + 1/16 + 1/16 +...) and summing > the series we get (9/16)*1/(1-1/16) = 0.6 as required. > > What we have is the rounded version which represents > > (9/16)*(1 + 1/16 + ... + 1/16^12) + 1/16^13 > > the last term coming from the hex a being 10 instead of 9. > > Now subtract the infinite series from the rounded version to get the > error in the 0.6 which is > > 1/16^13-(9/16)*(1/16^13 + 1/16^14 + ...) > > which simplifies to (1/16^13)*(2/5) > > > So 0.1 is represented as (1.6 + 2/(5*16^13))/16 = 0.1+0.4/16^14 = > 0.100000000000000005551115123125783 approximately > > If I am not mistaken this could be written exactly a decimal since > the denominator contains only 2 and 5 as factors and these are > factors of 10. > > Brian > > > > > -- > mailto:Brian.Farrelly(a)nho.hydro.com Norsk Hydro Research Centre > phone +47 55 99 68 74 ((( Postboks 7190 > fax +47 55 99 69 70 2oooS N-5020 Bergen > home +47 55 13 78 49 HYDRO Norway
From: Walter Roberson on 16 Jul 2010 17:10 Akshay wrote: > I am trying to convert from Hex to a floating point number. For Hex to > IEEE Double precision number I use the hex2num function and wanted to > know which function can be used to convert from Hex to a float. Convert the hex to uint8 and then typecast() the uint8 to single. (Provided that the hex represents a IEEE 754 float. You may also have to change the order of some of the bytes.)
From: us on 16 Jul 2010 17:23 "Akshay " <akshay_bandiwdekar(a)trimble.com> wrote in message <i1qgk0$3jl$1(a)fred.mathworks.com>... > Hi there, > > I am trying to convert from Hex to a floating point number. For Hex to IEEE Double precision number I use the hex2num function and wanted to know which function can be used to convert from Hex to a float. > > Thanks. > Akshay one of the solutions - if(f) by FLOAT you mean a SINGLE precision data... v=pi; % <- a DOUBLE by default... hv=num2hex(v) % hv = 400921fb54442d18 dv=hex2num(hv); isequal(dv,pi) % ans = 1 sv=single(hex2num(hv)); isequal(sv,single(pi)) % ans = 1 num2hex(sv) % ans = 40490fdb us
From: Walter Roberson on 16 Jul 2010 17:35 us wrote: > one of the solutions > - if(f) by FLOAT you mean a SINGLE precision data... > > v=pi; % <- a DOUBLE by default... > hv=num2hex(v) > % hv = 400921fb54442d18 > dv=hex2num(hv); > isequal(dv,pi) > % ans = 1 > sv=single(hex2num(hv)); > isequal(sv,single(pi)) > % ans = 1 > num2hex(sv) > % ans = 40490fdb num2hex() applied to a single will print out the hex equivalent of the single, but hex2num() applied to a sequence of hex digits shorter than would be the case for a double, implicitly pads with hex 0 to reach the size for a double and converts that. There is no option available to num2hex to tell it to interpret the hex as a single. This does make a difference as the representation of a single is not just a shorter version of the representation of a double.
From: James Tursa on 16 Jul 2010 18:11
"Paul Skoczylas" <pauls(a)cfertech.com> wrote in message <DhK_7.17$M5.620(a)jekyl.ab.tac.net>... > "Peter Boettcher" <boettcher(a)ll.mit.edu> wrote > > > > MATLAB presumeably calls the printf functions provided by the C > > library. I am using linux with a recent GNU libc, so I would expect > > that is the difference. Good to know this is system-dependent. > > Well, I don't think it's so good. Of course, my system gives less "good" > results than yours... > > I would like Matlab (or any other program that is built for different > platforms) to give the same results on any platform. I suppose, however, > that the different fprintf's we've seen here all meet the minimum > requirement in the ANSI C or IEEE (or?) standard governing such things. > It's just that the linux version goes that extra mile... > > So I'd like to reiterate my request for a Matlab function (which does not > require the symbolic toolbox) which will print out, to a number of digits > given by the user, the decimal representation of the actual number stored in > a particular variable. > > I wonder if Peter's fprintf works in all possible cases, or if the 0.1 case > is a nice one... If it does work, it shouldn't be too hard for TMW to > incorporate that version in all copies of Matlab... > > -Paul > > > Looks like this thread has been resurrected, so I will add this reply. See this FEX submission to get the exact decimal equivalent of any single or double number: http://www.mathworks.com/matlabcentral/fileexchange/22239-num2strexact-exact-version-of-num2str James Tursa |