Fixed.class.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* ************************************************************************** */
  2. /* */
  3. /* ::: :::::::: */
  4. /* Fixed.class.cpp :+: :+: :+: */
  5. /* +:+ +:+ +:+ */
  6. /* By: bchanot <bchanot@gmail.fr> +#+ +:+ +#+ */
  7. /* +#+#+#+#+#+ +#+ */
  8. /* Created: 2025/07/16 16:07:34 by bchanot #+# #+# */
  9. /* Updated: 2025/07/17 17:13:55 by bchanot ### ########.fr */
  10. /* */
  11. /* ************************************************************************** */
  12. #include "Fixed.class.hpp"
  13. #include <iostream>
  14. #include <cmath>
  15. Fixed::Fixed(void) : _value(0) {
  16. std::cout << "Default constructor called" << std::endl;
  17. return ;
  18. }
  19. Fixed::Fixed(int const num) : _value((int)roundf(num * (1 << _frac))){
  20. std::cout << "Int constructor called" << std::endl;
  21. return ;
  22. }
  23. Fixed::Fixed(float const num) : _value((float)roundf(num * (1 << _frac))){
  24. std::cout << "Float constructor called" << std::endl;
  25. return ;
  26. }
  27. Fixed::Fixed(Fixed const &src) {
  28. *this = src;
  29. std::cout << "Copy constructor called" << std::endl;
  30. return ;
  31. }
  32. Fixed::~Fixed(void) {
  33. std::cout << "Destructor called" << std::endl;
  34. return ;
  35. }
  36. int Fixed::getRawBits(void) const {
  37. std::cout << "getRawBits member function called" << std::endl;
  38. return this->_value;
  39. }
  40. void Fixed::setRawBits(int const raw) {
  41. std::cout << "setRawBits member function called" << std::endl;
  42. this->_value = raw;
  43. return ;
  44. }
  45. Fixed &Fixed::operator=(Fixed const &rhs) {
  46. std::cout << "Copy assignment operator called" << std::endl;
  47. this->_value = rhs.getRawBits();
  48. return *this;
  49. }
  50. float Fixed::toFloat(void) const {
  51. return (float(this->_value) / float(1 << this->_frac));
  52. }
  53. int Fixed::toInt(void) const {
  54. return (int(this->_value) / int(1 << this->_frac));
  55. }
  56. std::ostream &operator<<(std::ostream &o, Fixed const &i) {
  57. o << i.toFloat();
  58. return o;
  59. }