C++Builder Programming Forum
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
C++빌더 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
컴포넌트/라이브러리
메신저 프로젝트
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
볼랜드포럼 광고 모집

C++빌더 팁&트릭
C++Builder Programming Tip&Tricks
[980] float type의 실수를 bit로 표현하는 방법
이경문 [gilgil] 9979 읽음    2010-04-06 00:11
여러가지 방법이 있지만 요즘에는 실수를 2진수로 처리를 합니다.

floating point를 사용하는 방법은 여기를 참고하시기 바랍니다.
http://en.wikipedia.org/wiki/Single_precision_floating-point_format

아래는 소스및 실행 결과입니다. sign, exponent, significand가 어떻게 구성되어 있는지를 알 수 있습니다.


void writeFloat(float f)
{
  uint32 u;
  int sign;
  int exponent;
  int significand;

  //
  // Extract bits
  // 
  u = *(uint32*)&f;

  //
  // Extract sign
  //
  sign = (u & 0x80000000) >> 31;     //  1 bit

  //
  // Extract exponent
  //
  exponent = (u & 0x7F800000) >> 23; //  8 bit
  exponent -= 127;                   // exponent bias

  //
  // Extract significand
  //
  significand = (u & 0x007FFFFF);    // 23 bit
  significand |= 0x00800000;         // implicit leading bit with value 1

  printf("%10f %x %d %d 0x%x\n", f, u, sign, exponent, significand, significand);
}

int main()
{
  writeFloat(0.125);
  writeFloat(0.25);
  writeFloat(0.5);
  writeFloat(1);
  writeFloat(2);
  writeFloat(3);
  writeFloat(4);
  writeFloat(8);
  writeFloat(16);
}


[실행결과]
  0.125000 3e000000 0 -3 0x800000
  0.250000 3e800000 0 -2 0x800000
  0.500000 3f000000 0 -1 0x800000
  1.000000 3f800000 0 0 0x800000
  2.000000 40000000 0 1 0x800000
  3.000000 40400000 0 1 0xc00000
  4.000000 40800000 0 2 0x800000
  8.000000 41000000 0 3 0x800000
 16.000000 41800000 0 4 0x800000

+ -

관련 글 리스트
980 float type의 실수를 bit로 표현하는 방법 이경문 9979 2010/04/06
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.