Last active
February 7, 2023 08:24
-
-
Save SeroviICAI/661a3642ece68dc91e53e748cb210abf to your computer and use it in GitHub Desktop.
The function "decimal_to_IEEE754" is a Matlab function that converts a decimal number to its equivalent representation in the IEEE 754 format (double precision). It takes in one input, the decimal number to be converted, and returns the converted number as a string. The function first determines the sign bit of the number (0 for positive and 1 f…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function output = decimal_to_IEEE754(decimal) | |
% Given a decimal number, this function returns the exact same number in | |
% IEEE754 format (double precision) | |
% decimal: Decimal number to be converted | |
% output: Converted number (string) | |
% Get the sign bit (0 for positive, 1 for negative) | |
sign = 0; | |
if decimal < 0 | |
sign = 1; | |
decimal = abs(decimal); | |
end | |
% Get the exponent and mantissa | |
[mantissa, exponent] = log2(decimal); | |
mantissa = mantissa - 1; | |
exponent = exponent + 1022; | |
% Convert exponent and mantissa to binary | |
exponent = dec2bin(exponent, 11); | |
mantissa = dec2bin(round((mantissa - floor(mantissa)) * 2^53), 53); | |
% Concatenate sign, exponent, and mantissa | |
output = strcat(num2str(sign), " ", exponent, " ", mantissa(2:end)); | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment