feat: finishing the ex02
This commit is contained in:
parent
95fb804997
commit
1179527d22
7 changed files with 373 additions and 13 deletions
|
|
@ -6,7 +6,7 @@
|
|||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/12/21 19:52:44 by rparodi #+# #+# */
|
||||
/* Updated: 2024/12/27 13:06:30 by rparodi ### ########.fr */
|
||||
/* Updated: 2024/12/27 13:42:55 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -29,13 +29,11 @@ Fixed::Fixed(const Fixed ©) {
|
|||
}
|
||||
|
||||
Fixed::Fixed(const int &integer) {
|
||||
this->_integer_value = integer;
|
||||
this->_floating_value = integer;
|
||||
this->_fixed_value = integer << _fractionnal;
|
||||
}
|
||||
|
||||
Fixed::Fixed(const float &floating) {
|
||||
this->_integer_value = roundf(floating);
|
||||
this->_floating_value = floating;
|
||||
this->_fixed_value = (int)(roundf(floating * (1 << this->_fractionnal)));
|
||||
}
|
||||
|
||||
Fixed::~Fixed() {
|
||||
|
|
@ -62,14 +60,14 @@ void Fixed::setRawBits(const int value) {
|
|||
}
|
||||
|
||||
int Fixed::toInt() const {
|
||||
return (std::roundf(this->_floating_value));
|
||||
return ((int)this->_fixed_value >> this->_fractionnal);
|
||||
}
|
||||
|
||||
float Fixed::toFloat() const {
|
||||
return (this->_floating_value);
|
||||
return ((float)this->_fixed_value / ( 1 << this->_fractionnal));
|
||||
}
|
||||
|
||||
int Fixed::getRawBits() {
|
||||
int Fixed::getRawBits() const {
|
||||
std::cout << "getRawBits member function called" << std::endl;
|
||||
return this->_fixed_value;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/12/21 19:30:23 by rparodi #+# #+# */
|
||||
/* Updated: 2024/12/23 17:37:34 by rparodi ### ########.fr */
|
||||
/* Updated: 2024/12/27 13:50:06 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -22,12 +22,11 @@
|
|||
Fixed(const int &integer);
|
||||
Fixed(const float &floating);
|
||||
~Fixed();
|
||||
int getRawBits();
|
||||
int getRawBits() const;
|
||||
void setRawBits(const int value);
|
||||
int toInt() const;
|
||||
float toFloat() const;
|
||||
Fixed & operator = (const Fixed &value);
|
||||
friend std::ostream& operator<< (std::ostream &output, const Fixed &value);
|
||||
private:
|
||||
int _integer_value;
|
||||
float _floating_value;
|
||||
|
|
|
|||
|
|
@ -6,25 +6,39 @@
|
|||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/12/23 14:41:58 by rparodi #+# #+# */
|
||||
/* Updated: 2024/12/27 13:11:37 by rparodi ### ########.fr */
|
||||
/* Updated: 2024/12/27 14:01:23 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Fixed.hpp"
|
||||
#include <ostream>
|
||||
|
||||
int main( void ) {
|
||||
Fixed a;
|
||||
Fixed const b( 10 );
|
||||
Fixed const b( -10 );
|
||||
Fixed const c( 42.42f );
|
||||
Fixed const d( b );
|
||||
|
||||
a = Fixed( 1234.4321f );
|
||||
std::cout << std::endl;
|
||||
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 << 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;
|
||||
std::cout << std::endl;
|
||||
std::cout << "a is " << a.toFloat() << " as float" << std::endl;
|
||||
std::cout << "b is " << b.toFloat() << " as float" << std::endl;
|
||||
std::cout << "c is " << c.toFloat() << " as float" << std::endl;
|
||||
std::cout << "d is " << d.toFloat() << " as float" << std::endl;
|
||||
std::cout << std::endl;
|
||||
std::cout << "a is " << a.getRawBits() << " as raw" << std::endl;
|
||||
std::cout << "b is " << b.getRawBits() << " as raw" << std::endl;
|
||||
std::cout << "c is " << c.getRawBits() << " as raw" << std::endl;
|
||||
std::cout << "d is " << d.getRawBits() << " as raw" << std::endl;
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue