AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
AFraction.h
1/*
2 * AUI Framework - Declarative UI toolkit for modern C++20
3 * Copyright (C) 2020-2024 Alex2772 and Contributors
4 *
5 * SPDX-License-Identifier: MPL-2.0
6 *
7 * This Source Code Form is subject to the terms of the Mozilla Public
8 * License, v. 2.0. If a copy of the MPL was not distributed with this
9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 */
11
12//
13// Created by Alex2772 on 11/20/2021.
14//
15#pragma once
16
20class AFraction {
21private:
22 int mNumerator;
23 int mDenominator;
24
25public:
26 AFraction(int numerator, int denominator) : mNumerator(numerator), mDenominator(denominator) {}
27 AFraction(int number): mNumerator(number), mDenominator(1) {}
28
29 AFraction& operator+=(int number) {
30 mNumerator += number * mDenominator;
31 return *this;
32 }
33
34 AFraction operator+(int number) const {
35 auto copy = *this;
36 copy += number;
37 return copy;
38 }
39
40 AFraction& operator-=(int number) {
41 mNumerator -= number * mDenominator;
42 return *this;
43 }
44
45 AFraction operator-(int number) const {
46 auto copy = *this;
47 copy -= number;
48 return copy;
49 }
50
51 AFraction& operator*=(int number) {
52 mNumerator *= number;
53 return *this;
54 }
55
56 AFraction operator*(int number) const {
57 auto copy = *this;
58 copy *= number;
59 return copy;
60 }
61
62 AFraction& operator/=(int number) {
63 mDenominator *= number;
64 return *this;
65 }
66
67 AFraction operator/(int number) const {
68 auto copy = *this;
69 copy /= number;
70 return copy;
71 }
72
73 [[nodiscard]]
74 int toInt() const {
75 return mNumerator / mDenominator;
76 }
77
78 [[nodiscard]]
79 float toFloat() const {
80 return float(mNumerator) / float(mDenominator);
81 }
82 [[nodiscard]]
83 double toDouble() const {
84 return double(mNumerator) / double(mDenominator);
85 }
86};