개발자_이훈규
천천히, 빠르게. 개발자의 Repository
개발자_이훈규
전체 방문자
오늘
어제
  • 분류 전체보기 (473)
    • 티스토리 (4)
    • 개발자 뉴스 (2)
    • 소프트웨어 (203)
      • C (7)
      • c++ (25)
      • Objective-C (3)
      • Do it! 반응형 웹디자인 (4)
      • openGL (8)
      • Java (24)
      • Jni (3)
      • Android (9)
      • Wordpress (2)
      • 버그 만난 후 느낀점 (2)
      • Git (3)
      • node js (2)
      • window tablet (1)
      • HTML (3)
      • javascript (3)
      • perl (1)
      • AngularJS (0)
      • JSON (0)
      • Docker (3)
      • python (5)
      • jQuery (1)
      • MFC (4)
      • cocos studio (6)
      • Golang (1)
      • SQLite3 (0)
      • Spring Boot (8)
      • thymeleaf (0)
      • Django (0)
      • iOS (3)
      • skia (0)
      • VBA (0)
      • PHP (2)
      • Oracle (1)
      • JSP (0)
      • R (0)
    • TCP IP (2)
    • 금융 (0)
      • 금융 Study (0)
      • 금융 Archive (0)
      • 금융 Article (0)
    • 개인 프로젝트 (7)
      • gif 홈페이지 만들기 (0)
      • study app만들기 (0)
      • 크롤러 만들기 (1)
      • 카툰 홈페이지 만들기 (1)
      • 외주 홈페이지 만들기 (3)
      • 웹 홈페이지 만들기 (0)
      • 미디어 서버 만들기 (0)
      • 소개팅 어플 만들기 (0)
      • 인스타그램 풀스택 클론 코딩(인강 노트) (0)
      • 주식 모의거래 만들기 (1)
    • html php mysql (0)
    • node.Js (2)
    • 일상 (2)
    • 빈공간 uml 공부 (0)
    • Ubuntu(linux) (12)
    • 맥OS (10)
      • android 설치하기 (2)
    • Programming quizzes (0)
    • IoT (구 유비쿼터스) (16)
      • 라즈베리 파이 (11)
      • 아두이노 (5)
    • 하드웨어 (5)
      • 아수스 비보탭 노트8 asus vivotap no.. (2)
      • 크레마 카르타 (3)
    • 분석할 문장, 구문, 코드 (0)
    • 키보드 (1)
      • 해피해킹 (1)
    • 코드 라이언 (0)
    • 전자기기 (4)
    • Ted (0)
    • NAS (0)
    • 알고리즘 (0)
    • 연합인포맥스 (0)
    • 이벤트 응모함 (4)

블로그 메뉴

  • 홈
  • 태그
  • 미디어로그
  • 위치로그
  • 방명록

공지사항

인기 글

태그

  • ubuntu
  • Python
  • 코드
  • 우분투
  • 방법
  • 개발
  • 소스
  • CODE
  • 예제
  • C
  • 설명
  • 에러
  • C++
  • GIT
  • Example
  • 라즈베리 파이
  • error
  • install
  • Java
  • 설치

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
개발자_이훈규

천천히, 빠르게. 개발자의 Repository

소프트웨어/C

int * float 의 관계에 대해서

2015. 7. 30. 15:52

코딩을 하다가 예상과 다른 값이 나와서 이것 저것 찾아보다 보니..

float와 int의 관계에서 문제가 생겼다.


float와 int의 곱에서 float의 값을 유지하기 위해서는,


(int)(int * int * float)의 형태가 되어야한다.

즉, float와 int의 곱을 한 값에서 int로 캐스팅을 하는 것이다.


아래는 그 실험이다.








Result : 

hklee@hklee:~/Dropbox/cpp/testcase/float$ g++ main.cpp ; ./a.out

main.cpp: In function ‘void func1()’:

main.cpp:16:29: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘int’ [-Wformat=]

     printf("1/2 = %f\n", 1/2);

                             ^

main.cpp: In function ‘void func2()’:

main.cpp:36:55: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat=]

     printf("sum(int) * scale(float) = %d\n", sum*scale);

                                                       ^

main.cpp:37:62: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat=]

     printf("sum(int) * scale(float) = %d\n", (float)sum*scale);

                                                              ^

main.cpp:40:52: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat=]

     printf("int * int * float = %d(%%d)\n", 5*5*0.5);

                                                    ^

main.cpp:41:66: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat=]

     printf("(float)int * int * float = %d(%%d)\n", (float)5*5*0.5);

                                                                  ^

main.cpp:42:80: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat=]

     printf("(float)int * (float)int * float = %d(%%d)\n", (float)5*(float)5*0.5);

                                                                                ^

main.cpp:46:62: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat=]

     printf("(int)int * int * float = %d(%%d)\n", (int)5*5*0.5);

                                                              ^

1/2 = -0.052041

1/2 = 0

1/2 = 0.500000

1/2 = 0

sum(int) * scale(float) = 5

sum(int) * scale(float) = 0

sum(int) * scale(float) = 0

sum(int) * scale(float) = 5

int * int * float = 0(%d)

(float)int * int * float = 0(%d)

(float)int * (float)int * float = 0(%d)

(int)((float)int * (float)int * float) = 12(%d)


(int)int * int * float = 0(%d)

(int)(int * int * float) = 12(%d)

num = 4.000000

num = 1.000000

num = 0.250000

num = 0.062500

num = 0.015625

num = 0.003906

num = 0.000977

num = 0.000244

num = 0.000061



저작자표시 비영리 (새창열림)

'소프트웨어 > C' 카테고리의 다른 글

scanf속에 표현식을 넣는 경우, scanf가 무시될 수 있다.  (0) 2015.07.29
파일 입출력, 전체 파일 메모리 복사해놓기  (0) 2015.07.01
struct 선언하면서 할당하기(?) _ struct의 이상한 모양 분석  (0) 2015.06.10
[에러 리뷰] error: expected ‘,’ or ‘...’ before ‘this’  (0) 2015.05.07
엔터(enter,개행문자) 입력받기.  (0) 2015.01.26
    '소프트웨어/C' 카테고리의 다른 글
    • scanf속에 표현식을 넣는 경우, scanf가 무시될 수 있다.
    • 파일 입출력, 전체 파일 메모리 복사해놓기
    • struct 선언하면서 할당하기(?) _ struct의 이상한 모양 분석
    • [에러 리뷰] error: expected ‘,’ or ‘...’ before ‘this’
    개발자_이훈규
    개발자_이훈규
    혼자 꽁양꽁양 개발하면서 놀아요~ - 노트같은 블로그

    티스토리툴바