Fixed.class.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 18:55:07 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. bool Fixed::operator==(Fixed const &rhs) const {
  51. return this->toFloat() == rhs.toFloat();
  52. }
  53. bool Fixed::operator!=(Fixed const &rhs) const {
  54. return this->toFloat() != rhs.toFloat();
  55. }
  56. bool Fixed::operator<=(Fixed const &rhs) const {
  57. return this->toFloat() <= rhs.toFloat();
  58. }
  59. bool Fixed::operator>=(Fixed const &rhs) const {
  60. return this->toFloat() >= rhs.toFloat();
  61. }
  62. bool Fixed::operator<(Fixed const &rhs) const {
  63. return this->toFloat() < rhs.toFloat();
  64. }
  65. bool Fixed::operator>(Fixed const &rhs) const {
  66. return this->toFloat() > rhs.toFloat();
  67. }
  68. Fixed Fixed::operator+(Fixed const &rhs) const {
  69. return Fixed::toFloat() + rhs.toFloat();
  70. }
  71. Fixed Fixed::operator-(Fixed const &rhs) const {
  72. return Fixed::toFloat() - rhs.toFloat();
  73. }
  74. Fixed Fixed::operator*(Fixed const &rhs) const {
  75. return Fixed::toFloat() * rhs.toFloat();
  76. }
  77. Fixed Fixed::operator/(Fixed const &rhs) const {
  78. return Fixed::toFloat() / rhs.toFloat();
  79. }
  80. Fixed &Fixed::operator++(void) {
  81. this->_value++;
  82. return *this;
  83. }
  84. Fixed &Fixed::operator--(void) {
  85. this->_value--;
  86. return *this;
  87. }
  88. Fixed Fixed::operator++(int) {
  89. Fixed other(*this);
  90. operator++();
  91. return other;
  92. }
  93. Fixed Fixed::operator--(int) {
  94. Fixed other(*this);
  95. operator--();
  96. return other;
  97. }
  98. const Fixed &Fixed::min(Fixed const &left, Fixed const &right) {
  99. return left < right ? left : right;
  100. }
  101. Fixed &Fixed::min(Fixed &left, Fixed &right) {
  102. return left < right ? left : right;
  103. }
  104. const Fixed &Fixed::max(Fixed const &left, Fixed const &right) {
  105. return left > right ? left : right;
  106. }
  107. Fixed &Fixed::max(Fixed &left, Fixed &right) {
  108. return left > right ? left : right;
  109. }
  110. float Fixed::toFloat(void) const {
  111. return (float(this->_value) / float(1 << this->_frac));
  112. }
  113. int Fixed::toInt(void) const {
  114. return (int(this->_value) / int(1 << this->_frac));
  115. }
  116. std::ostream &operator<<(std::ostream &o, Fixed const &i) {
  117. o << i.toFloat();
  118. return o;
  119. }