Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Matlab code function float=decimal_to_bin_str(a,n,k) % a: decimal expression of the number to be converted. % n: the number of available bits % k: the desired

Matlab code

function float=decimal_to_bin_str(a,n,k)

% a: decimal expression of the number to be converted. % n: the number of available bits % k: the desired precision

% Determine if there will be overflow or underflow.

if ((abs(a)>0) && (abs(a)2^(2^(n-k-2)-1)*(2-2^(-k))); display('OVERFLOW') return end;

% Determine the signum: s='0'; if a

w = floor(abs(a)); f = abs(a) - floor(abs(a));

% Convert the whole part of a to binary. We initialize the % empty string to store the binary digits of the whole part of a.

wb = '';

% The method for converting decimal integers to binary requires % collecting the remainders of iterative division by 2 until % enough divisions have occurred so that the resulting quotient % is 0.

q=1; while q>0; q=floor(w/2); r=w-2*q; wb=strcat(num2str(r),wb); w=q; end

% We desire to collect the digits of the mantissa, but to do so % we must detect where the first leading 1 is located. In the % event that the whole part of a is nonzero, the exponent is % detected by counting the number of digits in the binary % representation of the whole part of a.

exp=length(wb)-1;

% If the whole part of a is zero, then we must determine the % number of leading zeros before the first leading 1.

% We initialize the empty string to store binary digits of the % fractional part of a.

fb = '';

% Because the binary expansion of a finite decimal fraction may % be infinite, we must limit the number of digits generated to % avoid an endless loop. We stop this process once we have % collected k digits for the mantissa, which we call m.

if wb(1)=='0'; exp=floor(log2(f)); f=2^-exp*f-floor(2^-exp*f); d=0; while length(fb)

% To determine the mantissa when the whole part of a is nonzero, % we concatenate the string wb with enough digits to provide the % desired precision.

if wb(1)=='1'; m=wb(2:length(wb)); d=0; while length(m)

% Add the bias to the exponent.

exp = exp + 2^(n-k-2)-1;

% Convert the exponent to a binary number. We initialize the % empty string to store the binary digits of the exponent.

e='';

q=1; while q>0; q=floor(exp/2); r=exp-2*q; e=strcat(num2str(r),e); exp=q; end

% Place sufficiently many leading 0s to the left of the binary % representation of the exponent, so that the number of binary % digits of the exponent is equal to n-k-1.

while length(e)

% Concatenate s, e, and m to generate the desired floating point % representation.

float=strcat(s,e,m);

image text in transcribed

(a) If the leading binary digit of the whole part of a is 0, then there is a procedure performed in lines 68-78 of the script. If the leading binary digit is 1, rather than 0, then the script performs a different procedure (lines 84-92). What do each of these procedures do? How do these procedures differ? Why is it necessary to have two separate procedures for each of these cases? (b) Suppose that n bits are used to store a floating point binary number with k digits of precision. Determine (with explicit calculation) the maximum value of the exponent, and (recalling that the bias is taken to be half of the maximum value of the exponent, rounded down) explain how line 96 correctly accounts for the bias. (a) If the leading binary digit of the whole part of a is 0, then there is a procedure performed in lines 68-78 of the script. If the leading binary digit is 1, rather than 0, then the script performs a different procedure (lines 84-92). What do each of these procedures do? How do these procedures differ? Why is it necessary to have two separate procedures for each of these cases? (b) Suppose that n bits are used to store a floating point binary number with k digits of precision. Determine (with explicit calculation) the maximum value of the exponent, and (recalling that the bias is taken to be half of the maximum value of the exponent, rounded down) explain how line 96 correctly accounts for the bias<-k-1>

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

what is the percent of sales formula

Answered: 1 week ago