Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Problem in matlab IEE float

Subject: Problem in matlab IEE float

From: Kaze

Date: 20 Jan, 2010 19:41:07

Message: 1 of 20

Hello,
I need to solve a problem in matlab. Here is what I must do:

Let F, set of all numbers in IEEE floating point, except NaN and inf, whith 7ff (in hexadecimal) exponent moved and numbers not normalized with superscript moved 000 (in hexadecimal)

I realy don't know how should I start.

After this I have to answer:

1. How many elements F has
2. What proportion of elements of F are in the interval [1, 2)?
3. What proportion of elements of F are in the interval [1/64, 1/32) ?
4. I need to determine by random selection the proportion of elements of F which satisfy the logical relationship: x*(1/x) == 1

Can someone please help me here :( I need this to be done by monday .

Regards.

Subject: Problem in matlab IEE float

From: James Tursa

Date: 20 Jan, 2010 20:18:05

Message: 2 of 20

"Kaze " <raicabogdan@gmail.com> wrote in message <hj7m8j$23m$1@fred.mathworks.com>...
> Hello,
> I need to solve a problem in matlab. Here is what I must do:
>
> Let F, set of all numbers in IEEE floating point, except NaN and inf, whith 7ff (in hexadecimal) exponent moved and numbers not normalized with superscript moved 000 (in hexadecimal)
>
> I realy don't know how should I start.
>
> After this I have to answer:
>
> 1. How many elements F has
> 2. What proportion of elements of F are in the interval [1, 2)?
> 3. What proportion of elements of F are in the interval [1/64, 1/32) ?
> 4. I need to determine by random selection the proportion of elements of F which satisfy the logical relationship: x*(1/x) == 1
>
> Can someone please help me here :( I need this to be done by monday .
>
> Regards.

Obviously homework, very little of which has to do with MATLAB. What have you done on this so far? Any ideas?

I will get you started. Think of the IEEE floating point (I presume double precision since you use the value 7ff for the exponent) as a 64-bit integer. And I presume you are not considering denormalized numbers. Then just count the integers that satisfy your criteria. (1) should be pretty easy to answer. For (2) and (3), just figure out what exponent bit patterns match the criteria and then the answer should easily follow. For (4), write some code to randomly pick numbers (this will be a bit tricky) from F and then test the condition.

James Tursa

Subject: Problem in matlab IEE float

From: Kaze

Date: 20 Jan, 2010 21:24:05

Message: 3 of 20

> Obviously homework, very little of which has to do with MATLAB. What have you done on this so far? Any ideas?
>
> I will get you started. Think of the IEEE floating point (I presume double precision since you use the value 7ff for the exponent) as a 64-bit integer. And I presume you are not considering denormalized numbers. Then just count the integers that satisfy your criteria. (1) should be pretty easy to answer. For (2) and (3), just figure out what exponent bit patterns match the criteria and then the answer should easily follow. For (4), write some code to randomly pick numbers (this will be a bit tricky) from F and then test the condition.
>
> James Tursa

Hello James,

Yes this a homework, the problem is that I'm real beginer in matlab. I've read about IEEE floating point in readme and I understand what they are but how should I write them in matlab ... I don't know.

I have done some simple functions in matlab but now I'm over my head :(

Regards.

Subject: Problem in matlab IEE float

From: James Tursa

Date: 21 Jan, 2010 00:35:06

Message: 4 of 20

"Kaze " <raicabogdan@gmail.com> wrote in message <hj7s9l$19n$1@fred.mathworks.com>...
> > Obviously homework, very little of which has to do with MATLAB. What have you done on this so far? Any ideas?
> >
> > I will get you started. Think of the IEEE floating point (I presume double precision since you use the value 7ff for the exponent) as a 64-bit integer. And I presume you are not considering denormalized numbers. Then just count the integers that satisfy your criteria. (1) should be pretty easy to answer. For (2) and (3), just figure out what exponent bit patterns match the criteria and then the answer should easily follow. For (4), write some code to randomly pick numbers (this will be a bit tricky) from F and then test the condition.
> >
> > James Tursa
>
> Hello James,
>
> Yes this a homework, the problem is that I'm real beginer in matlab. I've read about IEEE floating point in readme and I understand what they are but how should I write them in matlab ... I don't know.
>
> I have done some simple functions in matlab but now I'm over my head :(
>
> Regards.

OK, I think you are in trouble for Monday. Honest opinion. But let's walk through the simple question #1, how many different IEEE double floating point numbers are there, excluding NaN, Inf, and denormalized? The 64-bit pattern has 1 sign bit, 11 exponent bits, and 52 mantissa bits. All of the possible 64-bit patterns are in the IEEE floating point model, so there are 2^64 different bit patterns in the model. Now lets throw out the NaN and Inf and denormalized. For NaN and Inf, the exponent is always all bits set, or leading bits of 7ff. So 11 bits have to be set to this, and the other 1 + 52 = 53 bits can be set to anything. The total number of bit patterns for this is then 2^53. For the denormalized, the exponent has to be 000, so again the total number of denormalized is (almost) 2^53. The reason I say almost is that the two bit patterns for +0 and -0 are not strictly part of the
denormlized set. So, the total number of floating point bit patterns in IEEE double excluding NaN and Inf and denormalized is:

    2^64 - 2^53 - 2^53 + 2

To get the same answer with MATLAB, one could do this:

>> ieeemax = typecast(realmax,'int64')
ieeemax =
  9218868437227405311
>> ieeemin = typecast(realmin,'int64')
ieeemin =
     4503599627370496
>> 2*(double(ieeemax)-double(ieeemin))
ans =
    1.842872967520007e+019
>> 2^64 - 2^53 - 2^53 + 2
ans =
    1.842872967520007e+019

I will let you think about why the two methods give the same answer.

Now you can do a similar analysis for questions #2 and #3. Just figure out how many different exponent bit patterns satisfy the requirement (should be pretty easy if you follow the analysis I show above) and then multiply by 2^52 (number of different mantissa bit patterns).

For question #4, you need to figure out a way to generate a random bit pattern that is uniform across F. I will let you work on that one. If you need help then post your code and we can talk about it.

James Tursa

Subject: Problem in matlab IEE float

From: James Tursa

Date: 21 Jan, 2010 02:58:05

Message: 5 of 20

"James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <hj87fq$lr8$1@fred.mathworks.com>...
> "Kaze " <raicabogdan@gmail.com> wrote in message <hj7s9l$19n$1@fred.mathworks.com>...
> > > Obviously homework, very little of which has to do with MATLAB. What have you done on this so far? Any ideas?
> > >
> > > I will get you started. Think of the IEEE floating point (I presume double precision since you use the value 7ff for the exponent) as a 64-bit integer. And I presume you are not considering denormalized numbers. Then just count the integers that satisfy your criteria. (1) should be pretty easy to answer. For (2) and (3), just figure out what exponent bit patterns match the criteria and then the answer should easily follow. For (4), write some code to randomly pick numbers (this will be a bit tricky) from F and then test the condition.
> > >
> > > James Tursa
> >
> > Hello James,
> >
> > Yes this a homework, the problem is that I'm real beginer in matlab. I've read about IEEE floating point in readme and I understand what they are but how should I write them in matlab ... I don't know.
> >
> > I have done some simple functions in matlab but now I'm over my head :(
> >
> > Regards.
>
> OK, I think you are in trouble for Monday. Honest opinion. But let's walk through the simple question #1, how many different IEEE double floating point numbers are there, excluding NaN, Inf, and denormalized? The 64-bit pattern has 1 sign bit, 11 exponent bits, and 52 mantissa bits. All of the possible 64-bit patterns are in the IEEE floating point model, so there are 2^64 different bit patterns in the model. Now lets throw out the NaN and Inf and denormalized. For NaN and Inf, the exponent is always all bits set, or leading bits of 7ff. So 11 bits have to be set to this, and the other 1 + 52 = 53 bits can be set to anything. The total number of bit patterns for this is then 2^53. For the denormalized, the exponent has to be 000, so again the total number of denormalized is (almost) 2^53. The reason I say almost is that the two bit patterns for +0 and -0 are not strictly part of the
> denormlized set. So, the total number of floating point bit patterns in IEEE double excluding NaN and Inf and denormalized is:
>
> 2^64 - 2^53 - 2^53 + 2
>
> To get the same answer with MATLAB, one could do this:
>
> >> ieeemax = typecast(realmax,'int64')
> ieeemax =
> 9218868437227405311
> >> ieeemin = typecast(realmin,'int64')
> ieeemin =
> 4503599627370496
> >> 2*(double(ieeemax)-double(ieeemin))
> ans =
> 1.842872967520007e+019
> >> 2^64 - 2^53 - 2^53 + 2
> ans =
> 1.842872967520007e+019
>
> I will let you think about why the two methods give the same answer.
>
> Now you can do a similar analysis for questions #2 and #3. Just figure out how many different exponent bit patterns satisfy the requirement (should be pretty easy if you follow the analysis I show above) and then multiply by 2^52 (number of different mantissa bit patterns).
>
> For question #4, you need to figure out a way to generate a random bit pattern that is uniform across F. I will let you work on that one. If you need help then post your code and we can talk about it.
>
> James Tursa

P.S. If you want to get the exact number down to the last digit, you can download the num2vpi and vpi submissions from the FEX and do this:

>> ieeemax = typecast(realmax,'int64')
ieeemax =
  9218868437227405311
>> ieeemin = typecast(realmin,'int64')
ieeemin =
     4503599627370496
>> vieeemax = num2vpi(ieeemax)
vieeemax =
   9218868437227405311
>> vieeemin = num2vpi(ieeemin)
vieeemin =
   4503599627370496
>> vtwo = vpi(2)
vtwo =
   2
>> vtwo*(vieeemax - vieeemin + vpi(1)) + vtwo
ans =
   18428729675200069634
>> vtwo^64 - vtwo^53 - vtwo^53 + vtwo
ans =
   18428729675200069634

num2vpi is a utility I wrote that is needed to convert the int64 type into vpi and it can be found here:

http://www.mathworks.com/matlabcentral/fileexchange/25643-num2vpi-converts-input-exactly-into-vpi

vpi is a variable precision integer class that John D'Errico wrote for doing exact integer arithmetic and it can be found here:

http://www.mathworks.com/matlabcentral/fileexchange/22725-variable-precision-integer-arithmetic

James Tursa

Subject: Problem in matlab IEE float

From: Kaze

Date: 23 Jan, 2010 18:04:02

Message: 6 of 20

Thanks for all the help.

I don't think I can use num2vpi because I can't install anything on the computer since I don't have the permissions.

I think I need another metode to get the numbers.

A friend help me solve the last problem:
*determine by random selection the proportion of elements of F which satisfy the logical relationship: x*(1/x) == 1

>> x=rand(1,n)*realmin;
>> sum(x.*(1./x)==1)

ans =

     1

Now for the second problem, I think I can use this:
*What proportion of elements of F are in the interval [1, 2)?

>> x=rand(1,2)*realmin

x =

  1.0e-308 *

    0.1541 0.4837

Can you please tell me If I did something wrong and help me with the other two ?

Kind regards.

Subject: Problem in matlab IEE float

From: Walter Roberson

Date: 23 Jan, 2010 18:10:50

Message: 7 of 20

Kaze wrote:
> *What proportion of elements of F are in the interval [1, 2)?
>
>>> x=rand(1,2)*realmin

rand(1,2) does not select values in the range [1,2); instead it creates
a 1 x 2 matrix of random numbers.

Subject: Problem in matlab IEE float

From: James Tursa

Date: 23 Jan, 2010 21:38:03

Message: 8 of 20

"Kaze " <raicabogdan@gmail.com> wrote in message <hjfdmi$30m$1@fred.mathworks.com>...
>
> I don't think I can use num2vpi because I can't install anything on the computer since I don't have the permissions.

Well, unless you don't have permission to download anything I don't see why this would be an issue. These are just ASCII text files, m-files and C source code. You do have permission to create files, don't you? How would you ever learn MATLAB if you didn't have permission to create files?

> I think I need another metode to get the numbers.

Hmmm. What is wrong with the methods I showed you using direct counting or typecast? Neither one of them require you to download anything.

> A friend help me solve the last problem:
> *determine by random selection the proportion of elements of F which satisfy the logical relationship: x*(1/x) == 1
>
> >> x=rand(1,n)*realmin;

Amazing. You have managed to construct a vector x where absolutely *none* of the elements of x are in F. Exactly the opposite of the posed question.

> >> sum(x.*(1./x)==1)

Good ... except the x you are using is wrong.

> Now for the second problem, I think I can use this:
> *What proportion of elements of F are in the interval [1, 2)?
>
> >> x=rand(1,2)*realmin

A total misuse of rand to get numbers in the interval [1,2). You need to read the doc on rand, and then look *very closely* at the examples I gave you with the direct counting or with typecast. Once you understand either one of them the answer to this question and the other interval question will be very easy. rand has nothing to do with it ... you can get the exact answer analytically with either of the methods I showed you.

James Tursa

Subject: Problem in matlab IEE float

From: Kaze

Date: 24 Jan, 2010 12:27:03

Message: 9 of 20

Sorry James,

Please believe me that I'm over my head here and I'm not trying to ignore you or something like that.

I've download the source code of num2vpi and vpi and is working. Happy for that.

I'm not very sure if this is corect, but still :
2nd
>> ieeemin = typecast(1,'int64')

ieeemin =

  4607182418800017408

>> ieeemax = typecast(2,'int64')

ieeemax =

  4611686018427387904

>> 2*(double(ieeemax)-double(ieeemin))

ans =

  9.0072e+015

3rd
>> ieeemax = typecast((1/32),'int64')

ieeemax =

  4584664420663164928

>> ieeemin = typecast((1/64),'int64')

ieeemin =

  4580160821035794432

>> 2*(double(ieeemax)-double(ieeemin))

ans =

  9.0072e+015

If is not ok, what did I do wrong ?

Regards.

Subject: Problem in matlab IEE float

From: James Tursa

Date: 24 Jan, 2010 18:44:04

Message: 10 of 20

"Kaze " <raicabogdan@gmail.com> wrote in message <hjhean$88p$1@fred.mathworks.com>...
>
> I'm not very sure if this is corect, but still :
> 2nd
> >> ieeemin = typecast(1,'int64')
> ieeemin =
> 4607182418800017408
> >> ieeemax = typecast(2,'int64')
> ieeemax =
> 4611686018427387904
> >> 2*(double(ieeemax)-double(ieeemin))
> ans =
> 9.0072e+015
>
> 3rd
> >> ieeemax = typecast((1/32),'int64')
> ieeemax =
> 4584664420663164928
> >> ieeemin = typecast((1/64),'int64')
> ieeemin =
> 4580160821035794432
> >> 2*(double(ieeemax)-double(ieeemin))
> ans =
> 9.0072e+015
>
> If is not ok, what did I do wrong ?

Hmmmm ... well, now isn't that curious? You got the same answer for the number of elements of F in the interval [1,2) as in the interval [1/64,1/32). Does that seem right to you? Is that what you expect, or would you expect to get a different answer for these two questions?

James Tursa

Subject: Problem in matlab IEE float

From: Kaze

Date: 24 Jan, 2010 19:20:05

Message: 11 of 20

> Hmmmm ... well, now isn't that curious? You got the same answer for the number of elements of F in the interval [1,2) as in the interval [1/64,1/32). Does that seem right to you? Is that what you expect, or would you expect to get a different answer for these two questions?
>
> James Tursa

What I aspect is to understand what I'm doing and to have no errors. Of course the problem has to be solved corecly.

I get the same answer but I don't think is wrong.

Thank you again for the help, I learn more from you than from my teacher.

Kind regards.

Subject: Problem in matlab IEE float

From: Steven Lord

Date: 25 Jan, 2010 04:01:52

Message: 12 of 20


"Kaze " <raicabogdan@gmail.com> wrote in message
news:hj7m8j$23m$1@fred.mathworks.com...
> Hello,
> I need to solve a problem in matlab. Here is what I must do:
>
> Let F, set of all numbers in IEEE floating point, except NaN and inf,
> whith 7ff (in hexadecimal) exponent moved and numbers not normalized
> with superscript moved 000 (in hexadecimal)
>
> I realy don't know how should I start.
>
> After this I have to answer:
>
> 1. How many elements F has
> 2. What proportion of elements of F are in the interval [1, 2)?
> 3. What proportion of elements of F are in the interval [1/64, 1/32) ?
> 4. I need to determine by random selection the proportion of elements of F
> which satisfy the logical relationship: x*(1/x) == 1

The first three questions you can do without using MATLAB. Take a look at
this Cleve's Corner article for more information about IEEE floating point
numbers:

http://www.mathworks.com/company/newsletters/news_notes/pdf/Fall96Cleve.pdf

For question 4, two of the functions in the Data Type Conversion list of
functions will help you in the random selection.

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/f16-6011seg05.html#f16-52710

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: Problem in matlab IEE float

From: Kaze

Date: 25 Jan, 2010 06:20:21

Message: 13 of 20

"Steven Lord" <slord@mathworks.com> wrote in message <hjj53e$qaj$1@fred.mathworks.com>...
>
> "Kaze " <raicabogdan@gmail.com> wrote in message
> news:hj7m8j$23m$1@fred.mathworks.com...
> > Hello,
> > I need to solve a problem in matlab. Here is what I must do:
> >
> > Let F, set of all numbers in IEEE floating point, except NaN and inf,
> > whith 7ff (in hexadecimal) exponent moved and numbers not normalized
> > with superscript moved 000 (in hexadecimal)
> >
> > I realy don't know how should I start.
> >
> > After this I have to answer:
> >
> > 1. How many elements F has
> > 2. What proportion of elements of F are in the interval [1, 2)?
> > 3. What proportion of elements of F are in the interval [1/64, 1/32) ?
> > 4. I need to determine by random selection the proportion of elements of F
> > which satisfy the logical relationship: x*(1/x) == 1
>
> The first three questions you can do without using MATLAB. Take a look at
> this Cleve's Corner article for more information about IEEE floating point
> numbers:
>
> http://www.mathworks.com/company/newsletters/news_notes/pdf/Fall96Cleve.pdf
>
> For question 4, two of the functions in the Data Type Conversion list of
> functions will help you in the random selection.
>
> http://www.mathworks.com/access/helpdesk/help/techdoc/ref/f16-6011seg05.html#f16-52710
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
> Thank you, but this was needed to be solved in matlab.
I'm going to look in the second link you provided, but as I said, I'm a beginner so ... I don't realy know what to chose from there.

Kind regards

Subject: Problem in matlab IEE float

From: James Tursa

Date: 25 Jan, 2010 06:54:06

Message: 14 of 20

"Kaze " <raicabogdan@gmail.com> wrote in message <hji6h5$as3$1@fred.mathworks.com>...
> > Hmmmm ... well, now isn't that curious? You got the same answer for the number of elements of F in the interval [1,2) as in the interval [1/64,1/32). Does that seem right to you? Is that what you expect, or would you expect to get a different answer for these two questions?
> >
> > James Tursa
>
> What I aspect is to understand what I'm doing and to have no errors. Of course the problem has to be solved corecly.
>
> I get the same answer but I don't think is wrong.

Well, if you really think those results are correct then turn them in. Good luck.

James Tursa

Subject: Problem in matlab IEE float

From: Kaze

Date: 25 Jan, 2010 08:02:02

Message: 15 of 20

"James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <hjjf6e$8iu$1@fred.mathworks.com>...
> "Kaze " <raicabogdan@gmail.com> wrote in message <hji6h5$as3$1@fred.mathworks.com>...
> > > Hmmmm ... well, now isn't that curious? You got the same answer for the number of elements of F in the interval [1,2) as in the interval [1/64,1/32). Does that seem right to you? Is that what you expect, or would you expect to get a different answer for these two questions?
> > >
> > > James Tursa
> >
> > What I aspect is to understand what I'm doing and to have no errors. Of course the problem has to be solved corecly.
> >
> > I get the same answer but I don't think is wrong.
>
> Well, if you really think those results are correct then turn them in. Good luck.
>
> James Tursa

Thank you.

Regards.

Subject: Problem in matlab IEE float

From: James Tursa

Date: 26 Jan, 2010 02:44:03

Message: 16 of 20

"Kaze " <raicabogdan@gmail.com> wrote in message <hjjj5q$eg3$1@fred.mathworks.com>...
> "James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <hjjf6e$8iu$1@fred.mathworks.com>...
> > "Kaze " <raicabogdan@gmail.com> wrote in message <hji6h5$as3$1@fred.mathworks.com>...
> > > > Hmmmm ... well, now isn't that curious? You got the same answer for the number of elements of F in the interval [1,2) as in the interval [1/64,1/32). Does that seem right to you? Is that what you expect, or would you expect to get a different answer for these two questions?
> > > >
> > > > James Tursa
> > >
> > > What I aspect is to understand what I'm doing and to have no errors. Of course the problem has to be solved corecly.
> > >
> > > I get the same answer but I don't think is wrong.
> >
> > Well, if you really think those results are correct then turn them in. Good luck.
> >
> > James Tursa
>
> Thank you.
>
> Regards.

I would be interested in the answer that your professor has for #4. This question does not seem all that trivial to me. I can think of at least three ways of doing it (and have coded them up), but you either need some fancy code to generate the samples or make some assumptions about how the floating point processor behaves for different exponents to answer the question, neither of which I would expect a beginning student to understand. I am curious how your professor did it.

James Tursa

Subject: Problem in matlab IEE float

From: Derek O'Connor

Date: 26 Jan, 2010 07:30:21

Message: 17 of 20

"James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message
>
> I would be interested in the answer that your professor has for #4. This question does not seem all that trivial to me. I can think of at least three ways of doing it (and have coded them up), but you either need some fancy code to generate the samples or make some assumptions about how the floating point processor behaves for different exponents to answer the question, neither of which I would expect a beginning student to understand. I am curious how your professor did it.
>
> James Tursa

This question is not trivial. Here is Prof Alan Edelman's answer (1994):

http://www-math.mit.edu/%7Eedelman/homepage/papers/ieee.ps


Derek O'Connor

Subject: Problem in matlab IEE float

From: Kaze

Date: 26 Jan, 2010 08:28:05

Message: 18 of 20

"Derek O'Connor" <derekroconnor@eircom.net> wrote in message <hjm5md$hi3$1@fred.mathworks.com>...
> "James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message
> >
> > I would be interested in the answer that your professor has for #4. This question does not seem all that trivial to me. I can think of at least three ways of doing it (and have coded them up), but you either need some fancy code to generate the samples or make some assumptions about how the floating point processor behaves for different exponents to answer the question, neither of which I would expect a beginning student to understand. I am curious how your professor did it.
> >
> > James Tursa
>
> This question is not trivial. Here is Prof Alan Edelman's answer (1994):
>
> http://www-math.mit.edu/%7Eedelman/homepage/papers/ieee.ps
>
>
> Derek O'Connor

Hello,

He didn't show the last question to me. I was sure that I he would ask me why didn't I do the last questiong, but he only asked me about the first 3 and I was able to explain the functions typecast , realmax, realmin and why did I use them. Of course is thanks to you, you gave me a good explanation about the IEEE floating point without NaN and Inf.

Because he needed to listen to others about their problems, it didn't took more than 5 minutes to evaluate me. Maybe next time, I will ask him how would he aswer to the last question, because as you probably know, I didn't do the last one.
My problem was found by me today in a book of a Universty profesor in Romania, state Cluj. In his book, was my questions, if you are curiouse I can call or email the profesor and try to make him aswer the last question.

Thank again.

Subject: Problem in matlab IEE float

From: James Tursa

Date: 26 Jan, 2010 09:00:05

Message: 19 of 20

"Derek O'Connor" <derekroconnor@eircom.net> wrote in message <hjm5md$hi3$1@fred.mathworks.com>...
> "James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message
> >
> > I would be interested in the answer that your professor has for #4. This question does not seem all that trivial to me. I can think of at least three ways of doing it (and have coded them up), but you either need some fancy code to generate the samples or make some assumptions about how the floating point processor behaves for different exponents to answer the question, neither of which I would expect a beginning student to understand. I am curious how your professor did it.
> >
> > James Tursa
>
> This question is not trivial. Here is Prof Alan Edelman's answer (1994):
>
> http://www-math.mit.edu/%7Eedelman/homepage/papers/ieee.ps
>
>
> Derek O'Connor

Don't have any s/w to open a ps file on the computer I am on at the moment. Will try at work tomorrow. A couple of my solutions were as follows:

% 1st method
% Direct method by generating random 64-bit integer bit patterns (as two
% 32-bit pieces) and typecasting them as double precision bit patterns.
x = uint32(floor(rand(n,1)*(2^32))); % Generate uniformly distributed 32-bit integers
y = typecast(x,'double'); % Typecast them as IEEE double precision
z = isnan(y) | isinf(y) | (y < realmin); % Figure out which ones are NaN, Inf, and denorm
t = y(~z); % Get rid of the NaN, Inf, and denorm
f = sum(t.*(1./t)==1) / numel(t); % Fraction of bit patterns satisfying the relationship
disp(f);

% 2nd method
% Indirect method assumes that rand generates uniformly distributed bit patterns
% down to the least significant bit after doing the +1 operation. Also assumes
% that the floating point processor has the same rounding algorithm regardless
% of the value of the exponent (probably a safe assumption, but still it is an
% assumption). By looking only at the bit patterns for a fixed binary exponent
% value (in this case the numbers between 1 and 2), we can extrapolate the
% result for all exponent bit patterns (an assumption).
x = rand(n,1)+1; % Generate uniformly distributed numbers in [1,2)
f = sum(x.*(1./x)==1) / n; % Fraction of bit patterns satisfying the relationship
disp(f);

For critical work, I wouldn't rely on the 2nd method because of the assumptions made, but as it turns out for my PC the assumptions look to be correct since I get essentially the same answer as the first method which I trust to be uniformly distributed across F. The results for n=10000000:

   0.84612180788734 % Method 1
   0.84646010000000 % Method 2

I would hardly expect a beginning student to come up with this, which is why I was curious what the professor expected them to do. Thanks for the link ... hopefully I will be able to look at it tomorrow.

James Tursa

Subject: Problem in matlab IEE float

From: James Tursa

Date: 26 Jan, 2010 18:04:02

Message: 20 of 20

"Derek O'Connor" <derekroconnor@eircom.net> wrote in message <hjm5md$hi3$1@fred.mathworks.com>...
>
> This question is not trivial. Here is Prof Alan Edelman's answer (1994):
>
> http://www-math.mit.edu/%7Eedelman/homepage/papers/ieee.ps

Couldn't even open it in MS Visio, which is supposed to handle ps files. So haven't been able to look at this yet.

James Tursa

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us