Skip to content

Commit a6e91d2

Browse files
authored
Merge pull request #34 from Miniongolf/Number_double_operators
✨ Number and double operators
2 parents 0f2d2d5 + 42d0398 commit a6e91d2

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed

include/units/units.hpp

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ void quantityChecker(Quantity<Mass, Length, Time, Current, Angle, Temperature, L
130130
template <typename Q>
131131
concept isQuantity = requires(Q q) { quantityChecker(q); };
132132

133-
// Isomorphic concept - used to ensure unit equivalecy
133+
// Isomorphic concept - used to ensure unit equivalency
134134
template <typename Q, typename... Quantities>
135135
concept Isomorphic = ((std::convertible_to<Q, Quantities> && std::convertible_to<Quantities, Q>) && ...);
136136

@@ -357,6 +357,57 @@ constexpr inline Number from_num(double value) { return Number(value); }
357357

358358
constexpr inline double to_num(Number quantity) { return quantity.internal(); }
359359

360+
#define NEW_NUM_TO_DOUBLE_COMPARISON(op) \
361+
constexpr bool operator op(Number lhs, double rhs) { return (lhs.internal() op rhs); } \
362+
constexpr bool operator op(double lhs, Number rhs) { return (lhs op rhs.internal()); }
363+
364+
NEW_NUM_TO_DOUBLE_COMPARISON(==)
365+
NEW_NUM_TO_DOUBLE_COMPARISON(!=)
366+
NEW_NUM_TO_DOUBLE_COMPARISON(<=)
367+
NEW_NUM_TO_DOUBLE_COMPARISON(>=)
368+
NEW_NUM_TO_DOUBLE_COMPARISON(<)
369+
NEW_NUM_TO_DOUBLE_COMPARISON(>)
370+
371+
#define NEW_NUM_AND_DOUBLE_OPERATION(op) \
372+
constexpr Number operator op(Number lhs, double rhs) { return (lhs.internal() op rhs); } \
373+
constexpr Number operator op(double lhs, Number rhs) { return (lhs op rhs.internal()); }
374+
375+
NEW_NUM_AND_DOUBLE_OPERATION(+)
376+
NEW_NUM_AND_DOUBLE_OPERATION(-)
377+
NEW_NUM_AND_DOUBLE_OPERATION(*)
378+
NEW_NUM_AND_DOUBLE_OPERATION(/)
379+
380+
#define NEW_NUM_AND_DOUBLE_ASSIGNMENT(op) \
381+
constexpr void operator op##=(Number& lhs, double rhs) { lhs = lhs.internal() op rhs; } \
382+
constexpr void operator op##=(double& lhs, Number rhs) { lhs = lhs op rhs.internal(); }
383+
384+
NEW_NUM_AND_DOUBLE_ASSIGNMENT(+)
385+
NEW_NUM_AND_DOUBLE_ASSIGNMENT(-)
386+
NEW_NUM_AND_DOUBLE_ASSIGNMENT(*)
387+
NEW_NUM_AND_DOUBLE_ASSIGNMENT(/)
388+
389+
constexpr Number& operator++(Number& lhs, int) {
390+
lhs += 1;
391+
return lhs;
392+
}
393+
394+
constexpr Number operator++(Number& lhs) {
395+
Number copy = lhs;
396+
lhs += 1;
397+
return copy;
398+
}
399+
400+
constexpr Number& operator--(Number& lhs, int) {
401+
lhs -= 1;
402+
return lhs;
403+
}
404+
405+
constexpr Number operator--(Number& lhs) {
406+
Number copy = lhs;
407+
lhs -= 1;
408+
return copy;
409+
}
410+
360411
NEW_UNIT_LITERAL(Number, percent, num / 100)
361412

362413
NEW_UNIT(Mass, kg, 1, 0, 0, 0, 0, 0, 0, 0)

src/main.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,34 @@ void angleTests() {
5151
static_assert(r2i(to_stDeg(30_cDeg)) == r2i(to_stDeg(60_stDeg)));
5252
static_assert(r2i(to_stDeg(+0_cDeg)) == r2i(to_stDeg(90_stDeg)));
5353
Angle a = 2_cDeg;
54+
}
55+
56+
constexpr Number numAssignmentTests() {
57+
Number n = 1_num; // 1
58+
n += 2; // 3
59+
n--; // 2
60+
n -= 3; // -1
61+
n *= 2; // -2
62+
n /= 2; // -1
63+
n++; // 0
64+
return n;
65+
}
66+
67+
constexpr double doubleAssignmentTests() {
68+
double d = 1; // 1
69+
d += 2_num; // 3
70+
d -= 2_num; // 1
71+
d *= 2_num; // 2
72+
d /= 2_num; // 1
73+
return d;
74+
}
75+
76+
77+
void numberOperatorTests() {
78+
static_assert(1_num + 2 == 3);
79+
static_assert(1 + 2_num <= 3);
80+
static_assert(1 / 2_num >= 0);
81+
82+
static_assert(numAssignmentTests() == 0);
83+
static_assert(doubleAssignmentTests() == 1);
5484
}

0 commit comments

Comments
 (0)