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. WPF 程序自删除(自毁)|卸载程序删除

    一般是在MainWindow_Closed 事件中调用批处理命令删除. 在借鉴别人的想法的基础上的算是改进. 自删除步骤: 1.删除文件 2.删除存放文件夹. 实现代码: private static ...

  2. 【JS复习笔记】04 数组

    JS里的数组其实并不是一个数组,它其实是一个对象,a[1]这种调用方式其实就是一个字面量为1的属性. 因为这东西实际上是一个对象,所以你就可以理解下面这种声明了吧! var arrName=['我可以 ...

  3. 泛函编程(22)-泛函数据类型-Monoid In Action

    在上一节我们讨论了Monoid的结合性和恒等值的作用以及Monoid如何与串类元素折叠算法相匹配.不过我们只示范了一下基础类型(primitive type)Monoid实例的应用,所以上一节的讨论目 ...

  4. java中的代码块执行顺序

    /* 代码块:在Java中,使用{}括起来的代码被称为代码块. 根据其位置和声明的不同,可以分为 局部代码块:局部位置,用于限定变量的生命周期. 构造代码块:在类中的成员位置,用{}括起来的代码.每次 ...

  5. css中的定位和框模型问题

    和定位有关的元素属性如下 position  元素的定位类型   绝对定位会相对于最近定位的祖先元素的位置来定位,而不会影响其他框的位置 固定定位 相对定位 z-index   元素的堆叠顺序 值越大 ...

  6. couchbase单向同步

    我们知道,couchbase默认情况下就是N主的HA模式,bucket同时存储在多个节点中.如下所示: 但事实上,有些时候我们希望某些节点只能读,不能写以避免各种副作用以及分布式系统下出于管理和安全性 ...

  7. C# 循环语句 for循环

    循环:反复执行某段代码. 循环四要素:初始条件,循环条件,循环体,状态改变.for(初始条件;循环条件;状态改变){ 循环体} 给出初始条件,先判断是否满足循环条件,如果不满足条件则跳过for语句,如 ...

  8. 移动端调试工具-Debuggap

    随着移动互联网的迅速崛起,开发移动应用程序越来越多,但如果在移动端开发应用程序需要调试时,额- 仿佛又回到了IE时代,最方便也只能到处 alert 来调试.目前已经有一款产品可以做到这一点,比如pho ...

  9. IOS中程序如何进行推送消息(本地推送,远程推送)2(上)

    未看过本地推送的,可以提前看一下本地推送. http://www.cnblogs.com/wolfhous/p/5135711.html =============================== ...

  10. 【转】Android中处理崩溃异常

    大家都知道,现在安装Android系统的手机版本和设备千差万别,在模拟器上运行良好的程序安装到某款手机上说不定就出现崩溃的现象,开发者个人不可能购买所有设备逐个调试,所以在程序发布出去之后,如果出现了 ...