FuncSketch
Loading...
Searching...
No Matches
binary_operators.h
Go to the documentation of this file.
1/*
2 * Copyright 2026 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
20#pragma once
21
22#include <string_view>
23
27
28namespace func_sketch::math {
29
34public:
40 [[nodiscard]] static std::string_view name() noexcept { return "add"; }
41
49 static void operator()(
50 const Number& left, const Number& right, Number& result) {
51 std::visit(
52 [&result](const auto& left_value, const auto& right_value) {
53 using ResultType =
54 CommonType<decltype(left_value), decltype(right_value)>;
55 result = static_cast<ResultType>(left_value) +
56 static_cast<ResultType>(right_value);
57 },
58 left, right);
59 }
60};
61
66public:
72 [[nodiscard]] static std::string_view name() noexcept { return "sub"; }
73
81 static void operator()(
82 const Number& left, const Number& right, Number& result) {
83 std::visit(
84 [&result](const auto& left_value, const auto& right_value) {
85 using ResultType =
86 CommonType<decltype(left_value), decltype(right_value)>;
87 result = static_cast<ResultType>(left_value) -
88 static_cast<ResultType>(right_value);
89 },
90 left, right);
91 }
92};
93
98public:
104 [[nodiscard]] static std::string_view name() noexcept { return "mul"; }
105
113 static void operator()(
114 const Number& left, const Number& right, Number& result) {
115 std::visit(
116 [&result](const auto& left_value, const auto& right_value) {
117 using ResultType =
118 CommonType<decltype(left_value), decltype(right_value)>;
119 result = static_cast<ResultType>(left_value) *
120 static_cast<ResultType>(right_value);
121 },
122 left, right);
123 }
124};
125
130public:
136 [[nodiscard]] static std::string_view name() noexcept { return "div"; }
137
145 static void operator()(
146 const Number& left, const Number& right, Number& result) {
147 std::visit(
148 [&result](const auto& left_value, const auto& right_value) {
149 using ResultType =
150 CommonType<decltype(left_value), decltype(right_value)>;
151 result = static_cast<ResultType>(left_value) /
152 static_cast<ResultType>(right_value);
153 },
154 left, right);
155 }
156};
157
162public:
168 [[nodiscard]] static std::string_view name() noexcept { return "pow"; }
169
177 static void operator()(
178 const Number& left, const Number& right, Number& result) {
179 std::visit(
180 [&result](const auto& left_value, const auto& right_value) {
181 result = pow_number(left_value, right_value);
182 },
183 left, right);
184 }
185};
186
187} // namespace func_sketch::math
Class of addition operator.
static void operator()(const Number &left, const Number &right, Number &result)
Operate on a pair of scalars.
static std::string_view name() noexcept
Get the name of the operator.
Class of division operator.
static std::string_view name() noexcept
Get the name of the operator.
static void operator()(const Number &left, const Number &right, Number &result)
Operate on a pair of scalars.
Class of multiplication operator.
static void operator()(const Number &left, const Number &right, Number &result)
Operate on a pair of scalars.
static std::string_view name() noexcept
Get the name of the operator.
static void operator()(const Number &left, const Number &right, Number &result)
Operate on a pair of scalars.
static std::string_view name() noexcept
Get the name of the operator.
Class of subtraction operator.
static std::string_view name() noexcept
Get the name of the operator.
static void operator()(const Number &left, const Number &right, Number &result)
Operate on a pair of scalars.
Definition of CommonType type.
std::common_type_t< T1, T2 > CommonType
Get common type of two types.
Definition common_type.h:34
Definitions of common types.
std::variant< Integer, Real, Complex > Number
Type of numbers.
Definition of pow_number function.
auto pow_number(Base base, Exponent exponent)
Compute power for number types in this library.
Definition pow_number.h:38