Prev: Differential Equations
Next: figure sizing issue
From: Kaze on 20 Jan 2010 14:41 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.
From: James Tursa on 20 Jan 2010 15:18 "Kaze " <raicabogdan(a)gmail.com> wrote in message <hj7m8j$23m$1(a)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
From: Kaze on 20 Jan 2010 16:24 > 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.
From: James Tursa on 20 Jan 2010 19:35 "Kaze " <raicabogdan(a)gmail.com> wrote in message <hj7s9l$19n$1(a)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
From: James Tursa on 20 Jan 2010 21:58
"James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <hj87fq$lr8$1(a)fred.mathworks.com>... > "Kaze " <raicabogdan(a)gmail.com> wrote in message <hj7s9l$19n$1(a)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 |