1#include <guanaqo/export.h>
7#if __has_include(<stdfloat>)
16std::string_view float_to_str_vw_snprintf(
auto &&print,
auto &buf,
17 std::floating_point
auto value,
18 int precision,
const char *fmt) {
19 int n = print(buf.data(), buf.size(), fmt, precision, value);
20 assert((
size_t)n < buf.size());
21 return {buf.data(), (size_t)n};
24template <std::
floating_po
int F>
25#ifdef GUANAQO_WITH_QUAD_PRECISION
26 requires(!std::same_as<F, __float128>)
28std::string_view float_to_str_vw_impl(std::span<char> buf, F value,
30 auto begin = buf.data(), buf_end = begin + buf.size();
31 if (!std::signbit(value) && !std::isnan(value))
33 auto fmt = std::chars_format::scientific;
36 constexpr bool print_chars_available =
37 requires { std::to_chars(begin, buf_end, value, fmt, precision); };
38 if constexpr (print_chars_available) {
39 auto [end, _] = std::to_chars(begin, buf_end, value, fmt, precision);
40 return std::string_view{buf.data(), end};
42 auto value_ld =
static_cast<long double>(value);
43 auto [end, _] = std::to_chars(begin, buf_end, value_ld, fmt, precision);
44 return std::string_view{buf.data(), end};
48#pragma message("Using std::snprintf as a fallback to replace std::to_chars")
50std::string_view float_to_str_vw_impl(std::span<char> buf,
double value,
52 return float_to_str_vw_snprintf(std::snprintf, buf, value, precision,
55std::string_view float_to_str_vw_impl(std::span<char> buf,
float value,
57 return float_to_str_vw_impl(buf,
static_cast<double>(value), precision);
59std::string_view float_to_str_vw_impl(std::span<char> buf,
long double value,
61 return float_to_str_vw_snprintf(std::snprintf, buf, value, precision,
66#ifdef GUANAQO_WITH_QUAD_PRECISION
67std::string_view float_to_str_vw_impl(std::span<char> buf, __float128 value,
69 return float_to_str_vw_snprintf(quadmath_snprintf, buf, value, precision,
76template <std::
floating_po
int F>
78 return float_to_str_vw_impl(buf, value, precision);
84#define GUANAQO_PRINT_INSTANTIATE_F(type) \
85 template GUANAQO_EXPORT std::string_view float_to_str_vw(std::span<char>, type value, int precision); \
86 template GUANAQO_EXPORT std::string float_to_str(type value, int precision); \
87 template GUANAQO_EXPORT std::ostream &detail::print_csv_impl(std::ostream &os, PrintMatrixView<const type>, PrintOpts); \
88 template GUANAQO_EXPORT std::ostream &detail::print_matlab_impl(std::ostream &os, PrintMatrixView<const type>, std::string_view); \
89 template GUANAQO_EXPORT std::ostream &detail::print_python_impl(std::ostream &os, PrintMatrixView<const type>, std::string_view, bool); \
90 template GUANAQO_EXPORT std::ostream &detail::print_csv_impl(std::ostream &os, PrintMatrixView<const std::complex<type>>, PrintOpts); \
91 template GUANAQO_EXPORT std::ostream &detail::print_matlab_impl(std::ostream &os, PrintMatrixView<const std::complex<type>>, std::string_view); \
92 template GUANAQO_EXPORT std::ostream &detail::print_python_impl(std::ostream &os, PrintMatrixView<const std::complex<type>>, std::string_view, bool)
96GUANAQO_PRINT_INSTANTIATE_F(
float);
97GUANAQO_PRINT_INSTANTIATE_F(
double);
98GUANAQO_PRINT_INSTANTIATE_F(
long double);
99#if defined(__STDCPP_FLOAT16_T__) && __STDCPP_FLOAT16_T__ == 1
100GUANAQO_PRINT_INSTANTIATE_F(std::float16_t);
102#if defined(__STDCPP_FLOAT32_T__) && __STDCPP_FLOAT32_T__ == 1
103GUANAQO_PRINT_INSTANTIATE_F(std::float32_t);
105#if defined(__STDCPP_FLOAT64_T__) && __STDCPP_FLOAT64_T__ == 1
106GUANAQO_PRINT_INSTANTIATE_F(std::float64_t);
108#if defined(__STDCPP_FLOAT128_T__) && __STDCPP_FLOAT128_T__ == 1
109GUANAQO_PRINT_INSTANTIATE_F(std::float128_t);
111#if defined(__STDCPP_BFLOAT16_T__) && __STDCPP_BFLOAT16_T__ == 1
112GUANAQO_PRINT_INSTANTIATE_F(std::bfloat16_t);
114#ifdef GUANAQO_WITH_QUAD_PRECISION
115GUANAQO_PRINT_INSTANTIATE_F(__float128);
119#define GUANAQO_PRINT_INSTANTIATE_I(type) \
120 template GUANAQO_EXPORT std::ostream &detail::print_csv_impl(std::ostream &os, PrintMatrixView<const type>, PrintOpts); \
121 template GUANAQO_EXPORT std::ostream &detail::print_matlab_impl(std::ostream &os, PrintMatrixView<const type>, std::string_view); \
122 template GUANAQO_EXPORT std::ostream &detail::print_python_impl(std::ostream &os, PrintMatrixView<const type>, std::string_view, bool)
125GUANAQO_PRINT_INSTANTIATE_I(
char);
126GUANAQO_PRINT_INSTANTIATE_I(
signed char);
127GUANAQO_PRINT_INSTANTIATE_I(
short);
128GUANAQO_PRINT_INSTANTIATE_I(
int);
129GUANAQO_PRINT_INSTANTIATE_I(
long);
130GUANAQO_PRINT_INSTANTIATE_I(
long long);
131GUANAQO_PRINT_INSTANTIATE_I(
unsigned char);
132GUANAQO_PRINT_INSTANTIATE_I(
unsigned short);
133GUANAQO_PRINT_INSTANTIATE_I(
unsigned int);
134GUANAQO_PRINT_INSTANTIATE_I(
unsigned long);
135GUANAQO_PRINT_INSTANTIATE_I(
unsigned long long);
std::string_view float_to_str_vw(std::span< char > buf, F value, int precision=std::numeric_limits< F >::max_digits10)