42Seoul/CPP Module 02

ex01(>>, << 쉬프트 연산, 입출력 연산자 오버로딩)

재윤 2024. 1. 24. 15:15
반응형

밑에 부분을 보기 전 공부해야할 

 

1. 이진 기수법

https://wo-dbs.tistory.com/141

 

이진 기수법(이진법)

컴퓨터는 0과 1로 이루어진 기계어를 사용한다. 우리 사람은 수를 표현할 때 10진법을 사용한다 → 10진법은 0, 1, 2, 3, 4, 5, 6, 7, 8, 9을 말함. 컴퓨터는 0과 1인 이진법으로 수를 저장하게 된다. → 이

wo-dbs.tistory.com

 

2. 고정소수점과 부동소수점

https://wo-dbs.tistory.com/142

 

이진 기수법을 통한 고정 소수점과 부동 소수점

이 글은 이 분의 블로그 통해 공부한 글입니다. 이진 기수법을 통한 고정 소수점(Fixed Point) 와 부동 소수점(Floating Point) 이진 기수법을 통한 고정 소수점(Fixed Point) 와 부동 소수점(Floating Point) 이

wo-dbs.tistory.com

 

3. 입출력 오버라이딩

https://wo-dbs.tistory.com/138

 

C++ 입출력 오버로딩

A가 Coord형 오브젝트 일 때 cout

wo-dbs.tistory.com

 

 

 

 

 

main.cpp

#include "Fixed.hpp"

int main( void ) {
	Fixed a;
	Fixed const b( 10 );
	Fixed const c( 42.42f );
	Fixed const d( b );

	a = Fixed( 1234.4321f );

	std::cout << "a is " << a << std::endl;
	std::cout << "b is " << b << std::endl;
	std::cout << "c is " << c << std::endl;
	std::cout << "d is " << d << std::endl;

	std::cout << "a is " << a.toInt() << " as integer" << std::endl;
	std::cout << "b is " << b.toInt() << " as integer" << std::endl;
	std::cout << "c is " << c.toInt() << " as integer" << std::endl;
	std::cout << "d is " << d.toInt() << " as integer" << std::endl;
	return 0;
}

fixed.cpp

#include "Fixed.hpp"

Fixed::Fixed(void)
{
	std::cout << "Default constructor called" << std::endl;
	this->number = 0;
}

Fixed::Fixed(const Fixed &obj)
{
	std::cout << "Copy constructor called" << std::endl;
	*this = obj;
}

Fixed::Fixed(int num)
{
	std::cout << "Int constructor called" << std::endl;
	this->number = num << this->bits;
}

Fixed::Fixed(const float num)
{
	std::cout << "Float constructor called" << std::endl;
	this->number = roundf(num * (1 << this->bits));
}

float Fixed::toFloat(void) const 
{
	return ((float)this->number / (1 << this->bits)); 
}

int Fixed::toInt(void) const 
{
	return (this->number >> this->bits);
}

std::ostream &operator<<(std::ostream &out, const Fixed &obj)
{
	out << obj.toFloat();
	return (out);
}

Fixed& Fixed::operator=(const Fixed &obj)
{
	std::cout << "Copy assignment operator called" << std::endl;
	if (this == &obj)
		return *this;
	this->number = obj.getRawBits();
	return *this;
}

Fixed::~Fixed(void)
{
	std::cout << "Destructor called" << std::endl;
}

int	Fixed::getRawBits(void) const
{
	return (this->number);
}

void	Fixed::setRawBits(int const raw)
{
	this->number = raw;
}

쉬프트 연산을 공부했는데 코드에서 “num * (1 << 8{this→bits})” 이런 것이 있을 것이다.

  • 이짓을 하는 이유?
    • 뒤에 소수부를 남겨놓기 위해 (고정소수점 방식임)
  • 64비트 체계에서 int형은 32비트인데 그 안에서 num이 차지하고 있는 비트에서 8비트를 앞으로 땡기는 것임.
    • 만약 8비트 체계에서는 1 << 8하면 값은 0

그 이후 roundf를 한다

  • 이 짓을 하는 이유?
    • 만약 num이 42.42가 들어오고 1<< 8 한 값 42.42 * 256이 되는데 10859.52가 나오는데 고정소수점으로 했을 때 이 값을 완벽하게 저장하지 못함.
    • 반올림을 해서 그 값에 제일 접근하게 함.

roundf

[C++] round 반올림 함수에 대해서.

 

[C++] round 반올림 함수에 대해서.

안녕하세요! BlockDMask 입니다.오늘은 C++11에 추가된 반올림함수 round에 대해서 이야기해보려 합니다.(C언어/C++ 올림함수 ceil, 내림함수 floor가 궁금하다면 [바로가기] 이쪽에 포스팅이 있습니다.)(C+

blockdmask.tistory.com

 

반응형

'42Seoul > CPP Module 02' 카테고리의 다른 글

ex02(비교, 산술, 증감 연산자 오버로딩)  (1) 2024.01.24
ex00  (0) 2023.12.02