| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2000-2022 Inria | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions are met: | ||
| 7 | * | ||
| 8 | * * Redistributions of source code must retain the above copyright notice, | ||
| 9 | * this list of conditions and the following disclaimer. | ||
| 10 | * * Redistributions in binary form must reproduce the above copyright notice, | ||
| 11 | * this list of conditions and the following disclaimer in the documentation | ||
| 12 | * and/or other materials provided with the distribution. | ||
| 13 | * * Neither the name of the ALICE Project-Team nor the names of its | ||
| 14 | * contributors may be used to endorse or promote products derived from this | ||
| 15 | * software without specific prior written permission. | ||
| 16 | * | ||
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
| 21 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 26 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 27 | * POSSIBILITY OF SUCH DAMAGE. | ||
| 28 | * | ||
| 29 | * Contact: Bruno Levy | ||
| 30 | * | ||
| 31 | * https://www.inria.fr/fr/bruno-levy | ||
| 32 | * | ||
| 33 | * Inria, | ||
| 34 | * Domaine de Voluceau, | ||
| 35 | * 78150 Le Chesnay - Rocquencourt | ||
| 36 | * FRANCE | ||
| 37 | * | ||
| 38 | */ | ||
| 39 | |||
| 40 | #include <geogram/numerics/expansion_nt.h> | ||
| 41 | #include <iostream> | ||
| 42 | |||
| 43 | /** | ||
| 44 | * \brief Outputs an expansion_nt to a stream. | ||
| 45 | * \param out a reference to the output stream | ||
| 46 | * \param x a const reference to the expansion_nt to be printed | ||
| 47 | */ | ||
| 48 | ✗ | static void print(std::ostream& out, const GEO::expansion_nt& x) { | |
| 49 | out << "expansion_nt(estimate=" | ||
| 50 | << x.estimate(); | ||
| 51 | ✗ | out << ", components=["; | |
| 52 | ✗ | for(GEO::index_t i=0; i<x.length(); ++i) { | |
| 53 | out << x.component(i); | ||
| 54 | ✗ | if(i != x.length()-1) { | |
| 55 | ✗ | out << " "; | |
| 56 | } | ||
| 57 | } | ||
| 58 | ✗ | out << "]"; | |
| 59 | ✗ | out << ")"; | |
| 60 | ✗ | } | |
| 61 | |||
| 62 | /** | ||
| 63 | * \brief Outputs a rational_nt to a stream. | ||
| 64 | * \param out a reference to the output stream | ||
| 65 | * \param x a const reference to the expansion_nt to be printed | ||
| 66 | */ | ||
| 67 | ✗ | static void print(std::ostream& out, const GEO::rational_nt& x) { | |
| 68 | ✗ | out << "estimate=" << x.estimate() << ":"; | |
| 69 | ✗ | print(out,x.num()); | |
| 70 | ✗ | out << " / "; | |
| 71 | ✗ | print(out,x.denom()); | |
| 72 | ✗ | } | |
| 73 | |||
| 74 | /** | ||
| 75 | * \brief Outputs a double precision number to a stream. | ||
| 76 | * \details this function is here so that the same generic code | ||
| 77 | * can be used with double and with expansion to show the difference. | ||
| 78 | * \param out a reference to the output stream | ||
| 79 | * \param x the double-precision number to be printed | ||
| 80 | */ | ||
| 81 | static void print(std::ostream& out, double x) { | ||
| 82 | out << x; | ||
| 83 | } | ||
| 84 | |||
| 85 | /** | ||
| 86 | * \brief performs a simple computation designed to | ||
| 87 | * give an errouenous result when using doubles. | ||
| 88 | * \param zzz an ignored parameter, just there to | ||
| 89 | * specify the type to be used for computations, i.e. | ||
| 90 | * use a double to test with doubles, and an expansion_nt | ||
| 91 | * to test with expansion_nt. | ||
| 92 | */ | ||
| 93 | ✗ | template <class T> inline void compute(const T& zzz) { | |
| 94 | GEO::geo_argused(zzz); | ||
| 95 | |||
| 96 | ✗ | T r = T(1e-30)+T(5.0)+T(1e30)+T(2e-30)-T(1e30); | |
| 97 | std::cout << " sign(1e-30 + 5.0 + 1e30 + 2e-30 - 1e30) = " | ||
| 98 | ✗ | << GEO::geo_sgn(r) << std::endl; | |
| 99 | ✗ | std::cout << " result = "; | |
| 100 | ✗ | print(std::cout,r); | |
| 101 | std::cout << std::endl; | ||
| 102 | ✗ | } | |
| 103 | |||
| 104 | |||
| 105 | ✗ | template <class T> inline void compute2(const T& zzz) { | |
| 106 | GEO::geo_argused(zzz); | ||
| 107 | |||
| 108 | ✗ | T r = T(1e-30) / T(3.0) + T(5.0) / T(3.0) + T(1e30) / T(3.0) + T(2e-30) / T(3.0) - T(1e30) / T(3.0); | |
| 109 | std::cout << " sign(1e-30/3.0 + 5.0/3.0 + 1e30/3.0 + 2e-30/3.0 - 1e30/3.0) = " | ||
| 110 | ✗ | << GEO::geo_sgn(r) << std::endl; | |
| 111 | ✗ | std::cout << " result = "; | |
| 112 | ✗ | print(std::cout,r); | |
| 113 | std::cout << std::endl; | ||
| 114 | ✗ | } | |
| 115 | |||
| 116 | |||
| 117 | ✗ | int main() { | |
| 118 | // This function needs to be called before | ||
| 119 | // using expansion_nt. | ||
| 120 | ✗ | GEO::expansion::initialize(); | |
| 121 | |||
| 122 | std::cout << "Using double:" << std::endl; | ||
| 123 | ✗ | compute(double()); | |
| 124 | |||
| 125 | std::cout << "Using expansion_nt:" << std::endl; | ||
| 126 | ✗ | compute(GEO::expansion_nt()); | |
| 127 | |||
| 128 | std::cout << "Using rational_nt:" << std::endl; | ||
| 129 | ✗ | compute(GEO::rational_nt()); | |
| 130 | |||
| 131 | std::cout << "With divisions:" << std::endl; | ||
| 132 | |||
| 133 | std::cout << "Using double:" << std::endl; | ||
| 134 | ✗ | compute2(double()); | |
| 135 | |||
| 136 | std::cout << "Using rational_nt:" << std::endl; | ||
| 137 | ✗ | compute2(GEO::rational_nt()); | |
| 138 | |||
| 139 | return 0; | ||
| 140 | } | ||
| 141 |