creation d02 ex02
This commit is contained in:
132
d02/ex02/Fixed.cpp
Normal file
132
d02/ex02/Fixed.cpp
Normal file
@@ -0,0 +1,132 @@
|
||||
#include "Fixed.hpp"
|
||||
|
||||
/*
|
||||
* functions to print numbers in binary
|
||||
* for the float, found help from stackoverflow :
|
||||
* https://stackoverflow.com/questions/474007/floating-point-to-binary-valuec
|
||||
*/
|
||||
|
||||
std::string printBitsInt(int num)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
for (unsigned int mask = 1U << (sizeof(int) *8 -1); mask; mask >>= 1)
|
||||
{
|
||||
std::cout << ((num & mask) != 0);
|
||||
i++;
|
||||
if (i == 1 || i == 9 || i == 24)
|
||||
std::cout << ' ';
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
std::string printBitsFloat(float num)
|
||||
{
|
||||
int *p = (int *)#
|
||||
int i = 0;
|
||||
|
||||
for (unsigned int mask = 1U << (sizeof(float) *8 -1); mask; mask >>= 1)
|
||||
{
|
||||
std::cout << ((*p & mask) != 0);
|
||||
i++;
|
||||
if (i == 1 || i == 9 || i == 24)
|
||||
std::cout << ' ';
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/*
|
||||
* statics variables initialisation
|
||||
*
|
||||
* for MAX integer :
|
||||
* 00000000 01111111 11111111 11111111 ( 8388607) (-1U >> (this->_frac +1))
|
||||
* <= ... >=
|
||||
* 11111111 10000000 00000000 00000000 (-8388608)
|
||||
*
|
||||
*/
|
||||
|
||||
int const Fixed::_frac = 8;
|
||||
int const Fixed::_max = -1U >> (_frac +1);
|
||||
|
||||
/*
|
||||
* default constructor / copy constructor / destructor
|
||||
*/
|
||||
|
||||
Fixed::Fixed() : _value(0) {
|
||||
std::cout << "Default constructor called" << '\n';
|
||||
return;
|
||||
}
|
||||
|
||||
Fixed::Fixed(Fixed const & src) {
|
||||
std::cout << "Copy constructor called" << '\n';
|
||||
*this = src;
|
||||
return;
|
||||
}
|
||||
|
||||
Fixed::~Fixed( void ) {
|
||||
std::cout << "Destructor called" << '\n';
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* int and float constructors
|
||||
*/
|
||||
|
||||
Fixed::Fixed(int integer) {
|
||||
std::cout << "Int constructor called" << '\n';
|
||||
|
||||
if (integer < ~this->_max || integer > this->_max)
|
||||
std::cout << "error: integer out of range" << '\n';
|
||||
else
|
||||
this->_value = integer << this->_frac;
|
||||
}
|
||||
|
||||
Fixed::Fixed(float const floater) {
|
||||
std::cout << "Float constructor called" << '\n';
|
||||
|
||||
if (floater < ~this->_max || floater > this->_max)
|
||||
std::cout << "error: float out of range" << '\n';
|
||||
else
|
||||
this->_value = floater * (1 << this->_frac);
|
||||
}
|
||||
|
||||
/*
|
||||
* assignement operator
|
||||
*/
|
||||
|
||||
Fixed & Fixed::operator=( Fixed const & rhs ) {
|
||||
std::cout << "Copy assignment operator called" << '\n';
|
||||
if ( this != &rhs )
|
||||
this->_value = rhs.getRawBits();
|
||||
return *this;
|
||||
}
|
||||
|
||||
/*
|
||||
* functions that returns _value
|
||||
*/
|
||||
|
||||
int Fixed::getRawBits( void ) const {
|
||||
return this->_value;
|
||||
}
|
||||
|
||||
void Fixed::setRawBits( int const raw ) {
|
||||
this->_value = raw;
|
||||
}
|
||||
|
||||
int Fixed::toInt( void ) const {
|
||||
return (this->_value >> this->_frac);
|
||||
}
|
||||
float Fixed::toFloat( void ) const {
|
||||
return ((float)this->_value / (float)(1 << this->_frac));
|
||||
}
|
||||
|
||||
/*
|
||||
* overload "<<" -> output fixed point in float representation
|
||||
* took here : https://github.com/pgomez-a/42_CPP_Piscine/blob/master/cpp02/ex01/Fixed.cpp
|
||||
*/
|
||||
|
||||
std::ostream & operator<<(std::ostream & out, Fixed const & fixed)
|
||||
{
|
||||
out << fixed.toFloat();
|
||||
return (out);
|
||||
}
|
||||
35
d02/ex02/Fixed.hpp
Normal file
35
d02/ex02/Fixed.hpp
Normal file
@@ -0,0 +1,35 @@
|
||||
#ifndef FIXED_HPP
|
||||
# define FIXED_HPP
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cmath>
|
||||
|
||||
class Fixed {
|
||||
|
||||
public:
|
||||
|
||||
Fixed(void); // default/parametric constructor
|
||||
Fixed(Fixed const & src); // copy constructor
|
||||
~Fixed(void); // destructor
|
||||
Fixed(int integer);
|
||||
Fixed(float const floater);
|
||||
|
||||
Fixed & operator=(Fixed const & rhs); // assignement operator
|
||||
|
||||
int getRawBits(void) const;
|
||||
void setRawBits(int const raw);
|
||||
float toFloat(void) const;
|
||||
int toInt(void) const;
|
||||
|
||||
private:
|
||||
|
||||
int _value;
|
||||
static int const _frac;
|
||||
static int const _max;
|
||||
|
||||
};
|
||||
|
||||
std::ostream & operator<<(std::ostream & out, Fixed const & fixed);
|
||||
|
||||
#endif
|
||||
62
d02/ex02/Makefile
Normal file
62
d02/ex02/Makefile
Normal file
@@ -0,0 +1,62 @@
|
||||
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
|
||||
# . name = value . name is case sensitive #
|
||||
# VARIABLES . or name = value \ . use VPATH only for .c #
|
||||
# . value . or .cpp #
|
||||
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
|
||||
|
||||
NAME = fixed
|
||||
|
||||
CC = clang++
|
||||
CFLAGS = -Wall -Wextra -Werror $(INCLUDES) -std=c++98
|
||||
|
||||
VPATH = $(D_SRCS)
|
||||
|
||||
LIBS =
|
||||
|
||||
INCLUDES = -I$(D_HEADERS)
|
||||
|
||||
D_SRCS = .
|
||||
SRCS = main.cpp \
|
||||
Fixed.cpp
|
||||
|
||||
D_HEADERS = .
|
||||
HEADERS = Fixed.hpp
|
||||
|
||||
D_OBJS = builds
|
||||
OBJS = $(SRCS:%.cpp=$(D_OBJS)/%.o)
|
||||
|
||||
RM_D_OBJS = rm -rf $(D_OBJS)
|
||||
ifeq "$(D_OBJS)" "."
|
||||
RM_D_OBJS =
|
||||
endif
|
||||
|
||||
|
||||
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
|
||||
# . target: prerequisites . $@ : target #
|
||||
# RULES . recipe . $< : 1st prerequisite #
|
||||
# . recipe . $^ : all prerequisites #
|
||||
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
$(D_OBJS)/%.o: %.cpp | $(D_OBJS)
|
||||
$(CC) $(CFLAGS) -c $< -o $@
|
||||
|
||||
$(D_OBJS):
|
||||
mkdir $@
|
||||
|
||||
$(OBJS): $(HEADERS:%=$(D_HEADERS)/%)
|
||||
|
||||
$(NAME): $(OBJS)
|
||||
$(CC) $(OBJS) -o $@ $(LIBS)
|
||||
|
||||
clean:
|
||||
rm -f $(OBJS)
|
||||
|
||||
fclean: clean
|
||||
rm -f $(NAME)
|
||||
$(RM_D_OBJS)
|
||||
|
||||
re: fclean all
|
||||
|
||||
.PHONY : all clean fclean re bonus run valgrind
|
||||
24
d02/ex02/main.cpp
Normal file
24
d02/ex02/main.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#include "Fixed.hpp"
|
||||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user