summaryrefslogtreecommitdiff
path: root/asl/format_float.cpp
diff options
context:
space:
mode:
authorSteven Le Rouzic <steven.lerouzic@gmail.com>2025-02-17 00:21:48 +0100
committerSteven Le Rouzic <steven.lerouzic@gmail.com>2025-02-17 22:29:50 +0100
commita141c401f78467bc15f62882fca5d55a007cacbb (patch)
tree908ac71a8640f78f45d22c6808c5fa6e373000fa /asl/format_float.cpp
parentcb77cbe9ce4cddad6a460aa190ff70f0c13e4703 (diff)
Reorganize everything
Diffstat (limited to 'asl/format_float.cpp')
-rw-r--r--asl/format_float.cpp98
1 files changed, 0 insertions, 98 deletions
diff --git a/asl/format_float.cpp b/asl/format_float.cpp
deleted file mode 100644
index 1b3186e..0000000
--- a/asl/format_float.cpp
+++ /dev/null
@@ -1,98 +0,0 @@
-#include "asl/format.hpp"
-#include "asl/float.hpp"
-
-#define JKJ_STD_REPLACEMENT_NAMESPACE_DEFINED 0
-#define JKJ_STATIC_DATA_SECTION_DEFINED 0
-#include <dragonbox.h>
-
-static constexpr isize_t kZeroCount = 100;
-static constexpr char kZeros[kZeroCount] = {
- '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
- '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
- '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
- '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
- '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
- '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
- '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
- '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
- '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
- '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
-};
-
-template<asl::is_floating_point T>
-static void format_float(asl::Formatter& f, T value)
-{
- if (asl::is_infinity(value))
- {
- if (value > 0)
- {
- f.write("Infinity"_sv);
- }
- else
- {
- f.write("-Infinity"_sv);
- }
- return;
- }
-
- if (value == static_cast<T>(0))
- {
- f.write("0"_sv);
- return;
- }
-
- if (asl::is_nan(value))
- {
- f.write("NaN"_sv);
- return;
- }
-
- auto decimal = jkj::dragonbox::to_decimal(value);
-
- if (decimal.is_negative) { f.write("-"); }
-
- char buffer[20];
- asl::string_view digits = asl::format_uint64(decimal.significand, buffer);
-
- if (decimal.exponent >= 0)
- {
- f.write(digits);
- while (decimal.exponent > 0)
- {
- isize_t to_write = asl::min(static_cast<isize_t>(decimal.exponent), kZeroCount);
- f.write(asl::string_view(kZeros, to_write));
- decimal.exponent -= to_write;
- }
- }
- else
- {
- if (digits.size() <= -decimal.exponent)
- {
- f.write("0.");
- decimal.exponent = -decimal.exponent - static_cast<int>(digits.size());
- while (decimal.exponent > 0)
- {
- isize_t to_write = asl::min(static_cast<isize_t>(decimal.exponent), kZeroCount);
- f.write(asl::string_view(kZeros, to_write));
- decimal.exponent -= to_write;
- }
- f.write(digits);
- }
- else
- {
- f.write(digits.first(digits.size() + decimal.exponent));
- f.write(".");
- f.write(digits.last(-decimal.exponent));
- }
- }
-}
-
-void asl::AslFormat(Formatter& f, float value)
-{
- format_float(f, value);
-}
-
-void asl::AslFormat(Formatter& f, double value)
-{
- format_float(f, value);
-}