6#include "TTauri/Foundation/required.hpp"
7#include "TTauri/Foundation/string_tag.hpp"
8#include "TTauri/Foundation/safe_int.hpp"
14template<
typename T,
int M>
17 static constexpr int multiplier = M;
28 template<
typename O, std::enable_if_t<std::is_
floating_po
int_v<O>,
int> = 0>
29 explicit constexpr fixed(O other) noexcept :
30 value(
static_cast<T
>(other * M)) {
37 template<
typename O, std::enable_if_t<std::is_
integral_v<O>,
int> = 0>
38 explicit constexpr fixed(O other) noexcept :
39 value(
static_cast<T
>(other) * M) {
49 template<
typename O, std::enable_if_t<std::is_
floating_po
int_v<O>,
int> = 0>
50 constexpr fixed &operator=(O other)
noexcept {
51 value =
static_cast<T
>(other * M);
59 template<
typename O, std::enable_if_t<std::is_
integral_v<O>,
int> = 0>
60 constexpr fixed &operator=(O other)
noexcept {
61 value =
static_cast<T
>(other) * M;
70 value =
static_cast<T
>(stod(other) * M);
78 template<
typename O, std::enable_if_t<std::is_
floating_po
int_v<O>,
int> = 0>
79 explicit operator O ()
const noexcept {
80 return static_cast<O
>(value) / M;
83 template<
typename O, std::enable_if_t<std::is_
integral_v<O>,
int> = 0>
84 explicit operator O ()
const noexcept {
85 return static_cast<O
>(value / M);
89 return fmt::format(
"{}",
static_cast<double>(value) / M);
92 static fixed fromValue(T value)
noexcept {
99template<
typename T,
int M>
inline bool operator==(
fixed<T,M> const &lhs,
fixed<T,M> const &rhs) {
return lhs.value == rhs.value; }
100template<
typename T,
int M>
inline bool operator!=(fixed<T,M>
const &lhs, fixed<T,M>
const &rhs) {
return lhs.value != rhs.value; }
101template<
typename T,
int M>
inline bool operator<(fixed<T,M>
const &lhs, fixed<T,M>
const &rhs) {
return lhs.value < rhs.value; }
102template<
typename T,
int M>
inline bool operator>(fixed<T,M>
const &lhs, fixed<T,M>
const &rhs) {
return lhs.value > rhs.value; }
103template<
typename T,
int M>
inline bool operator<=(fixed<T,M>
const &lhs, fixed<T,M>
const &rhs) {
return lhs.value <= rhs.value; }
104template<
typename T,
int M>
inline bool operator>=(fixed<T,M>
const &lhs, fixed<T,M>
const &rhs) {
return lhs.value >= rhs.value; }
106template<
typename T,
int M>
107fixed<T,M> operator+(fixed<T,M>
const &lhs, fixed<T,M>
const &rhs)
109 return fixed<T,M>::fromValue(lhs.value + rhs.value);
112template<
typename T,
int M>
113fixed<T,M> operator-(fixed<T,M>
const &lhs, fixed<T,M>
const &rhs)
115 return fixed<T,M>::fromValue(lhs.value - rhs.value);
118template<
typename T,
int M>
124template<
typename T,
int M>
127 return lhs << rhs.string();
130using money = fixed<safe_int<int64_t>,100>;