I would like to convert a hex value to the IEEE single precision (32
bits) floating-point number it represents.
i.e. 449A5225=1234.567
Thanks in advance for your help.
Kevin
i.e. - 449A5225=1234.567
-Monica
"Kevin Bergman" <kevin....@boge-na.com> wrote in message
news:eea9...@WebX.raydaftYaTP...
I have functions which can do this, namely
hex2float (hot off the press!) which converts a single hex string
of 8 characters to a 32 bit float contained in a uint32.
float2num which can convert a matrix of 32 bit floats contained in
a uint32 matrix and convert them to double for display and
calculation
num2float which takes a matrix of doubles and converts them to
32 bit floats contained in a uint32 matrix.
Thus
>> format hex; b=num2float(1234.567)
449a5225
>> n=float2num(hex2float('449a5225')) ; sprintf('%8.3f',n)
1234.567
and of course
>> nn=float2num(b) ; sprintf('%8.3f',nn)
1234.567
Using format doesn't display the whole number and format long displays
too much so that you see the roundoff error of the conversion!
I'm sending these to Kevin but if anyone else needs them let me know.
Brian
--
mailto:Brian.F...@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
HEX2DEC regards the string as representing the integer 1150964261.
> I have functions which can do this, namely
>
> hex2float (hot off the press!) which converts a single hex string
> of 8 characters to a 32 bit float contained in a uint32.
>
> float2num which can convert a matrix of 32 bit floats contained in
> a uint32 matrix and convert them to double for display and
> calculation
>
> num2float which takes a matrix of doubles and converts them to
> 32 bit floats contained in a uint32 matrix.
>
>
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
> HEX2DEC
Re-read the post more carefully and try again.
You might have to roll your own using the source to hex2num and:
http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html
--
Peter Boettcher
MIT Lincoln Laboratory
boet...@ll.mit.edu
(781) 981-5275
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.
> 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.
>
>
> 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.
>
> [snip]
>
> So 0.1 is represented as (1.6 + 2/(5*16^13))/16 = 0.1+0.4/16^14 =
> 0.100000000000000005551115123125783 approximately
How about fprintf?
>> fprintf('%.56f\n', .1)
0.10000000000000000555111512312578270211815834045410156250
On my computer (Win2K, V5.3):
» fprintf('%.56f\n',0.1)
0.10000000000000001000000000000000000000000000000000000000
It shows that there's a difference between 0.1 and how the computer
represents it, but it's not very good at showing what the difference is.
Why is your fprintf so much better than mine?
-Paul
Very nice.I always forget what you can do with fprintf.
I was pretty close though!
Paul,
I get the same as you on a PC, Win NT, version 6.0.0.88, R12.
On a Unix machine I get the right answer. I guess Peter is
using Unix.
Is this a bug or a feature?
Thanks,
--ws
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
Peter Boettcher wrote:
>
> wsun <ws...@ford.com> writes:
>
> > I got what Paul had on my NT4SP5, Matlab R12.1 using fprintf. Same with
> > my DEC unix machine. Why Peter has different fprintf?
> >
> > Thanks,
> > --ws
> >
> > Paul Skoczylas wrote:
> >>
> >> "Peter Boettcher" <boet...@ll.mit.edu> wrote:>
> >> > How about fprintf?
> >> >
> >> > >> fprintf('%.56f\n', .1)
> >> > 0.10000000000000000555111512312578270211815834045410156250
> >>
> >> On my computer (Win2K, V5.3):
> >>
> >> » fprintf('%.56f\n',0.1)
> >> 0.10000000000000001000000000000000000000000000000000000000
> >>
> >> It shows that there's a difference between 0.1 and how the computer
> >> represents it, but it's not very good at showing what the difference is.
> >> Why is your fprintf so much better than mine?
>
> MATLAB presumeably calls the printf functions provided by the
> C library. I am using linux with a recent GNU libc, so I would expect
^^^^^^^^^^^^
What C library? Doesn't TMW pre-compile these functions and ship with
products? It should
be their responsibility to have same results on such functions, or
inform us if they are
may be platform-dependent.
Thanks,
--ws
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.F...@nho.hydro.com> wrote in message <3C3AB961...@nho.hydro.com>...
> 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.)
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
> 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.
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
of course...
but i showed something different...
see also the results of ISEQUAL...
us