creation d02 ex02
This commit is contained in:
@@ -58,7 +58,7 @@ DECIMALE VS BINARY
|
||||
---------------------------------------------------------
|
||||
NEGATIVS INTEGERS
|
||||
---------------------------------------------------------
|
||||
|
||||
```
|
||||
5 0101 0 0000 0 0000 0000 0000 0000 0000 0000 0000 0000
|
||||
1010 + 1 1 0001 1 0000 0000 0000 0000 0000 0000 0000 0001
|
||||
-5 1011 2 0010 2 0000 0000 0000 0000 0000 0000 0000 0010
|
||||
@@ -79,13 +79,14 @@ NEGATIVS INTEGERS
|
||||
13 1101 -3 1111 1111 1111 1111 1111 1111 1111 1101
|
||||
14 1110 -2 1111 1111 1111 1111 1111 1111 1111 1110
|
||||
15 1111 -1 1111 1111 1111 1111 1111 1111 1111 1111
|
||||
|
||||
```
|
||||
|
||||
---------------------------------------------------------
|
||||
FLOATS
|
||||
---------------------------------------------------------
|
||||
https://stackoverflow.com/questions/7644699/how-are-floating-point-numbers-stored-in-memory
|
||||
|
||||
```
|
||||
1.....................23
|
||||
8......1
|
||||
seeeeeeeemmmmmmmmmmmmmmmmmmmmmmm meaning
|
||||
@@ -93,20 +94,21 @@ https://stackoverflow.com/questions/7644699/how-are-floating-point-numbers-store
|
||||
signe
|
||||
exponent
|
||||
mantis
|
||||
|
||||
```
|
||||
1.175494351 * 10^-38 < ... > 3.40282347 * 10^+38
|
||||
|
||||
2x10^-1 = 0.2x10^0 = 0.02x10^1 = 0.2
|
||||
|
||||
x 00000000 xxxxxxxx xxxxxxxx xxxxxxx special meanings (see bellow)
|
||||
x 11111111 xxxxxxxx xxxxxxxx xxxxxxx //
|
||||
x 00000001 xxxxxxxx xxxxxxxx xxxxxxx smallest exponent (-126)
|
||||
x 01111111 xxxxxxxx xxxxxxxx xxxxxxx 0 is middle exponent (254 / 2 = 127)
|
||||
x 11111110 xxxxxxxx xxxxxxxx xxxxxxx biggest exponent (+127)
|
||||
- x 00000000 xxxxxxxx xxxxxxxx xxxxxxx special meanings (see bellow)
|
||||
- x 11111111 xxxxxxxx xxxxxxxx xxxxxxx //
|
||||
- x 00000001 xxxxxxxx xxxxxxxx xxxxxxx smallest exponent (-126)
|
||||
- x 01111111 xxxxxxxx xxxxxxxx xxxxxxx 0 is middle exponent (254 / 2 = 127)
|
||||
- x 11111110 xxxxxxxx xxxxxxxx xxxxxxx biggest exponent (+127)
|
||||
|
||||
x 00000000 00000000 00000000 0000000 0 (x can still be + or -)
|
||||
x 11111111 00000000 00000000 0000000 +/- infinity
|
||||
x 11111111 xxxxxxxx xxxxxxxx xxxxxx1 NaN (at least one non-zero mantissa digit)
|
||||
x 00000000 xxxxxxxx xxxxxxxx xxxxxx1 denormalized numbers (same for mantissa)
|
||||
- x 00000000 00000000 00000000 0000000 0 (x can still be + or -)
|
||||
- x 11111111 00000000 00000000 0000000 +/- infinity
|
||||
- x 11111111 xxxxxxxx xxxxxxxx xxxxxx1 NaN (at least one non-zero mantissa digit)
|
||||
- x 00000000 xxxxxxxx xxxxxxxx xxxxxx1 denormalized numbers (same for mantissa)
|
||||
|
||||
---------------------------------------------------------
|
||||
CONVERSIONS
|
||||
|
||||
@@ -53,18 +53,18 @@ int const Fixed::_max = -1U >> (_frac +1);
|
||||
*/
|
||||
|
||||
Fixed::Fixed() : _value(0) {
|
||||
// std::cout << "Default constructor called" << '\n';
|
||||
std::cout << "Default constructor called" << '\n';
|
||||
return;
|
||||
}
|
||||
|
||||
Fixed::Fixed(Fixed const & src) {
|
||||
// std::cout << "Copy constructor called" << '\n';
|
||||
std::cout << "Copy constructor called" << '\n';
|
||||
*this = src;
|
||||
return;
|
||||
}
|
||||
|
||||
Fixed::~Fixed( void ) {
|
||||
// std::cout << "Destructor called" << '\n';
|
||||
std::cout << "Destructor called" << '\n';
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -73,42 +73,21 @@ Fixed::~Fixed( void ) {
|
||||
*/
|
||||
|
||||
Fixed::Fixed(int integer) {
|
||||
// std::cout << "Int constructor called" << '\n';
|
||||
std::cout << "Int constructor called" << '\n';
|
||||
|
||||
if (integer < ~this->_max || integer > this->_max)
|
||||
{
|
||||
std::cout << "error: integer out of range" << '\n';
|
||||
return;
|
||||
}
|
||||
// std::cout << "integer : " << printBitsInt(integer) << " (" << integer << ")\n";
|
||||
this->_value = integer << this->_frac;
|
||||
// std::cout << "_value : " << printBitsInt(this->_value) << " (" << this->_value << ")\n";
|
||||
else
|
||||
this->_value = integer << this->_frac;
|
||||
}
|
||||
|
||||
Fixed::Fixed(float const floater) {
|
||||
// std::cout << "Float constructor called" << '\n';
|
||||
std::cout << "Float constructor called" << '\n';
|
||||
|
||||
if (floater < ~this->_max || floater > this->_max)
|
||||
{
|
||||
std::cout << "error: float out of range" << '\n';
|
||||
return;
|
||||
}
|
||||
// std::cout << "floater : " << printBitsFloat(floater) << " (" << floater << ")\n";
|
||||
this->_value = floater * (1 << this->_frac);
|
||||
// std::cout << "_value : " << printBitsInt(this->_value) << " (" << this->_value << ")\n";
|
||||
|
||||
/*
|
||||
this->_value = *((int *)&floater); // cast float to int
|
||||
int sign = this->_value & (1 << 31); // extract sign
|
||||
int exponent = ((unsigned int)(this->_value << 1) >> 24) - 127; // extract exponent
|
||||
int integer = (this->_value << 8) | (1 << 31); // add left 1
|
||||
integer = (unsigned int)integer >> (31 - this->_frac - exponent);// align to right
|
||||
if (sign != 0)
|
||||
integer = (~integer + 1); // reverse negatif
|
||||
integer = (integer << (30 - this->_frac - exponent)) | sign; // add sign
|
||||
integer >>= (30 - this->_frac - exponent); // align right
|
||||
std::cout << "integer : " << printBitsInt(integer) << " (" << integer << ")\n";
|
||||
*/
|
||||
else
|
||||
this->_value = floater * (1 << this->_frac);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -116,19 +95,17 @@ Fixed::Fixed(float const floater) {
|
||||
*/
|
||||
|
||||
Fixed & Fixed::operator=( Fixed const & rhs ) {
|
||||
// std::cout << "Copy assignment operator called" << '\n';
|
||||
std::cout << "Copy assignment operator called" << '\n';
|
||||
if ( this != &rhs )
|
||||
this->_value = rhs.getRawBits();
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
/*
|
||||
* other functions
|
||||
* functions that returns _value
|
||||
*/
|
||||
|
||||
int Fixed::getRawBits( void ) const {
|
||||
// std::cout << "getRawBits member function called" << '\n';
|
||||
return this->_value;
|
||||
}
|
||||
|
||||
@@ -136,9 +113,20 @@ void Fixed::setRawBits( int const raw ) {
|
||||
this->_value = raw;
|
||||
}
|
||||
|
||||
void Fixed::toFloat( void ) const {}
|
||||
int Fixed::toInt( void ) const {
|
||||
return 0;
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cmath>
|
||||
|
||||
class Fixed {
|
||||
|
||||
@@ -18,7 +19,7 @@ public:
|
||||
|
||||
int getRawBits(void) const;
|
||||
void setRawBits(int const raw);
|
||||
void toFloat(void) const;
|
||||
float toFloat(void) const;
|
||||
int toInt(void) const;
|
||||
|
||||
private:
|
||||
@@ -29,4 +30,6 @@ private:
|
||||
|
||||
};
|
||||
|
||||
std::ostream & operator<<(std::ostream & out, Fixed const & fixed);
|
||||
|
||||
#endif
|
||||
|
||||
BIN
d02/ex01/fixed
BIN
d02/ex01/fixed
Binary file not shown.
@@ -3,39 +3,22 @@
|
||||
|
||||
int main( void ) {
|
||||
|
||||
// Fixed a(1105979538);
|
||||
Fixed a(1105979);
|
||||
std::cout << '\n';
|
||||
Fixed b(1000.5979f);
|
||||
std::cout << '\n';
|
||||
Fixed c(-1105.979f);
|
||||
std::cout << '\n';
|
||||
Fixed d(3726.7623683f);
|
||||
std::cout << '\n';
|
||||
Fixed e(4328239.384271721f);
|
||||
std::cout << '\n';
|
||||
Fixed f(1.11117f);
|
||||
std::cout << '\n';
|
||||
Fixed g(42.42f);
|
||||
std::cout << '\n';
|
||||
Fixed h(1234.4321f);
|
||||
Fixed a;
|
||||
Fixed const b( 10 );
|
||||
Fixed const c( 42.42f );
|
||||
Fixed const d( b );
|
||||
|
||||
// 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;
|
||||
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;
|
||||
}
|
||||
|
||||
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