aboutsummaryrefslogtreecommitdiff
path: root/cpp02/ex01/Fixed.cpp
blob: 759807b3f81113a0d28f4714d794b67d12c04201 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   Fixed.cpp                                          :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: charles <charles.cabergs@gmail.com>        +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/04/13 11:45:18 by charles           #+#    #+#             */
/*   Updated: 2020/11/10 09:59:45 by cacharle         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "Fixed.hpp"

Fixed::Fixed() : m_value(0)
{
    std::cout << "Default constructor called" << std::endl;
}

Fixed::Fixed(Fixed const& other)
{
    std::cout << "Copy constructor called" << std::endl;
    *this = other;
}

Fixed::~Fixed()
{
    std::cout << "Destructor called" << std::endl;
}

Fixed& Fixed::operator=(Fixed const& other)
{
    std::cout << "Assignation operator called" << std::endl;
    m_value = other.getRawBits();
    return *this;
}

Fixed::Fixed(const int from)
{
    std::cout << "Int constructor called" << std::endl;
    m_value = from << m_fractionalBits;
}

Fixed::Fixed(const float from)
{
    std::cout << "Float constructor called" << std::endl;
    m_value = roundf(from * (1 << m_fractionalBits));
}

int Fixed::getRawBits() const
{
    return m_value;
}

void Fixed::setRawBits(int const raw)
{
    std::cout << "setRawBits member function called" << std::endl;
    m_value = raw;
}

float Fixed::toFloat() const
{
    return (float)m_value / (float)(1 << m_fractionalBits);
}

int Fixed::toInt() const
{
    return roundf(toFloat());
}

int Fixed::getFractionalBits()
{
    return m_fractionalBits;
}

std::ostream& operator<<(std::ostream& out, Fixed const& f)
{
    out << f.toFloat();
    return out;
}