LCOV - code coverage report
Current view: top level - include/as2js - float64.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 54 54 100.0 %
Date: 2014-11-22 Functions: 16 16 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : #ifndef AS2JS_FLOAT64_H
       2             : #define AS2JS_FLOAT64_H
       3             : /* float64.h -- written by Alexis WILKE for Made to Order Software Corp. (c) 2005-2014 */
       4             : 
       5             : /*
       6             : 
       7             : Copyright (c) 2005-2014 Made to Order Software Corp.
       8             : 
       9             : http://snapwebsites.org/project/as2js
      10             : 
      11             : Permission is hereby granted, free of charge, to any
      12             : person obtaining a copy of this software and
      13             : associated documentation files (the "Software"), to
      14             : deal in the Software without restriction, including
      15             : without limitation the rights to use, copy, modify,
      16             : merge, publish, distribute, sublicense, and/or sell
      17             : copies of the Software, and to permit persons to whom
      18             : the Software is furnished to do so, subject to the
      19             : following conditions:
      20             : 
      21             : The above copyright notice and this permission notice
      22             : shall be included in all copies or substantial
      23             : portions of the Software.
      24             : 
      25             : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
      26             : ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
      27             : LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
      28             : FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
      29             : EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
      30             : LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
      31             : WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
      32             : ARISING FROM, OUT OF OR IN CONNECTION WITH THE
      33             : SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
      34             : SOFTWARE.
      35             : 
      36             : */
      37             : 
      38             : #include    "as2js/compare.h"
      39             : 
      40             : #pragma GCC diagnostic push
      41             : #pragma GCC diagnostic ignored "-Wfloat-equal"
      42             : #include    <controlled_vars/controlled_vars_fauto_init.h>
      43             : #pragma GCC diagnostic pop
      44             : 
      45             : #include    <limits>
      46             : #include    <cmath>
      47             : 
      48             : 
      49             : namespace as2js
      50             : {
      51             : 
      52             : class Float64
      53             : {
      54             : public:
      55             :     typedef double  float64_type;
      56             : 
      57    73264804 :                     Float64()
      58             :                         // : f_float(0.0) auto-init
      59    73264804 :                     {
      60    73264804 :                     }
      61             : 
      62      445516 :                     Float64(float64_type const rhs)
      63      445516 :                     {
      64      445516 :                         f_float = rhs;
      65      445516 :                     }
      66             : 
      67     1288113 :                     Float64(Float64 const& rhs)
      68     1288113 :                     {
      69     1288113 :                         f_float = rhs.f_float;
      70     1288113 :                     }
      71             : 
      72      534943 :     Float64&        operator = (Float64 const& rhs)
      73             :                     {
      74      534943 :                         f_float = rhs.f_float;
      75      534943 :                         return *this;
      76             :                     }
      77             : 
      78      939258 :     float64_type    get() const
      79             :                     {
      80      939258 :                         return f_float;
      81             :                     }
      82             : 
      83       51507 :     void            set(float64_type const new_float)
      84             :                     {
      85       51507 :                         f_float = new_float;
      86       51507 :                     }
      87             : 
      88        8409 :     void            set_NaN()
      89             :                     {
      90        8409 :                         f_float = std::numeric_limits<Float64::float64_type>::quiet_NaN();
      91        8409 :                     }
      92             : 
      93        8474 :     void            set_infinity()
      94             :                     {
      95        8474 :                         f_float = std::numeric_limits<Float64::float64_type>::infinity();
      96        8474 :                     }
      97             : 
      98      249932 :     bool            is_NaN() const
      99             :                     {
     100      249932 :                         return std::isnan(f_float);
     101             :                     }
     102             : 
     103        9098 :     bool            is_infinity() const
     104             :                     {
     105        9098 :                         return std::isinf(f_float);
     106             :                     }
     107             : 
     108       10837 :     bool            is_positive_infinity() const
     109             :                     {
     110       10837 :                         return std::isinf(f_float) && !std::signbit(f_float);
     111             :                     }
     112             : 
     113       10648 :     bool            is_negative_infinity() const
     114             :                     {
     115       10648 :                         return std::isinf(f_float) && std::signbit(f_float);
     116             :                     }
     117             : 
     118        8003 :     int             classified_infinity() const
     119             :                     {
     120             :                         // if infinity, return -1 or +1
     121             :                         // if not infinity, return 0
     122        8003 :                         return std::isinf(f_float)
     123           2 :                                 ? (std::signbit(f_float) ? -1 : 1)
     124        8005 :                                 : 0;
     125             :                     }
     126             : 
     127       48190 :     compare_t       compare(Float64 const& rhs) const
     128             :                     {
     129             :                         // if we got a NaN, it's not ordered
     130       48190 :                         if(is_NaN() || rhs.is_NaN())
     131             :                         {
     132          22 :                             return compare_t::COMPARE_UNORDERED;
     133             :                         }
     134             : 
     135             :                         // comparing two floats properly handles infinity
     136             :                         // (at least in g++ on Intel processors)
     137       48168 :                         return f_float == rhs.f_float ? compare_t::COMPARE_EQUAL
     138       48120 :                              : (f_float < rhs.f_float ? compare_t::COMPARE_LESS
     139       96288 :                                                       : compare_t::COMPARE_GREATER);
     140             :                     }
     141             : 
     142          57 :     static float64_type default_epsilon()
     143             :                     {
     144          57 :                         return 0.00001;
     145             :                     }
     146             : 
     147      122165 :     bool            nearly_equal(Float64 const& rhs, float64_type epsilon = default_epsilon())
     148             :                     {
     149             : #pragma GCC diagnostic push
     150             : #pragma GCC diagnostic ignored "-Wfloat-equal"
     151             :                         // already equal?
     152      122165 :                         if(f_float == rhs.f_float)
     153             :                         {
     154      100121 :                             return true;
     155             :                         }
     156             : 
     157       22044 :                         float64_type const diff = fabs(f_float - rhs.f_float);
     158       44088 :                         if(f_float == 0.0
     159       22040 :                         || rhs.f_float == 0.0
     160       44080 :                         || diff < std::numeric_limits<double>::min())
     161             :                         {
     162           8 :                             return diff < (epsilon * std::numeric_limits<double>::min());
     163             :                         }
     164             : 
     165       22036 :                         return diff / (fabs(f_float) + fabs(rhs.f_float)) < epsilon;
     166             : #pragma GCC diagnostic pop
     167             :                     }
     168             :  
     169             : 
     170             : private:
     171             :     controlled_vars::fauto_init<float64_type>    f_float;
     172             : };
     173             : 
     174             : 
     175             : }
     176             : // namespace as2js
     177             : #endif
     178             : // #ifndef AS2JS_FLOAT64_H
     179             : 
     180             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.10