algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
scalar.hpp
1#pragma once
2
3#include <cmath>
4#include <cstddef>
5#include <cstdint>
6#include <cstring>
7#include <limits>
8
9namespace algotradelib {
10namespace math {
11
18using Scalar = double;
19
28inline Scalar missing() { return std::numeric_limits<Scalar>::quiet_NaN(); }
29
36inline bool observed(Scalar value) { return !std::isnan(value); }
37
51inline Scalar undefined() {
52 // A quiet NaN with a distinguishing payload: exponent all ones, quiet bit set, and a
53 // payload no library produces by accident.
54 static_assert(sizeof(Scalar) == sizeof(std::uint64_t), "undefined() assumes a 64-bit Scalar");
55 const std::uint64_t bits = 0x7FF80000000F1A7EULL;
56 Scalar value;
57 std::memcpy(&value, &bits, sizeof(value));
58 return value;
59}
60
69inline bool is_undefined(Scalar value) {
70 if (!std::isnan(value)) {
71 return false;
72 }
73 std::uint64_t bits{0};
74 std::memcpy(&bits, &value, sizeof(bits));
75 // Compares the payload only, masking off the sign, the exponent, and the quiet bit, so a
76 // negated or re-quieted copy still reads as the same marker.
77 return (bits & 0x0007FFFFFFFFFFFFULL) == 0x000F1A7EULL;
78}
79
80} // namespace math
81} // namespace algotradelib
The numeric vocabulary: Scalar, missing(), and the matrix and vector types.
Definition eigen.hpp:10
bool is_undefined(Scalar value)
Tests whether a value is the undefined() marker.
Definition scalar.hpp:69
double Scalar
The floating-point type the analytics and math layers compute in (double).
Definition scalar.hpp:18
bool observed(Scalar value)
Tests whether a value is an observation.
Definition scalar.hpp:36
Scalar undefined()
Returns the value that stands for "the data was there, but the statistic does not exist".
Definition scalar.hpp:51
Scalar missing()
Returns the value that stands for "no observation" (a quiet NaN).
Definition scalar.hpp:28
The library: broker-neutral types (core), and the modules below.