kblib 0.2.3
General utilities library for modern C++
stats.cpp
Go to the documentation of this file.
1#include "kblib/stats.h"
2
3#include "catch.hpp"
4
5#include <array>
6#include <cmath>
7#include <iostream>
8
9[[gnu::unused]] static long double phi = 1.6180339887498948482L;
10// not using sqrtl because of https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79700
11[[gnu::unused]] static long double r5 = std::sqrt(5);
12
13TEST_CASE("fibonacci") {
14 std::array<std::uint_least64_t, 41> fibs{
15 0, 1, 1, 2, 3, 5, 8,
16 13, 21, 34, 55, 89, 144, 233,
17 377, 610, 987, 1597, 2584, 4181, 6765,
18 10946, 17711, 28657, 46368, 75025, 121393, 196418,
19 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887,
20 9227465, 14930352, 24157817, 39088169, 63245986, 102334155};
21 for (int i{}; i != fibs.size(); ++i) {
22 REQUIRE(fibs[kblib::to_unsigned(i)] == kblib::fibonacci(i));
23 }
24 /*for (int i{fibs.size()}; i < 93; ++i) {
25 std::cout<<(kblib::fibonacci(i) - std::llroundl(std::powl(phi,
26 i)/r5))<<'\n';
27 }//*/
28}
constexpr auto fibonacci(int n) noexcept -> U
Compile-time table fibonacci function.
Definition: stats.h:177
constexpr auto to_unsigned(I x) -> std::make_unsigned_t< I >
Cast integral argument to corresponding unsigned type.
Definition: fakestd.h:592
TEST_CASE("fibonacci")
Definition: stats.cpp:13
Provides numerical and mathematical utilities.