Compare commits

..

5 Commits

Author SHA1 Message Date
8df7eb638b add math fuction 2026-04-09 16:23:37 +03:00
7e85152094 fix test ssh 2026-03-28 21:14:32 +03:00
d645d7223b test ssh 2026-03-28 21:14:19 +03:00
b77a7b9149 add max abs 2026-03-16 15:32:22 +03:00
5fc6b43ea0 fix run 2026-02-26 09:29:08 +03:00
4 changed files with 30 additions and 31 deletions

View File

@@ -11,6 +11,11 @@ auto main(int argc, char *argv[]) -> int
hack::log()(hack::math::min(4, 5));
hack::log()(hack::math::min(c, b));
int a1 = -4, b1 = -5;
hack::log()(hack::math::max_abs(a1, b1));
// 1, 2, 3, 4, 5
hack::log()(hack::math::sum_arithmetic_progression(1, 1, 5));
return 0;
}

View File

@@ -2,10 +2,10 @@ executable(
meson.project_name(),
# 'examples/audio/main.cpp',
# 'examples/concepts/main.cpp',
# 'examples/math/main.cpp',
'examples/math/main.cpp',
# 'examples/range/main.cpp',
# 'examples/patterns/main.cpp',
'examples/logger/main.cpp',
# 'examples/logger/main.cpp',
# 'examples/exception/main.cpp',
# 'examples/comparators/main.cpp',
dependencies : deps,

29
run.sh
View File

@@ -1,29 +0,0 @@
#!/bin/zsh
PROJECT_NAME=$(basename $PWD)
run() {
command meson compile -C build
cd build
./bin/$PROJECT_NAME
cd ..
}
# run test [name_test]
# example: run test pattrens
if [[ "$1" == "test" ]]; then
echo ""
meson test $2 -C build
echo ""
awk '/^-------------------------------------------------------------------------------/{flag=1} /===============================================================================/{flag=0} flag' ./build/meson-logs/testlog.txt
elif [[ "$1" == "tests" ]]; then
echo ""
meson test -C build
echo ""
# awk '/^-------------------------------------------------------------------------------/{flag=1} /===============================================================================/{flag=0} flag' ./build/meson-logs/testlog.txt
elif [[ -d "build" ]]; then
run
else
command meson setup build
run
fi

View File

@@ -1,6 +1,7 @@
#pragma once
#include <type_traits>
#include <math.h>
namespace hack::math
{
@@ -18,4 +19,26 @@ namespace hack::math
{
return a < b ? a : b;
}
template<typename T, typename U, typename RT = std::common_type_t<T, U>>
inline RT max_abs(T a, U b)
{
return std::abs(a) > std::abs(b) ? a : b;
}
template<typename T, typename U, typename RT = std::common_type_t<T, U>>
inline RT min_abs(T a, U b)
{
return std::abs(a) < std::abs(b) ? a : b;
}
// Функция для вычисления суммы арифметической прогрессии
// a1 - первый член
// d - разность (шаг)
// n - количество членов
inline long long sum_arithmetic_progression(long long a1, long long d, std::size_t n) {
// Формула суммы: S = n * (2*a1 + (n-1)*d) / 2
// см. wiki/math
return n * (2 * a1 + (n - 1) * d) / 2;
}
}