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程序(3)

  2. 最全面的jdbcUtils,总有一种适合你

    附加jar包,TxQueryRunner.java文件,dbconfig.properties配置文件(点击链接下载): http://files.cnblogs.com/files/xiaoming ...

  3. ArcGIS Add-in——自动保存编辑

    需求:由于初次使用ArcGIS编辑器不习惯.数据量大造成经常程序未响应.计算机断电等因素,造成编辑的数据没有保存,影响了生产效率,本人根据草色静然的博文,总结了自动保存编辑的实现方法. 分析:自动保存 ...

  4. SQL学习笔记:选取第N条记录

    Northwind数据库,选取价格第二高的产品. 有两种方法,一个是用Row_Number()函数: SELECT productname FROM ( productname, Row_Number ...

  5. 桥牌笔记:Skill Level 4 D8

    西拿黑桃K.A后,转攻小方块. 看来只有一个小红桃失张了.飞方块没有必要冒险.但将牌分布4-0时会有点麻烦.

  6. 自定义带进度条的WebView , 增加获取web标题和url 回掉

    1.自定义ProgressWebView package com.app.android05; import android.content.Context; import android.graph ...

  7. Android系统提供的开发常用的包名及作用

    android.app :提供高层的程序模型.提供基本的运行环境 android.content :包含各种的对设备上的数据进行访问和发布的类 android.database :通过内容提供者浏览和 ...

  8. 在iOS 8中使用UIAlertController

    iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UIPresentationController在实现视图控制器间的过渡动画效果和自适应设备尺寸 ...

  9. 自增build ID配置

    本文转载出自:http://blog.sina.com.cn/s/blog_13ec67adc0102wimg.html   Build在plist文件中的key是“CFBundleVersion”, ...

  10. Objective-C之Protocol

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...