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: yangli0534@gmail.com

%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

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

  1. oracle的decimal和number的对比

    Oracle只是在语法上支持decimal类型,但是在底层实际上它就是number类型,支持decimal类型是为了能把数据从Oracle数据库移到其他数据库中(如DB2等). 因为decimal在O ...

  2. Oracle/MySQL decimal/int/number 转字符串

    有时客户需要流水数据,当导出为excel的时候,客户编号等很长数字的栏位,被excel变成科学记数法,无法正常查看. 因此,需要将Oracle/MySQL中的decimal/int 转 varchar ...

  3. **611. Valid Triangle Number three pointer O(n^3) -> square(binary search larget number smaller than target)

    Given an array consists of non-negative integers, your task is to count the number of triplets chose ...

  4. cvpr2015papers

    @http://www-cs-faculty.stanford.edu/people/karpathy/cvpr2015papers/ CVPR 2015 papers (in nicer forma ...

  5. General Decimal Arithmetic 浮点算法

    General Decimal Arithmetic http://speleotrove.com/decimal/ General Decimal Arithmetic [ FAQ | Decima ...

  6. Convertion of grey code and binary 格雷码和二进制数之间的转换

    以下转换代码摘自维基百科 Wikipedia: /* The purpose of this function is to convert an unsigned binary number to r ...

  7. UVa 575 Skew Binary 歪斜二进制

    呵呵,这个翻译还是很直白的嘛,大家意会就好. 第一次看到这个高大上题目还是有点小害怕的,还好题没有做过深的文章. 只要按照规则转化成十进制就好了,而且题目本身也说了最大不超过一个int的范围(2^31 ...

  8. General Purpose Hash Function Algorithms

    General Purpose Hash Function Algorithms post@: http://www.partow.net/programming/hashfunctions/inde ...

  9. Friendly number

    Friendly number Long numbers can be made to look nicer, so let’s write some code to do just that. Yo ...

随机推荐

  1. C#怎样处理xml文件的大于号和小于号等常用符号(xml符号引发的程序错误)

    在程序中由xml配置而成的sql语句要转换为C#支持的sql语句 <settings> <select> a.*</select> <from> (se ...

  2. ActiveReports 报表应用教程 (5)---解密电子商务领域首张电子发票的诞生(套打报表)

    6月27日京东商城发布了中国电子商务领域首张电子发票,同时宣布相关系统正式上线,这标志着中国电子商务的步伐又向前迈出了重要的一步.目前“电子发票”覆盖的服务范围是在北京地区购买图书.音像商品的个人消费 ...

  3. PHP实现文字水印图片

    php实现简单的文字水印图片,使用前需要开启php配置中的gd2功能 <?php/*打开图片*/ //1.配置图片路径 $src="image/55.jpg";//这个路径改 ...

  4. 从零开始学习Linux(mkdir and rmdir)

    今天说mkdir 和 rmdir.因为mkdir 内容比较少.而且也很好理解. 对于mkdir来说,一般只用到 -p -m,我只用过-p参数,-m也是刚刚看的. 先说不带参数的: mkdir  tes ...

  5. java之Class类详解

    测试中需要用到的代码 InterfaceA代码: package jichu; interface InterfaceA { String s1 = "this is s1 in Inter ...

  6. Ubuntu14.04安装ROOT集群

    之前尝试在CentOS7上部署ROOT集群,却发现无论是源码包安装,还是官方提供的二进制包,都缺少了关键的xproofd可执行文件,导致PoD不能运行.没有办法,只能尝试在其他OS上部署,这里我选择了 ...

  7. H5调用Android拨打电话

    1.AndroidAndJSInterface.java class AndroidAndJSInterface { /** * 该方法将被js调用,用于加载数据 */ @JavascriptInte ...

  8. mysql innodb表 utf8 gbk占用空间相同,毁三观

    昨天因为发生字符集转换相关错误,今天想验证下utf8和gbk中英文下各自空间的差距.这一测试,绝对毁三观,无论中文还是中文+英文,gbk和utf8占用的实际物理大小完全相同,根本不是理论上所述的“UT ...

  9. CSS3选择器(一)

    E[att^='val'] 选择属性值以val开头的任何字符 E[att$='val'] 选择属性值以val结尾的任何字符 E[att*='val'] 选择属性值包含val的任何字符 :root HT ...

  10. ArcEngine环境下合并断开的线要素(根据属性)

    1.遇到的问题: 最近遇到在线要素(矢量数据)中,一条完整的道路.河流等往往是断开的,如下图1所示: 2.思路: 在ArcGIS Desktop中没有相关的工具可以将这些断开的线要素进行自动合并,今天 ...