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() {
29 return std::numeric_limits<Scalar>::quiet_NaN();
30}
31
38inline bool observed(Scalar value) {
39 return !std::isnan(value);
40}
41
55inline Scalar undefined() {
56 // A quiet NaN with a distinguishing payload: exponent all ones, quiet bit set, and a
57 // payload no library produces by accident.
58 static_assert(sizeof(Scalar) == sizeof(std::uint64_t), "undefined() assumes a 64-bit Scalar");
59 const std::uint64_t bits = 0x7FF80000000F1A7EULL;
60 Scalar value;
61 std::memcpy(&value, &bits, sizeof(value));
62 return value;
63}
64
73inline bool is_undefined(Scalar value) {
74 if (!std::isnan(value)) {
75 return false;
76 }
77 std::uint64_t bits{0};
78 std::memcpy(&bits, &value, sizeof(bits));
79 // Compares the payload only, masking off the sign, the exponent, and the quiet bit, so a
80 // negated or re-quieted copy still reads as the same marker.
81 return (bits & 0x0007FFFFFFFFFFFFULL) == 0x000F1A7EULL;
82}
83
84} // namespace math
85} // 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:73
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:38
Scalar undefined()
Returns the value that stands for "the data was there, but the statistic does not exist".
Definition scalar.hpp:55
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.