Core Math Functions#

File: samarium/math/math.hpp

namespace math#

Functions

template<concepts::FloatingPoint T>
constexpr auto almost_equal(T a, T b, f64 threshold = epsilon) noexcept#

Check if 2 floating point values are equal.

Template Parameters:

T – Floating point type

Parameters:
  • a – First value

  • b – Second value

  • threshold – threshold within which to compare a and b, by default sm::math::epsilon

Returns:

true if a and b are within threshold else false

template<typename T>
constexpr auto min(T value0, T value1) noexcept#

Minimum of 2 values.

Template Parameters:

T

Parameters:
  • value0 – First value

  • value1 – Second value

Returns:

T

template<typename T>
constexpr auto max(T value0, T value1) noexcept#

Maximum of 2 values.

Template Parameters:

T

Parameters:
  • value0 – First value

  • value1 – Second value

Returns:

T

template<u32 n>
constexpr auto power(auto x) noexcept#

Raise a number to a fixed power.

Template Parameters:

n – Exponent

Parameters:

x – Base

Returns:

auto

constexpr auto to_degrees(concepts::FloatingPoint auto angle) noexcept#

Convert from radians to degrees.

Parameters:

angle – Angle in radians

Returns:

auto

constexpr auto to_radians(concepts::FloatingPoint auto angle) noexcept#

Convert from degrees to radians.

Parameters:

angle – Angle in degrees

Returns:

auto

template<concepts::Number T>
constexpr auto abs(T x) noexcept#

Absolute value of a value.

Template Parameters:

T – A numeric type

Parameters:

x – Value

Returns:

T

template<typename T>
constexpr auto sign(T x, std::false_type) noexcept -> i32#

Sign of an unsigned type.

Template Parameters:

T – Unsigned Type

Parameters:

x – Value

Returns:

1 if x is greater than 0 else 0

template<typename T>
constexpr auto sign(T x, std::true_type) noexcept -> i32#

Sign of a signed type.

Template Parameters:

T – Signed type

Parameters:

x – Value

Returns:

-1 if x < 0, 1 if x > 0, 0 is x == 0

template<typename T>
constexpr auto sign(T x) noexcept -> i32#

Sign of a value.

Template Parameters:

T – Numeric type

Parameters:

x – Value

Returns:

-1 if x < 0, 1 if x > 0, 0 is x == 0

template<concepts::FloatingPoint T>
constexpr auto mod(T x, T y) noexcept#

Modulus of 2 floating-point values (see https://stackoverflow.com/a/67098028)

Template Parameters:

T

Parameters:
  • x – First value

  • y – Second value

template<concepts::FloatingPoint T>
constexpr auto wrap_max(T x, T max)#

Wrap x to range [0, max) (see https://stackoverflow.com/a/29871193/17100530)

Template Parameters:

T

Parameters:
  • x – Value

  • max – Upper limit of range

template<concepts::FloatingPoint T>
constexpr auto wrap_min_max(T x, T min, T max)#

Wrap x to range [min, max) (see https://stackoverflow.com/a/29871193/17100530)

Template Parameters:

T

Parameters:
  • x – Value

  • min – Lower limit of range

  • max – Upper limit of range

template<typename T>
constexpr auto round_to_nearest(T value, T target)#

Round value to nearest multiple of target.

Template Parameters:

T

Parameters:
  • value

  • target

template<typename T>
constexpr auto ceil_to_nearest(T value, T target)#

Ceil value to nearest multiple of target.

Template Parameters:

T

Parameters:
  • value

  • target

template<typename T>
constexpr auto floor_to_nearest(T value, T target)#

Floor value to nearest multiple of target.

Template Parameters:

T

Parameters:
  • value

  • target

template<typename Input, typename Output>
auto sample(FunctionRef<Output(Input)> function, Input from, Input to, u64 steps)#

Sample a function at n values.

Template Parameters:
  • Input – Type of input to function

  • Output – Return type of function

Parameters:
  • function – Input function to sample

  • from – Start of range (inclusive)

  • to – End of range (exclusive)

  • steps – Number of times to sample

Returns:

std::vector<Output>

template<typename Output = f64, typename Input = f64>
auto integral(FunctionRef<Output(Input)> function, Input from, Input to, u64 steps)#

Integrate a function by sampling at n values and summing.

Template Parameters:
  • Input – Type of input to function

  • Output – Return type of function

Parameters:
  • function – Input function to sample

  • from – Start of range (inclusive)

  • to – End of range (exclusive)

  • steps – Number of times to sample

Returns:

std::vector<Output>

constexpr auto distance(Vector2 p1, Vector2 p2) noexcept#
constexpr auto distance_sq(Vector2 p1, Vector2 p2) noexcept#
constexpr auto within_distance(Vector2 p1, Vector2 p2, f64 distance) noexcept#
constexpr auto lerp_along(Vector2 point, const LineSegment &ls) noexcept#
constexpr auto clamped_lerp_along(Vector2 point, const LineSegment &ls) noexcept#
constexpr auto project(Vector2 point, const LineSegment &ls) noexcept#
constexpr auto project_clamped(Vector2 point, const LineSegment &ls) noexcept#
constexpr auto distance(Vector2 point, const LineSegment &ls) noexcept#
constexpr auto clamped_distance(Vector2 point, const LineSegment &ls) noexcept#
constexpr auto lies_in_segment(Vector2 point, const LineSegment &l) noexcept#
constexpr auto intersection(const LineSegment &l1, const LineSegment &l2) noexcept -> std::optional<Vector2>#
inline auto clamped_intersection(const LineSegment &l1, const LineSegment &l2) noexcept -> std::optional<Vector2>#
inline auto intersection(const LineSegment &line_segment, const BoundingBox<f64> &box)#
template<typename T>
constexpr auto area(BoundingBox<T> bounding_box) noexcept#
constexpr auto area(Circle circle)#
template<typename T>
constexpr auto abs_area(BoundingBox<T> bounding_box) noexcept#
constexpr auto abs_area(Circle circle) noexcept#
template<u32 point_count, concepts::FloatingPoint Float = f64>
constexpr auto regular_polygon_points() noexcept#
template<u32 point_count, concepts::FloatingPoint Float = f64>
constexpr auto regular_polygon_points(Circle circumcircle) noexcept#
template<concepts::FloatingPoint Float = f64>
constexpr auto regular_polygon_points(u32 point_count) noexcept#
template<concepts::FloatingPoint Float = f64>
constexpr auto regular_polygon_points(u32 point_count, Circle circumcircle) noexcept#

Variables

static const auto two_pi_i = 2.0 * pi * std::complex<f64>{0.0, 1.0}#
constexpr auto epsilon = 1.e-4#
constexpr auto pi = std::numbers::pi#
constexpr auto two_thirds_pi = 2.0 * pi / 3.0#
constexpr auto two_pi = 2.0 * pi#
constexpr auto e = std::numbers::e#
constexpr auto sqrt2 = std::numbers::sqrt2#