function [ binary,decimal ] = num2binary16( number )

function [ binary,decimal ] = num2binary16( number )
%The IEEE 754 standard specifies a binary16 as having the following format:

%Sign bit: 1 bit
%Exponent width: 5 bits
%Significand precision: 11 bits (10 explicitly stored)
%S EEEEE MMMMMMMMMM
%INPUT: number is a random precision number

%OUTPUT : var binary is a 2‘s string of the binary16
%         var decimail is the value of the number in decimal

%Author :Yang Li .
%Email: [email protected]

%Data:20150126
if real(number) >= 2^14 
    error(‘beyond the maximum :-2^14--+2^24‘);
end

S=number<0 ;
E=0;
M=abs(number);
while(M>=2 || M<1 )
    if(M>=2)
     E=E+1;
     M=M/2;
    else
        E=E-1;
        M=M*2;
    end   
end
M=round((M-1)*2^10) ;

  number1=(-1)^S*(2^ E   )*(1+M/2^10);
% binary=strcat(num2str(S),num2str(E),num2str(M))
E =E+15;
binary=strcat(num2str(S),dec2bin(E,5), dec2bin(M,10)) ;

sReal=(str2num(binary(1)));

eReal =-15;
for i=2:6
    eReal=eReal+str2num(binary(i))*2^(6-i);
end   
%  eReal=eReal*(-1)^str2num(binary(2));
mReal = 0;
for i=7:16
    mReal=mReal+str2num(binary(i))*2^(16-i);
end
 
numberReal=(-1)^sReal*2^eReal*(1+mReal/2^10);
err=num2str(abs(number-numberReal));
decimal=numberReal;
% disp([‘the origin data is : ‘,num2str(number)]);
% disp([‘the float format is : ‘,binary]);
% disp([‘so ,there is a error :‘,num2str(abs(number-numberReal))]);

end

时间: 2025-01-05 09:50:43

function [ binary,decimal ] = num2binary16( number )的相关文章

uva 575 Skew Binary(数论)

uva 575 Skew Binary When a number is expressed in decimal, the k-th digit represents a multiple of 10k. (Digits are numbered from right to left, where the least significant digit is number 0.) For example, When a number is expressed in binary, the k-

uvalive 6300 Signed Binary Representation of Integers

6300 Signed Binary Representation of IntegersComputing ax mod n for large integers x and n is the most time-consuming process in most public-keycryptography systems. An algorithm to compute ax mod n can be described in a C-like pseudo-code asfollows.

UVa 575 Skew Binary 歪斜二进制

呵呵,这个翻译还是很直白的嘛,大家意会就好. 第一次看到这个高大上题目还是有点小害怕的,还好题没有做过深的文章. 只要按照规则转化成十进制就好了,而且题目本身也说了最大不超过一个int的范围(2^31-1 == 2147483647). 直接位运算就好了.   Skew Binary  When a number is expressed in decimal, the k-th digit represents a multiple of 10k. (Digits are numbered f

General Purpose Hash Function Algorithms

General Purpose Hash Function Algorithms [email protected]: http://www.partow.net/programming/hashfunctions/index.html     Description Hashing Methodologies Hash Functions and Prime Numbers Bit Biases Various Forms Of Hashing String Hashing Cryptogra

[转]Date and String Function in BluePrism

本文转自:https://www.codeproject.com/Articles/1247389/Date-and-String-Function-in-BluePrism This article appears in the Third Party Products and Tools section. Articles in this section are for the members only and must not be used to promote or advertise

javascript中的Function和Object

写的很好,理解了很多,特此转发记录 转自:http://blog.csdn.net/tom_221x/archive/2010/02/22/5316675.aspx 在JavaScript中所有的对象都继承自Object原型,而Function又充当了对象的构造器,那么Funtion和Object到底有着什麽样的关系呢 ? 首先,一切都是对象. 1 alert(Object instanceof Object); // true 2 alert(Function instanceof Objec

Number To Indian Rupee Words in Oracle Forms / Reports

Convert numbers to Indian Rupees format in Oracle Forms / Reports. Create the below mention function in Oracle Forms / Reports and call it with passing a number parameter. FUNCTION INR_words( p_number In number, vFrDec varchar2 Default 'Paisa Only')

全面理解Javascript中Function对象的属性和方法

函数是 JavaScript 中的基本数据类型,在函数这个对象上定义了一些属性和方法,下面我们逐一来介绍这些属性和方法,这对于理解Javascript的继承机制具有一定的帮助. 属性(Properties) arguments 获取当前正在执行的 Function 对象的所有参数,是一个类似数组但不是数组的对象,说它类似数组是因为其具有数组一样的访问性质及方式,可以由arguments[n]来访问对应的单个参数的值,并拥有数组长度属性length.还有就是arguments对象存储的是实际传递给

[Leetcode] Binary search -- 222. Count Complete Tree Nodes

Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from Wikipedia:In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as pos