numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
trunc.h
Go to the documentation of this file.
1/*
2 * Copyright 2021 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 <cstdint>
23#include <limits>
24
25#include "num_collect/constants/zero.h" // IWYU pragma: keep
26
27namespace num_collect::constants {
28
42template <typename T>
43constexpr auto trunc(T x) -> T {
44 if (x < zero<T>) {
45 return -trunc(-x);
46 }
47 if (x > static_cast<T>(std::numeric_limits<std::uintmax_t>::max())) {
48 return x;
49 }
50 return static_cast<T>(static_cast<std::uintmax_t>(x));
51}
52
53} // namespace num_collect::constants
Namespace of constexpr variables and functions.
Definition cbrt.h:24
constexpr T zero
Value 0.
Definition zero.h:30
constexpr auto trunc(T x) -> T
Truncate the decimal part of a number x.
Definition trunc.h:43
Definition of zero.