File: verify_magic_division.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 (113 lines) | stat: -rw-r--r-- 3,829 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// 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.h"

#include <iostream>
#include <iomanip>

template <class Float>
static bool verify_check_divisibility_and_divide_by_pow10() {
    using namespace jkj::dragonbox::detail;

    constexpr int kappa = impl<Float>::kappa;
    constexpr auto max_n = compute_power<kappa + 1>(std::uint32_t(10));
    constexpr auto divisor = compute_power<kappa>(std::uint32_t(10));

    bool success = true;
    for (std::uint32_t n = 0; n <= max_n; ++n) {
        std::uint32_t computed_quotient = n;
        auto computed_divisibility =
            div::check_divisibility_and_divide_by_pow10<kappa>(computed_quotient);

        if (computed_quotient != (n / divisor)) {
            std::cout << "Dividing n = " << n << " by " << divisor
                      << "; computed_quotient = " << computed_quotient
                      << "; true_quotient = " << (n / divisor) << std::endl;
            success = false;
        }
        if (computed_divisibility != (n % divisor == 0)) {
            std::cout << "Dividing n = " << n << " by " << divisor
                      << "; computed_divisibility = " << std::boolalpha << computed_divisibility
                      << "; true_divisibility = " << (n % divisor == 0) << std::endl;
            success = false;
        }
    }

    if (success) {
        std::cout << "All cases are verified.\n";
    }
    else {
        std::cout << "Error detected.\n";
    }

    return success;
}

template <class Float>
static bool verify_divide_by_pow10() {
    using namespace jkj::dragonbox::detail;

    constexpr int kappa = impl<Float>::kappa;
    constexpr auto max_n = compute_power<kappa + 1>(std::uint32_t(10));
    constexpr auto divisor = compute_power<kappa>(std::uint32_t(10));

    bool success = true;
    for (std::uint32_t n = 0; n <= max_n; ++n) {
        auto computed_quotient = div::small_division_by_pow10<kappa>(n);

        if (computed_quotient != (n / divisor)) {
            std::cout << "Dividing n = " << n << " by " << divisor
                      << "; computed_quotient = " << computed_quotient
                      << "; true_quotient = " << (n / divisor) << std::endl;
            success = false;
        }
    }

    if (success) {
        std::cout << "All cases are verified.\n";
    }
    else {
        std::cout << "Error detected.\n";
    }

    return success;
}

int main() {
    bool success = true;

    std::cout << "[Verifying divisibility check and division by 10^kappa for binary32...]\n";
    success &= verify_check_divisibility_and_divide_by_pow10<float>();
    std::cout << "Done.\n\n\n";

    std::cout << "[Verifying division by 10^kappa for binary32...]\n";
    success &= verify_divide_by_pow10<float>();
    std::cout << "Done.\n\n\n";

    std::cout << "[Verifying divisibility check and division by 10^kappa for binary64...]\n";
    success &= verify_check_divisibility_and_divide_by_pow10<double>();
    std::cout << "Done.\n\n\n";

    std::cout << "[Verifying division by 10^kappa for binary64...]\n";
    success &= verify_divide_by_pow10<double>();
    std::cout << "Done.\n\n\n";

    if (!success) {
        return -1;
    }
}