Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ceil implementation #30

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions include/ccmath/math/nearest/ceil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,72 @@

#pragma once

#include "ccmath/math/compare/isinf.hpp"
#include "ccmath/math/compare/isnan.hpp"

#include <limits>
#include <type_traits>

namespace ccm
{
/**
* @brief Computes the smallest integer value no lesser than num.
* @tparam T The type of the number.
* @param num A floating-point or integer value.
* @return If no errors occur, the smallest integer value no lesser than num, that is ⌈num⌉, is returned.
*/
template <typename T>
constexpr T ceil(T num) noexcept
{
if constexpr (std::is_floating_point_v<T>)
{
// If num is NaN, NaN is returned.
if (ccm::isnan(num)) { return std::numeric_limits<T>::quiet_NaN(); }

// If num is ±∞ or ±0, num is returned, unmodified.
if (ccm::isinf(num) || num == static_cast<T>(0)) { return num; }

// Compute the largest integer value not greater than num.
constexpr T num_floored = static_cast<T>(static_cast<long long int>(num));

// if num_floored is not equal to num, num_floored incremented is returned
if constexpr (num_floored != num) { return num_floored + 1; }
}

// if num is an integer, num is returned, unmodified
if constexpr (std::is_integral_v<T>) { return num; }
}

/**
* @brief Computes the smallest integer value no lesser than num.
* @param num An integer value.
* @return If no errors occur, the smallest integer value no lesser than num, that is ⌈num⌉, is returned.
*/
template <typename Integer, std::enable_if_t<std::is_integral_v<Integer>, bool> = true>
constexpr double ceil(Integer num) noexcept
{
return static_cast<double>(num); // All integers already have a ceil value. Just cast to double and return.
}

/**
* @brief Computes the smallest integer value no lesser than num.
* @param num A floating-point value.
* @return If no errors occur, the smallest integer value no lesser than num, that is ⌈num⌉, is returned.
*/
constexpr float ceilf(float num) noexcept
{
return ceil<float>(num);
}

/**
* @brief Computes the smallest integer value no lesser than num.
* @param num A floating-point value.
* @return If no errors occur, the smallest integer value no lesser than num, that is ⌈num⌉, is returned.
*/
constexpr double ceill(double num) noexcept
{
return ceil<double>(num);
}
} // namespace ccm

/// @ingroup nearest
2 changes: 1 addition & 1 deletion include/ccmath/math/nearest/floor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace ccm

/**
* @brief Computes the largest integer value not greater than num.
* @param num A integer value.
* @param num An integer value.
* @return If no errors occur, the largest integer value not greater than num, that is ⌊num⌋, is returned.
*/
template <typename Integer, std::enable_if_t<std::is_integral_v<Integer>, bool> = true>
Expand Down
Loading