필터 지우기
필터 지우기

real number to it's binary form

조회 수: 8 (최근 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
Ben
Ben 2012년 10월 27일
sorry i fixed it!
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에서 GPU Computing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by