real number to it's binary form

4 次查看(过去 30 天)
Ben
Ben 2012-10-27
I am interested in learning more about the mechanics behind computer representation.
For example, if I had a function, like mybin, which takes a real number (either non-negative or negative) in a decimal form and outputs its binary form in a character string. Is there a way to do this without the function dec2bin or bin2dec in the problem? I am more interested in learning about how to program this transformation by myself.
For example i would want : input of (1:23446)_10 to be handled as (1:2344)_10; input of (1:23446)_10 to be be handled as (1:2344)_10; output of (11:00001)_2 to be shown as 11:0000; output of (0:00001)_2 to be shown as 0:0000.
Also, is there a way to do the opposite thing? So, is there a way of creating a function , which takes a binary number (either non-negative or negative) in a character string and outputs its decimal counterpart in another character string?
For example, an input of (1:10001)_2 will be handled as (1:1000)_2; input of (0:1001011)_2 will be handled as (0:1001)_2; output of (5:67898)_10 will be shown as 5:6789; output of (0:5)_10 will be shown as 0:5000.
or just let me know if the only way to go about this is through dec2bin or bin2dec.
thanks!
  3 个评论
Jan
Jan 2012-10-27
@Ben: Now you have inserted some underscores. So I find:
input of (1:23446)_10 to be handled as (1:2344)_10; input of (1:23446)_10 to be handled as (1:2344)_10
Beside the fact, that this is duplicated, it is not clear what "(1:23446)_10" should mean. "1:23446" is a vector, but what is "_10"? Do you mean the base of the number?

请先登录,再进行评论。

回答(2 个)

Star Strider
Star Strider 2012-10-27
I certainly support your interest.
You certainly could do what you describe, but I remember not being amused by it (and having been challenged by integer versions of it) in my engineering graduate school work. See the Wikipedia article on IEEE floating point for details.
In short, if you have a lot of time, experiment with it to you heart's content. Otherwise, consider it a problem solved, and be done with it.
Use the existing MATLAB functions if you have a pressing need.

Walter Roberson
Walter Roberson 2012-10-28
num2hex()
Google IEEE 754 double precision. The Wikipedia article shows the representation.
In order to get at the representation for the more obscure bit patterns such as inf and the many different NaN, you will want to typecast() to uint64() or uint32()

类别

Help CenterFile Exchange 中查找有关 Numeric Types 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by