aboutsummaryrefslogtreecommitdiff
path: root/cpp02/ex01/Fixed.cpp
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2020-10-19 11:30:22 +0200
committerCharles Cabergs <me@cacharle.xyz>2020-10-19 11:30:22 +0200
commita3ff15046fa3d367ff7304dc9482e81597eb76b9 (patch)
treef3c02121bd166dbbeab83653ca9d0ba360eb8478 /cpp02/ex01/Fixed.cpp
parent52a866baba3cbb05ecf0be88f1b50991b4a26b52 (diff)
downloadpiscine_cpp-a3ff15046fa3d367ff7304dc9482e81597eb76b9.tar.gz
piscine_cpp-a3ff15046fa3d367ff7304dc9482e81597eb76b9.tar.bz2
piscine_cpp-a3ff15046fa3d367ff7304dc9482e81597eb76b9.zip
Fixing cpp02/ex01 float/fixed conversions
Diffstat (limited to 'cpp02/ex01/Fixed.cpp')
-rw-r--r--cpp02/ex01/Fixed.cpp24
1 files changed, 9 insertions, 15 deletions
diff --git a/cpp02/ex01/Fixed.cpp b/cpp02/ex01/Fixed.cpp
index 36c76d5..755a6ae 100644
--- a/cpp02/ex01/Fixed.cpp
+++ b/cpp02/ex01/Fixed.cpp
@@ -6,14 +6,13 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/13 11:45:18 by charles #+# #+# */
-/* Updated: 2020/04/13 13:02:27 by charles ### ########.fr */
+/* Updated: 2020/10/19 11:29:39 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "Fixed.hpp"
-Fixed::Fixed()
- : m_value(0)
+Fixed::Fixed() : m_value(0)
{
std::cout << "Default constructor called" << std::endl;
}
@@ -35,20 +34,20 @@ void Fixed::operator=(Fixed const& other)
m_value = other.getRawBits();
}
-Fixed::Fixed(int from)
+Fixed::Fixed(const int from)
{
- std::cout << " constructor called" << std::endl;
+ std::cout << "Int constructor called" << std::endl;
m_value = from << m_fractionalBits;
}
-Fixed::Fixed(float from)
+Fixed::Fixed(const float from)
{
- m_value = 0;
+ std::cout << "Float constructor called" << std::endl;
+ m_value = roundf(from * (1 << m_fractionalBits));
}
int Fixed::getRawBits() const
{
- std::cout << "getRawBits member function called" << std::endl;
return m_value;
}
@@ -60,7 +59,7 @@ void Fixed::setRawBits(int const raw)
float Fixed::toFloat() const
{
- return 0;
+ return (float)m_value / (float)(1 << m_fractionalBits);
}
int Fixed::toInt() const
@@ -75,11 +74,6 @@ int Fixed::getFractionalBits()
std::ostream& operator<<(std::ostream& out, Fixed const& f)
{
- int dec_part;
- int shift_size;
-
- shift_size = (sizeof(int) * 8) - Fixed::getFractionalBits();
- dec_part = f.getRawBits() << shift_size >> shift_size;
- out << f.toInt() << '.' << dec_part;
+ out << f.toFloat();
return out;
}