gistlib
main.m% First, convert 0.852 to binary bin_852 = dec2bin(0.852 * 2^t); bin_852 = strcat('0.', bin_852); disp("Binary Representation: " + bin_852); % Determine the exponent exp = 0; for i = 1:length(bin_852) if bin_852(i) == '.' continue elseif bin_852(i) == '1' exp = t-i+1; break end end disp("Exponent: " + exp) % Determine the fraction frac = strrep(bin_852, '.', ''); frac = frac(frac ~= '0'); frac = frac(1:5); while length(frac) < 5 frac = strcat(frac, '0'); end disp("Fraction: " + frac); % Convert exponent to binary and add leading 0's exp = exp + (2^(k+1)); bin_exp = dec2bin(exp); while length(bin_exp) < 7 bin_exp = strcat('0', bin_exp); end disp("Binary Exponent: " + bin_exp); % Combine exponent and fraction result = strcat('0', bin_exp, frac); disp("Result: " + result); 829 chars38 lines
% First, convert 0.852 to binary bin_852 = dec2bin(0.852 * 2^t); bin_852 = strcat('0.', bin_852); disp("Binary Representation: " + bin_852); % Determine the exponent exp = 0; for i = 1:length(bin_852) if bin_852(i) == '.' continue elseif bin_852(i) == '1' exp = t-i+1; break end end disp("Exponent: " + exp) % Determine the fraction frac = strrep(bin_852, '.', ''); frac = frac(frac ~= '0'); frac = frac(1:5); while length(frac) < 5 frac = strcat(frac, '0'); end disp("Fraction: " + frac); % Convert exponent to binary and add leading 0's exp = exp + (2^(k+1)); bin_exp = dec2bin(exp); while length(bin_exp) < 7 bin_exp = strcat('0', bin_exp); end disp("Binary Exponent: " + bin_exp); % Combine exponent and fraction result = strcat('0', bin_exp, frac); disp("Result: " + result);
The output of this code is:
main.mBinary Representation: 0.11011100 Exponent: 3 Fraction: 11011 Binary Exponent: 0100000 Result: 011111011 105 chars6 lines
Binary Representation: 0.11011100 Exponent: 3 Fraction: 11011 Binary Exponent: 0100000 Result: 011111011
Therefore, the normalized, regularly rounded representation of 0.852 is:
f=[a=2,k−=−5,k+=5,t=6]
gistlibby LogSnag