Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please write code in MATLAB language Write a function with header [IEEE] = myDec

ID: 3829001 • Letter: P

Question

Please write code in MATLAB language Write a function with header [IEEE] = myDec2 IEEE (d), where d is a number in decimal and IEEE a 1 times 32 array of ones and zeros representing the 32-bit IEEE754 closest to d. You can assume that d will not cause an overflow for 32-bit IEEE numbers. Test Cases: >> IEEE= myDec2IEEE(15.18484199625) IEEE = 0 1 0 0 0 0 0 1 0 1 1 1 0 0 1 0 1 1 1 1 0 1 0 1 0 0 0 1 1 1 0 1 >> IEEE = myDec2IEEE(-309.141740) IEEE = 1 1 0 0 0 0 1 1 1 0 0 1 10 1 0 1 0 0 1 0 0 1 0 0 0 1 0 0 1 0 1 >> IEEE = myDec2IEEE(-25252) IEEE = 1 1 0 0 0 1 1 0 1 1 0 0 0 1 0 1 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0

Explanation / Answer

%%Code

function ieee = myDec2IEEE(dec)
    ieee = zeros(1,32);
    if dec < 0 %set the sign bit and make the number +ve
        ieee(1) = 1;
        dec = -dec;
    end
  
    fraction = zeros(1,24); %teporary array to srore the number in normalized form wih leading 1
    whole = floor(dec); %extract the whole number
    fac = dec - whole; %extract the part after point
    if whole > 0 %if whole number is more than zero, exponent is always more than 0 in normalized form
        len = floor(log2(whole))+1; %we find the exponet
    else
        len = (ceil(log2(1/fac)-1)) * -1 ; %if whone number is 0, exponent is -ve
    end
    for i = len:-1:1 %we now fill the whole number part
        fraction(i) = mod(whole,2);
        whole = floor(whole/2);
    end
    for i = len:24 %then fill the part for fraction
        if fac >= 1
            fraction(i) = 1;
            fac = fac - 1;
        end
        fac = fac * 2;
    end
    exponent = len -1 + 127; %fix the exponet to meet ieee standards
    for i=9:-1:2 %convert exponet to binaary
        ieee(i) = mod(exponent,2);
        exponent = floor(exponent/2);
    end
    ieee(1,10:32) = fraction(1,2:24); %copy fraction arry leaving the leading 1

end

%%end of code

I tried my best to keep the code as simple as possible. I f incase you are still facing difficulty with the code, please feel free to comment below. I shall be glad to help you with the code.