check leaks on d02 ok

This commit is contained in:
hugogogo
2022-03-14 17:42:07 +01:00
parent eec58e03ac
commit a3a82c1c18
10 changed files with 112 additions and 13 deletions

17
d02/< Normal file
View File

@@ -0,0 +1,17 @@
#include <iostream>
#include <string>
#include "colors.h"
#define N_TEST "1"
int main() {
int i = 0;
std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] "
<< "tests :" RESET "\n";
{
}
return 0;
}

View File

@@ -122,7 +122,7 @@ CONVERSIONS
1 / 2 = 0 -> 1
75 :
.75 *2^2 = 2,88 ~= 3
(*2^x parce que c le moyen dont fait "bouger" la virgule
(*2^x parce que c le moyen dont on fait "bouger" la virgule
en base 2 (*10^x) en base 10)
3 / 2 = 1 -> 1
1 / 2 = 0 -> 1
@@ -312,5 +312,3 @@ integer = (integer << (30 - this->_frac - exponent)) | sign; // add sign
integer >>= (30 - this->_frac - exponent); // align right
std::cout << "integer : " << printBitsInt(integer) << " (" << integer << ")\n";
```

View File

@@ -20,7 +20,8 @@ SRCS = main.cpp \
Fixed.cpp
D_HEADERS = .
HEADERS = Fixed.hpp
HEADERS = Fixed.hpp \
colors.h
D_OBJS = builds
OBJS = $(SRCS:%.cpp=$(D_OBJS)/%.o)
@@ -50,6 +51,9 @@ $(OBJS): $(HEADERS:%=$(D_HEADERS)/%)
$(NAME): $(OBJS)
$(CC) $(OBJS) -o $@ $(LIBS)
leaks: $(NAME)
valgrind --leak-check=full --show-leak-kinds=all ./$(NAME)
clean:
rm -f $(OBJS)

25
d02/ex00/colors.h Normal file
View File

@@ -0,0 +1,25 @@
#ifndef COLORS_H
# define COLORS_H
# define GRAY "\e[0;30m"
# define RED "\e[0;31m"
# define GREEN "\e[0;32m"
# define YELLOW "\e[0;33m"
# define BLUE "\e[0;34m"
# define PURPLE "\e[0;35m"
# define CYAN "\e[0;36m"
# define WHITE "\e[0;37m"
# define B_GRAY "\e[1;30m"
# define B_RED "\e[1;31m"
# define B_GREEN "\e[1;32m"
# define B_YELLOW "\e[1;33m"
# define B_BLUE "\e[1;34m"
# define B_PURPLE "\e[1;35m"
# define B_CYAN "\e[1;36m"
# define B_WHITE "\e[1;37m"
# define RESET "\e[0m"
#endif

View File

@@ -1,17 +1,54 @@
#include "Fixed.hpp"
#include <iostream>
#include "colors.h"
#define N_TEST "1"
int main( void ) {
Fixed a;
Fixed b( a );
Fixed c;
int i = 0;
c = b;
std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] "
<< "tests subject :" RESET "\n";
{
Fixed a;
Fixed b( a );
Fixed c;
c = b;
std::cout << a.getRawBits() << std::endl;
std::cout << b.getRawBits() << std::endl;
std::cout << c.getRawBits() << std::endl;
}
std::cout << a.getRawBits() << std::endl;
std::cout << b.getRawBits() << std::endl;
std::cout << c.getRawBits() << std::endl;
std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] "
<< "tests sets :" RESET "\n";
{
Fixed a;
Fixed b( a );
Fixed c;
c = b;
std::cout << "\n" B_BLUE "sets a(4), b(6.3), c(-187)" RESET "\n";
a.setRawBits(4);
b.setRawBits(6.3);
c.setRawBits(-187);
std::cout << a.getRawBits() << std::endl;
std::cout << b.getRawBits() << std::endl;
std::cout << c.getRawBits() << std::endl;
std::cout << "\n" B_BLUE "sets a(76.76), b(0), c(0.0)" RESET "\n";
a.setRawBits(76.76);
b.setRawBits(0);
c.setRawBits(0.0);
std::cout << a.getRawBits() << std::endl;
std::cout << b.getRawBits() << std::endl;
std::cout << c.getRawBits() << std::endl;
}
return 0;
}

View File

@@ -5,14 +5,14 @@
* statics variables initialisation
*
* for MAX integer :
* 00000000 01111111 11111111 11111111 ( 8388607) (-1U >> (this->_frac +1))
* 00000000 01111111 11111111 11111111 ( 8388607) (-1U >> (this->_frac + 1))
* <= ... >=
* 11111111 10000000 00000000 00000000 (-8388608) ~MAX
*
*/
int const Fixed::_frac = 8;
int const Fixed::_max = -1U >> (_frac +1);
int const Fixed::_max = -1U >> (_frac + 1);
int const Fixed::_min = ~_max;
/*
@@ -55,6 +55,18 @@ Fixed::Fixed(float const floater) {
std::cout << "error: float out of range" << '\n';
else
this->_value = roundf(floater * (1 << Fixed::_frac));
// this->_value = *((int *)&floater); // access float adress content as 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";
}
/*

View File

@@ -50,6 +50,9 @@ $(OBJS): $(HEADERS:%=$(D_HEADERS)/%)
$(NAME): $(OBJS)
$(CC) $(OBJS) -o $@ $(LIBS)
leaks: $(NAME)
valgrind --leak-check=full --show-leak-kinds=all ./$(NAME)
clean:
rm -f $(OBJS)

Binary file not shown.

View File

@@ -50,6 +50,9 @@ $(OBJS): $(HEADERS:%=$(D_HEADERS)/%)
$(NAME): $(OBJS)
$(CC) $(OBJS) -o $@ $(LIBS)
leaks: $(NAME)
valgrind --leak-check=full --show-leak-kinds=all ./$(NAME)
clean:
rm -f $(OBJS)

Binary file not shown.