aboutsummaryrefslogtreecommitdiff
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
parent52a866baba3cbb05ecf0be88f1b50991b4a26b52 (diff)
downloadpiscine_cpp-a3ff15046fa3d367ff7304dc9482e81597eb76b9.tar.gz
piscine_cpp-a3ff15046fa3d367ff7304dc9482e81597eb76b9.tar.bz2
piscine_cpp-a3ff15046fa3d367ff7304dc9482e81597eb76b9.zip
Fixing cpp02/ex01 float/fixed conversions
-rw-r--r--cpp02/ex00/Fixed.hpp5
-rw-r--r--cpp02/ex01/Fixed.cpp24
-rw-r--r--cpp02/ex01/Fixed.hpp11
-rw-r--r--cpp02/ex01/main.cpp2
4 files changed, 19 insertions, 23 deletions
diff --git a/cpp02/ex00/Fixed.hpp b/cpp02/ex00/Fixed.hpp
index 1f956d7..2ffe775 100644
--- a/cpp02/ex00/Fixed.hpp
+++ b/cpp02/ex00/Fixed.hpp
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/13 11:40:49 by charles #+# #+# */
-/* Updated: 2020/04/13 11:53:30 by charles ### ########.fr */
+/* Updated: 2020/10/19 08:27:01 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -22,7 +22,8 @@ public:
Fixed(Fixed const& other);
~Fixed();
void operator=(Fixed const& other);
- int getRawBits() const;
+
+ int getRawBits() const;
void setRawBits(int const raw);
private:
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;
}
diff --git a/cpp02/ex01/Fixed.hpp b/cpp02/ex01/Fixed.hpp
index 2dd2d7d..726b810 100644
--- a/cpp02/ex01/Fixed.hpp
+++ b/cpp02/ex01/Fixed.hpp
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/13 11:40:49 by charles #+# #+# */
-/* Updated: 2020/04/13 12:58:33 by charles ### ########.fr */
+/* Updated: 2020/10/19 11:29:30 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,6 +14,7 @@
# define FIXED_HPP
# include <iostream>
+# include <cmath>
class Fixed
{
@@ -23,14 +24,14 @@ public:
~Fixed();
void operator=(Fixed const& other);
- Fixed(int from);
- Fixed(float from);
+ Fixed(const int from);
+ Fixed(const float from);
- int getRawBits() const;
+ int getRawBits() const;
void setRawBits(int const raw);
float toFloat() const;
- int toInt() const;
+ int toInt() const;
static int getFractionalBits();
diff --git a/cpp02/ex01/main.cpp b/cpp02/ex01/main.cpp
index 44ffe34..34d365a 100644
--- a/cpp02/ex01/main.cpp
+++ b/cpp02/ex01/main.cpp
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/13 11:51:15 by charles #+# #+# */
-/* Updated: 2020/04/13 12:08:07 by charles ### ########.fr */
+/* Updated: 2020/10/19 11:28:50 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */