File: live_test.cpp

package info (click to toggle)
dragonbox 1.1.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,148 kB
  • sloc: cpp: 8,752; ansic: 1,522; makefile: 18
file content (81 lines) | stat: -rw-r--r-- 2,634 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright 2020 Junekey Jeon
//
// The contents of this file may be used under the terms of
// the Apache License v2.0 with LLVM Exceptions.
//
//    (See accompanying file LICENSE-Apache or copy at
//     https://llvm.org/foundation/relicensing/LICENSE.txt)
//
// Alternatively, the contents of this file may be used under the terms of
// the Boost Software License, Version 1.0.
//    (See accompanying file LICENSE-Boost or copy at
//     https://www.boost.org/LICENSE_1_0.txt)
//
// Unless required by applicable law or agreed to in writing, this software
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.

#include "dragonbox/dragonbox_to_chars.h"

#include <iostream>
#include <iomanip>
#include <string>

template <class Float>
static void live_test() {
    char buffer[41];

    while (true) {
        Float x;
        std::string x_str;
        while (true) {
            std::getline(std::cin, x_str);
            try {
                if constexpr (std::is_same_v<Float, float>) {
                    x = std::stof(x_str);
                }
                else {
                    x = std::stod(x_str);
                }
            }
            catch (...) {
                std::cout << "Not a valid input; input again.\n";
                continue;
            }
            break;
        }

        auto xx = jkj::dragonbox::float_bits<Float>{x};
        std::cout << "              sign: " << (xx.is_negative() ? "-" : "+") << std::endl;
        std::cout << "     exponent bits: "
                  << "0x" << std::hex << std::setfill('0') << xx.extract_exponent_bits() << std::dec
                  << " (value: " << xx.binary_exponent() << ")\n";
        std::cout << "  significand bits: "
                  << "0x" << std::hex << std::setfill('0');
        if constexpr (std::is_same_v<Float, float>) {
            std::cout << std::setw(8);
        }
        else {
            std::cout << std::setw(16);
        }
        std::cout << xx.extract_significand_bits() << " (value: 0x" << xx.binary_significand()
                  << ")\n"
                  << std::dec;

        jkj::dragonbox::to_chars(x, buffer);
        std::cout << "  Dragonbox output: " << buffer << "\n\n";
    }
}

int main() {
    constexpr enum { test_float, test_double } test = test_double;

    if constexpr (test == test_float) {
        std::cout << "[Start live test for float's]\n";
        live_test<float>();
    }
    else if constexpr (test == test_double) {
        std::cout << "[Start live test for double's]\n";
        live_test<double>();
    }
}