github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/test/common/gtest_math.cpp (about)

     1  // Copyright (C) 2018 go-nebulas authors
     2  //
     3  // This file is part of the go-nebulas library.
     4  //
     5  // the go-nebulas library is free software: you can redistribute it and/or
     6  // modify
     7  // it under the terms of the GNU General Public License as published by
     8  // the Free Software Foundation, either version 3 of the License, or
     9  // (at your option) any later version.
    10  //
    11  // the go-nebulas library is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU General Public License
    17  // along with the go-nebulas library.  If not, see
    18  // <http://www.gnu.org/licenses/>.
    19  //
    20  
    21  #include "common/common.h"
    22  #include "common/math.h"
    23  #include <gtest/gtest.h>
    24  #include <random>
    25  #define PRECESION 1e-5
    26  
    27  template <typename T> T precesion(const T &x, float pre = PRECESION) {
    28    return std::fabs(T(x * pre));
    29  }
    30  
    31  TEST(test_common_math, constants) {
    32    neb::floatxx_t actual_e = neb::math::constants<neb::floatxx_t>::e();
    33    neb::floatxx_t actual_pi = neb::math::constants<neb::floatxx_t>::pi();
    34    neb::floatxx_t actual_ln2 = neb::math::constants<neb::floatxx_t>::ln2();
    35  
    36    float expect_e = std::exp(1.0);
    37    float expect_pi = std::acos(-1.0);
    38    float expect_ln2 = std::log(2.0);
    39  
    40    EXPECT_TRUE(neb::math::abs(actual_e, neb::floatxx_t(expect_e)) <
    41                precesion(expect_e));
    42    EXPECT_TRUE(neb::math::abs(actual_pi, neb::floatxx_t(expect_pi)) <
    43                precesion(expect_pi));
    44    EXPECT_TRUE(neb::math::abs(actual_ln2, neb::floatxx_t(expect_ln2)) <
    45                precesion(expect_ln2));
    46  }
    47  
    48  TEST(test_common_math, min) {
    49    neb::floatxx_t x(0);
    50    neb::floatxx_t y(0);
    51    EXPECT_TRUE(x == neb::math::min(x, y));
    52    EXPECT_TRUE(y == neb::math::min(x, y));
    53  
    54    x = 1;
    55    EXPECT_TRUE(y == neb::math::min(x, y));
    56  
    57    y = 2;
    58    EXPECT_TRUE(x == neb::math::min(x, y));
    59  
    60    y = -1;
    61    EXPECT_TRUE(y == neb::math::min(x, y));
    62  
    63    x = -2;
    64    EXPECT_TRUE(x == neb::math::min(x, y));
    65  
    66    x = std::numeric_limits<int64_t>::min();
    67    EXPECT_TRUE(x == neb::math::min(x, y));
    68  
    69    x = std::numeric_limits<int64_t>::max();
    70    EXPECT_TRUE(y == neb::math::min(x, y));
    71  
    72    y = std::numeric_limits<int32_t>::max();
    73    EXPECT_TRUE(y == neb::math::min(x, y));
    74  
    75    std::random_device rd;
    76    std::mt19937 mt(rd());
    77    std::uniform_int_distribution<> dis(std::numeric_limits<int32_t>::min(),
    78                                        std::numeric_limits<int32_t>::max());
    79    for (auto i = 0; i < 1000; i++) {
    80      x = dis(mt);
    81      y = dis(mt);
    82      if (x < y) {
    83        EXPECT_TRUE(x == neb::math::min(x, y));
    84      } else if (x > y) {
    85        EXPECT_TRUE(y == neb::math::min(x, y));
    86      } else {
    87        EXPECT_TRUE(x == neb::math::min(x, y));
    88        EXPECT_TRUE(y == neb::math::min(x, y));
    89      }
    90    }
    91  }
    92  
    93  TEST(test_common_math, max) {
    94    neb::floatxx_t x(0);
    95    neb::floatxx_t y(0);
    96    EXPECT_TRUE(x == neb::math::max(x, y));
    97    EXPECT_TRUE(y == neb::math::max(x, y));
    98  
    99    x = 1;
   100    EXPECT_TRUE(x == neb::math::max(x, y));
   101  
   102    y = 2;
   103    EXPECT_TRUE(y == neb::math::max(x, y));
   104  
   105    y = -1;
   106    EXPECT_TRUE(x == neb::math::max(x, y));
   107  
   108    x = -2;
   109    EXPECT_TRUE(y == neb::math::max(x, y));
   110  
   111    x = std::numeric_limits<int64_t>::min();
   112    EXPECT_TRUE(y == neb::math::max(x, y));
   113  
   114    x = std::numeric_limits<int64_t>::max();
   115    EXPECT_TRUE(x == neb::math::max(x, y));
   116  
   117    y = std::numeric_limits<int32_t>::max();
   118    EXPECT_TRUE(x == neb::math::max(x, y));
   119  
   120    std::random_device rd;
   121    std::mt19937 mt(rd());
   122    std::uniform_int_distribution<> dis(std::numeric_limits<int32_t>::min(),
   123                                        std::numeric_limits<int32_t>::max());
   124    for (auto i = 0; i < 1000; i++) {
   125      x = dis(mt);
   126      y = dis(mt);
   127      if (x < y) {
   128        EXPECT_TRUE(y == neb::math::max(x, y));
   129      } else if (x > y) {
   130        EXPECT_TRUE(x == neb::math::max(x, y));
   131      } else {
   132        EXPECT_TRUE(x == neb::math::max(x, y));
   133        EXPECT_TRUE(y == neb::math::max(x, y));
   134      }
   135    }
   136  }
   137  
   138  TEST(test_common_math, abs) {
   139    neb::floatxx_t x(0);
   140    neb::floatxx_t y(0);
   141    EXPECT_TRUE((x - y) == neb::math::abs(x, y));
   142    EXPECT_TRUE((y - x) == neb::math::abs(x, y));
   143  
   144    x = 1;
   145    EXPECT_TRUE((x - y) == neb::math::abs(x, y));
   146  
   147    y = 2;
   148    EXPECT_TRUE((y - x) == neb::math::abs(x, y));
   149  
   150    y = -1;
   151    EXPECT_TRUE((x - y) == neb::math::abs(x, y));
   152  
   153    x = -2;
   154    EXPECT_TRUE((y - x) == neb::math::abs(x, y));
   155  
   156    x = std::numeric_limits<int64_t>::min();
   157    EXPECT_TRUE((y - x) == neb::math::abs(x, y));
   158  
   159    x = std::numeric_limits<int64_t>::max();
   160    EXPECT_TRUE((x - y) == neb::math::abs(x, y));
   161  
   162    y = std::numeric_limits<int32_t>::max();
   163    EXPECT_TRUE((x - y) == neb::math::abs(x, y));
   164  
   165    std::random_device rd;
   166    std::mt19937 mt(rd());
   167    std::uniform_int_distribution<> dis(std::numeric_limits<int32_t>::min(),
   168                                        std::numeric_limits<int32_t>::max());
   169    for (auto i = 0; i < 1000; i++) {
   170      x = dis(mt);
   171      y = dis(mt);
   172      if (x < y) {
   173        EXPECT_TRUE((y - x) == neb::math::abs(x, y));
   174      } else if (x > y) {
   175        EXPECT_TRUE((x - y) == neb::math::abs(x, y));
   176      } else {
   177        EXPECT_TRUE((x - y) == neb::math::abs(x, y));
   178        EXPECT_TRUE((y - x) == neb::math::abs(x, y));
   179      }
   180    }
   181  }
   182  
   183  TEST(test_common_math, to_string) {
   184    float x = 0;
   185    neb::floatxx_t y = x;
   186    std::ostringstream oss;
   187    oss << x;
   188    EXPECT_EQ(neb::math::to_string(y), oss.str());
   189    EXPECT_EQ(neb::math::to_string(x), std::string("0"));
   190  
   191    x = 1;
   192    y = x;
   193    oss.str(std::string());
   194    oss << x;
   195    EXPECT_EQ(neb::math::to_string(y), oss.str());
   196    EXPECT_EQ(neb::math::to_string(x), std::string("1"));
   197  
   198    x = -1;
   199    y = x;
   200    oss.str(std::string());
   201    oss << x;
   202    EXPECT_EQ(neb::math::to_string(y), oss.str());
   203    EXPECT_EQ(neb::math::to_string(x), std::string("-1"));
   204  
   205    x = 123456;
   206    y = x;
   207    oss.str(std::string());
   208    oss << x;
   209    EXPECT_EQ(neb::math::to_string(y), oss.str());
   210    EXPECT_EQ(neb::math::to_string(x), std::string("123456"));
   211  
   212    x = 1234567;
   213    y = x;
   214    oss.str(std::string());
   215    oss << x;
   216    EXPECT_EQ(neb::math::to_string(y), oss.str());
   217    EXPECT_EQ(neb::math::to_string(x), std::string("1.23457e+06"));
   218  
   219    x = -456789;
   220    y = x;
   221    oss.str(std::string());
   222    oss << x;
   223    EXPECT_EQ(neb::math::to_string(y), oss.str());
   224    EXPECT_EQ(neb::math::to_string(x), std::string("-456789"));
   225  
   226    x = -4567890;
   227    y = x;
   228    oss.str(std::string());
   229    oss << x;
   230    EXPECT_EQ(neb::math::to_string(y), oss.str());
   231    EXPECT_EQ(neb::math::to_string(x), std::string("-4.56789e+06"));
   232  
   233    std::random_device rd;
   234    std::mt19937 mt(rd());
   235    std::uniform_int_distribution<> dis(std::numeric_limits<int32_t>::min(),
   236                                        std::numeric_limits<int32_t>::max());
   237    for (auto i = 0; i < 1000; i++) {
   238      x = dis(mt);
   239      y = x;
   240      oss.str(std::string());
   241      oss << x;
   242      EXPECT_EQ(neb::math::to_string(y), oss.str());
   243    }
   244  }
   245  
   246  TEST(test_common_math, from_string) {
   247    float x = 0;
   248    std::ostringstream oss;
   249    oss << x;
   250    std::string x_str = oss.str();
   251    neb::floatxx_t y = neb::math::from_string<neb::floatxx_t>(x_str);
   252    EXPECT_EQ(std::stof(x_str), float(y));
   253  
   254    x = 1;
   255    oss.str(std::string());
   256    oss << x;
   257    x_str = oss.str();
   258    y = neb::math::from_string<neb::floatxx_t>(x_str);
   259    EXPECT_EQ(std::stof(x_str), float(y));
   260  
   261    x = -1;
   262    oss.str(std::string());
   263    oss << x;
   264    x_str = oss.str();
   265    y = neb::math::from_string<neb::floatxx_t>(x_str);
   266    EXPECT_EQ(std::stof(x_str), float(y));
   267  
   268    x = 123456;
   269    oss.str(std::string());
   270    oss << x;
   271    x_str = oss.str();
   272    y = neb::math::from_string<neb::floatxx_t>(x_str);
   273    EXPECT_EQ(std::stof(x_str), float(y));
   274  
   275    x = 1234567;
   276    oss.str(std::string());
   277    oss << x;
   278    x_str = oss.str();
   279    y = neb::math::from_string<neb::floatxx_t>(x_str);
   280    EXPECT_EQ(std::stof(x_str), float(y));
   281  
   282    x = -456789;
   283    oss.str(std::string());
   284    oss << x;
   285    x_str = oss.str();
   286    y = neb::math::from_string<neb::floatxx_t>(x_str);
   287    EXPECT_EQ(std::stof(x_str), float(y));
   288  
   289    x = -4567890;
   290    oss.str(std::string());
   291    oss << x;
   292    x_str = oss.str();
   293    y = neb::math::from_string<neb::floatxx_t>(x_str);
   294    EXPECT_EQ(std::stof(x_str), float(y));
   295  
   296    std::random_device rd;
   297    std::mt19937 mt(rd());
   298    std::uniform_int_distribution<> dis(std::numeric_limits<int32_t>::min(),
   299                                        std::numeric_limits<int32_t>::max());
   300    for (auto i = 0; i < 1000; i++) {
   301      x = dis(mt);
   302      oss.str(std::string());
   303      oss << x;
   304      x_str = oss.str();
   305      y = neb::math::from_string<neb::floatxx_t>(x_str);
   306      EXPECT_EQ(std::stof(x_str), float(y));
   307    }
   308  }
   309  
   310  TEST(test_common_math, exp) {
   311    EXPECT_EQ(neb::math::exp(neb::floatxx_t(0)), 1);
   312  
   313    neb::floatxx_t actual_x = neb::math::exp(neb::floatxx_t(1));
   314    float expect_x = std::exp(1.0);
   315    EXPECT_TRUE(neb::math::abs(actual_x, neb::floatxx_t(expect_x)) <
   316                precesion(expect_x));
   317  
   318    actual_x = neb::math::exp(neb::floatxx_t(-1));
   319    expect_x = std::exp(-1.0);
   320    EXPECT_TRUE(neb::math::abs(actual_x, neb::floatxx_t(expect_x)) <
   321                precesion(expect_x));
   322  
   323    actual_x = neb::math::exp(neb::floatxx_t(80));
   324    expect_x = std::exp(80.0);
   325    EXPECT_TRUE(neb::math::abs(actual_x, neb::floatxx_t(expect_x)) <
   326                precesion(expect_x));
   327  
   328    float delta(0.01);
   329    for (float x = -50.0; x <= 50.0; x += delta) {
   330      auto actual_x = neb::math::exp(neb::floatxx_t(x));
   331      auto expect_x = std::exp(x);
   332      EXPECT_TRUE(neb::math::abs(actual_x, neb::floatxx_t(expect_x)) <
   333                  precesion(expect_x));
   334    }
   335  }
   336  
   337  TEST(test_common_math, arctan) {
   338    EXPECT_EQ(neb::math::arctan(neb::floatxx_t(0)), std::atan(0));
   339  
   340    neb::floatxx_t actual_x = neb::math::arctan(neb::floatxx_t(1));
   341    float expect_x = std::atan(1.0);
   342    EXPECT_TRUE(neb::math::abs(actual_x, neb::floatxx_t(expect_x)) <
   343                precesion(expect_x));
   344  
   345    actual_x = neb::math::arctan(neb::floatxx_t(-1));
   346    expect_x = std::atan(-1.0);
   347    EXPECT_TRUE(neb::math::abs(actual_x, neb::floatxx_t(expect_x)) <
   348                precesion(expect_x));
   349  
   350    float delta(0.01);
   351    for (float x = -100.0; x <= 100.0; x += delta) {
   352      auto actual_x = neb::math::arctan(neb::floatxx_t(x));
   353      auto expect_x = std::atan(x);
   354      if (std::fabs(x) < 0.1) {
   355        ASSERT_TRUE(neb::math::abs(actual_x, neb::floatxx_t(expect_x)) <
   356                    precesion(expect_x, 1e2 * PRECESION));
   357      } else if (std::fabs(x) < 1.0) {
   358        EXPECT_TRUE(neb::math::abs(actual_x, neb::floatxx_t(expect_x)) <
   359                    precesion(expect_x, 1e1 * PRECESION));
   360      } else {
   361        EXPECT_TRUE(neb::math::abs(actual_x, neb::floatxx_t(expect_x)) <
   362                    precesion(expect_x, 1e1 * PRECESION));
   363      }
   364    }
   365  }
   366  
   367  TEST(test_common_math, sin) {
   368    EXPECT_EQ(neb::math::sin(neb::floatxx_t(0)), std::sin(0));
   369  
   370    neb::floatxx_t actual_x = neb::math::sin(neb::floatxx_t(1));
   371    float expect_x = std::sin(1.0);
   372    EXPECT_TRUE(neb::math::abs(actual_x, neb::floatxx_t(expect_x)) <
   373                precesion(expect_x));
   374  
   375    actual_x = neb::math::sin(neb::floatxx_t(-1));
   376    expect_x = std::sin(-1.0);
   377    EXPECT_TRUE(neb::math::abs(actual_x, neb::floatxx_t(expect_x)) <
   378                precesion(expect_x));
   379  
   380    float pi = std::acos(-1.0);
   381    actual_x = neb::math::sin(neb::floatxx_t(pi / 2.0));
   382    expect_x = std::sin(pi / 2.0);
   383    EXPECT_TRUE(neb::math::abs(actual_x, neb::floatxx_t(expect_x)) <
   384                precesion(expect_x));
   385  
   386    actual_x = neb::math::sin(neb::floatxx_t(pi / 3.0));
   387    expect_x = std::sin(pi / 3.0);
   388    EXPECT_TRUE(neb::math::abs(actual_x, neb::floatxx_t(expect_x)) <
   389                precesion(expect_x));
   390  
   391    actual_x = neb::math::sin(neb::floatxx_t(-pi / 4.0));
   392    expect_x = std::sin(-pi / 4.0);
   393    EXPECT_TRUE(neb::math::abs(actual_x, neb::floatxx_t(expect_x)) <
   394                precesion(expect_x));
   395  
   396    actual_x = neb::math::sin(neb::floatxx_t(-pi / 6.0));
   397    expect_x = std::sin(-pi / 6.0);
   398    EXPECT_TRUE(neb::math::abs(actual_x, neb::floatxx_t(expect_x)) <
   399                precesion(expect_x));
   400  
   401    for (int32_t t = -100; t <= 100; t++) {
   402      auto actual_x = neb::math::sin(neb::floatxx_t(pi * t));
   403      auto expect_x = std::sin(pi * t);
   404      EXPECT_TRUE(neb::math::abs(actual_x, neb::floatxx_t(expect_x)) <
   405                  precesion(1.0f, 1e2 * PRECESION));
   406    }
   407  
   408    float delta(0.01);
   409    for (float x = -100.0; x <= 100.0; x += delta) {
   410      auto actual_x = neb::math::sin(neb::floatxx_t(x));
   411      auto expect_x = std::sin(x);
   412      EXPECT_TRUE(neb::math::abs(actual_x, neb::floatxx_t(expect_x)) <
   413                  precesion(1.0f, 1e2 * PRECESION));
   414    }
   415  }
   416  
   417  TEST(test_common_math, ln) {
   418    EXPECT_EQ(neb::math::ln(neb::floatxx_t(1)), std::log(1));
   419  
   420    neb::floatxx_t actual_x = neb::math::ln(neb::floatxx_t(0.1));
   421    float expect_x = std::log(0.1);
   422    EXPECT_TRUE(neb::math::abs(actual_x, neb::floatxx_t(expect_x)) <
   423                precesion(expect_x, 1e1 * PRECESION));
   424  
   425    actual_x = neb::math::ln(neb::floatxx_t(0.5));
   426    expect_x = std::log(0.5);
   427    EXPECT_TRUE(neb::math::abs(actual_x, neb::floatxx_t(expect_x)) <
   428                precesion(expect_x, 1e1 * PRECESION));
   429  
   430    actual_x = neb::math::ln(neb::floatxx_t(2.0));
   431    expect_x = std::log(2.0);
   432    EXPECT_TRUE(neb::math::abs(actual_x, neb::floatxx_t(expect_x)) <
   433                precesion(expect_x, 1e1 * PRECESION));
   434  
   435    float delta(0.01);
   436    for (float x = delta; x < 1.0; x += delta) {
   437      auto actual_x = neb::math::ln(neb::floatxx_t(x));
   438      auto expect_x = std::log(x);
   439      if (std::fabs(expect_x) < PRECESION) {
   440        continue;
   441      }
   442  
   443      if (std::fabs(x) < 0.01 || std::fabs(expect_x) < 0.01) {
   444        EXPECT_TRUE(neb::math::abs(actual_x, neb::floatxx_t(expect_x)) <
   445                    precesion(expect_x, 1e3 * PRECESION));
   446      } else if (std::fabs(x) < 0.1 || std::fabs(expect_x) < 0.1) {
   447        EXPECT_TRUE(neb::math::abs(actual_x, neb::floatxx_t(expect_x)) <
   448                    precesion(expect_x, 1e2 * PRECESION));
   449      } else {
   450        EXPECT_TRUE(neb::math::abs(actual_x, neb::floatxx_t(expect_x)) <
   451                    precesion(expect_x, 1e1 * PRECESION));
   452      }
   453    }
   454  
   455    delta = 1.0;
   456    for (float x = 1.0 + delta; x < 100.0; x += delta) {
   457      auto actual_x = neb::math::ln(neb::floatxx_t(x));
   458      auto expect_x = std::log(x);
   459      EXPECT_TRUE(neb::math::abs(actual_x, neb::floatxx_t(expect_x)) <
   460                  precesion(expect_x, 1e2 * PRECESION));
   461    }
   462  
   463    delta = 10.0;
   464    for (float x = 1.0 + delta; x < 1000.0; x += delta) {
   465      auto actual_x = neb::math::ln(neb::floatxx_t(x));
   466      auto expect_x = std::log(x);
   467      EXPECT_TRUE(neb::math::abs(actual_x, neb::floatxx_t(expect_x)) <
   468                  precesion(expect_x, 1e3 * PRECESION));
   469    }
   470  
   471    delta = 100.0;
   472    for (float x = 1.0 + delta; x < 10000.0; x += delta) {
   473      auto actual_x = neb::math::ln(neb::floatxx_t(x));
   474      auto expect_x = std::log(x);
   475      EXPECT_TRUE(neb::math::abs(actual_x, neb::floatxx_t(expect_x)) <
   476                  precesion(expect_x, 1e3 * PRECESION));
   477    }
   478  
   479    delta = 1000.0;
   480    // for (float x = 1.0 + delta; x < 100000.0; x += delta) {
   481    // auto actual_x = neb::math::ln(neb::floatxx_t(x));
   482    // auto expect_x = std::log(x);
   483    // EXPECT_TRUE(neb::math::abs(actual_x, neb::floatxx_t(expect_x)) <
   484    // precesion(expect_x, 1e4 * PRECESION));
   485    //}
   486  
   487    // takes time
   488    delta = 10000.0;
   489    // for (float x = 1.0 + delta; x < 1000000.0; x += delta) {
   490    // auto actual_x = neb::math::ln(neb::floatxx_t(x));
   491    // auto expect_x = std::log(x);
   492    // EXPECT_TRUE(neb::math::abs(actual_x, neb::floatxx_t(expect_x)) <
   493    // precesion(expect_x, 1e5 * PRECESION));
   494    //}
   495  }
   496  
   497  TEST(test_common_math, fast_ln) {
   498    EXPECT_EQ(neb::math::fast_ln(neb::floatxx_t(1)), std::log(1));
   499  
   500    neb::floatxx_t actual_x = neb::math::fast_ln(neb::floatxx_t(0.1));
   501    float expect_x = std::log(0.1);
   502    EXPECT_TRUE(neb::math::abs(actual_x, neb::floatxx_t(expect_x)) <
   503                precesion(expect_x, 1e1 * PRECESION));
   504  
   505    actual_x = neb::math::fast_ln(neb::floatxx_t(0.5));
   506    expect_x = std::log(0.5);
   507    EXPECT_TRUE(neb::math::abs(actual_x, neb::floatxx_t(expect_x)) <
   508                precesion(expect_x, PRECESION));
   509  
   510    actual_x = neb::math::fast_ln(neb::floatxx_t(2.0));
   511    expect_x = std::log(2.0);
   512    EXPECT_TRUE(neb::math::abs(actual_x, neb::floatxx_t(expect_x)) <
   513                precesion(expect_x, PRECESION));
   514  
   515    float delta(0.01);
   516    for (float x = delta; x < 1.0; x += delta) {
   517      auto actual_x = neb::math::fast_ln(neb::floatxx_t(x));
   518      auto expect_x = std::log(x);
   519      if (std::fabs(expect_x) < PRECESION) {
   520        continue;
   521      }
   522  
   523      if (std::fabs(x) < 0.1 || std::fabs(expect_x) < 0.1) {
   524        EXPECT_TRUE(neb::math::abs(actual_x, neb::floatxx_t(expect_x)) <
   525                    precesion(expect_x, 1e2 * PRECESION));
   526      } else {
   527        EXPECT_TRUE(neb::math::abs(actual_x, neb::floatxx_t(expect_x)) <
   528                    precesion(expect_x, 1e1 * PRECESION));
   529      }
   530    }
   531  
   532    delta = 1.0;
   533    for (float x = 1.0 + delta; x < 100.0; x += delta) {
   534      auto actual_x = neb::math::fast_ln(neb::floatxx_t(x));
   535      auto expect_x = std::log(x);
   536      EXPECT_TRUE(neb::math::abs(actual_x, neb::floatxx_t(expect_x)) <
   537                  precesion(expect_x, 1e1 * PRECESION));
   538    }
   539  
   540    delta = 10.0;
   541    for (float x = 1.0 + delta; x < 1000.0; x += delta) {
   542      auto actual_x = neb::math::fast_ln(neb::floatxx_t(x));
   543      auto expect_x = std::log(x);
   544      EXPECT_TRUE(neb::math::abs(actual_x, neb::floatxx_t(expect_x)) <
   545                  precesion(expect_x, 1e2 * PRECESION));
   546    }
   547  
   548    delta = 100.0;
   549    for (float x = 1.0 + delta; x < 10000.0; x += delta) {
   550      auto actual_x = neb::math::fast_ln(neb::floatxx_t(x));
   551      auto expect_x = std::log(x);
   552      EXPECT_TRUE(neb::math::abs(actual_x, neb::floatxx_t(expect_x)) <
   553                  precesion(expect_x, 1e3 * PRECESION));
   554    }
   555  
   556    delta = 1000.0;
   557    // for (float x = 1.0 + delta; x < 100000.0; x += delta) {
   558    // auto actual_x = neb::math::fast_ln(neb::floatxx_t(x));
   559    // auto expect_x = std::log(x);
   560    // EXPECT_TRUE(neb::math::abs(actual_x, neb::floatxx_t(expect_x)) <
   561    // precesion(expect_x, 1e4 * PRECESION));
   562    //}
   563  
   564    // takes time
   565    delta = 10000.0;
   566    // for (float x = 1.0 + delta; x < 1000000.0; x += delta) {
   567    // auto actual_x = neb::math::fast_ln(neb::floatxx_t(x));
   568    // auto expect_x = std::log(x);
   569    // EXPECT_TRUE(neb::math::abs(actual_x, neb::floatxx_t(expect_x)) <
   570    // precesion(expect_x, 1e4 * PRECESION));
   571    //}
   572  }
   573  
   574  TEST(test_common_math, pow_int_y) {
   575    float x = 0.0;
   576    int64_t y = 0;
   577    EXPECT_EQ(neb::math::pow(neb::floatxx_t(x), y), 1);
   578    EXPECT_EQ(std::pow(x, y), 1);
   579    EXPECT_EQ(neb::math::pow(neb::floatxx_t(x), y), std::pow(x, y));
   580  
   581    x = 1.0;
   582    EXPECT_EQ(neb::math::pow(neb::floatxx_t(x), y), 1);
   583    EXPECT_EQ(std::pow(x, y), 1);
   584    EXPECT_EQ(neb::math::pow(neb::floatxx_t(x), y), std::pow(x, y));
   585  
   586    x = -1.0;
   587    EXPECT_EQ(neb::math::pow(neb::floatxx_t(x), y), 1);
   588    EXPECT_EQ(std::pow(x, y), 1);
   589    EXPECT_EQ(neb::math::pow(neb::floatxx_t(x), y), std::pow(x, y));
   590  
   591    x = 123.456;
   592    EXPECT_EQ(neb::math::pow(neb::floatxx_t(x), y), 1);
   593    EXPECT_EQ(std::pow(x, y), 1);
   594    EXPECT_EQ(neb::math::pow(neb::floatxx_t(x), y), std::pow(x, y));
   595  
   596    x = -456.789;
   597    EXPECT_EQ(neb::math::pow(neb::floatxx_t(x), y), 1);
   598    EXPECT_EQ(std::pow(x, y), 1);
   599    EXPECT_EQ(neb::math::pow(neb::floatxx_t(x), y), std::pow(x, y));
   600  
   601    x = std::numeric_limits<int32_t>::min();
   602    EXPECT_EQ(neb::math::pow(neb::floatxx_t(x), y), 1);
   603    EXPECT_EQ(std::pow(x, y), 1);
   604    EXPECT_EQ(neb::math::pow(neb::floatxx_t(x), y), std::pow(x, y));
   605  
   606    x = std::numeric_limits<int32_t>::max();
   607    EXPECT_EQ(neb::math::pow(neb::floatxx_t(x), y), 1);
   608    EXPECT_EQ(std::pow(x, y), 1);
   609    EXPECT_EQ(neb::math::pow(neb::floatxx_t(x), y), std::pow(x, y));
   610  
   611    x = std::numeric_limits<int64_t>::min();
   612    EXPECT_EQ(neb::math::pow(neb::floatxx_t(x), y), 1);
   613    EXPECT_EQ(std::pow(x, y), 1);
   614    EXPECT_EQ(neb::math::pow(neb::floatxx_t(x), y), std::pow(x, y));
   615  
   616    x = std::numeric_limits<int64_t>::max();
   617    EXPECT_EQ(neb::math::pow(neb::floatxx_t(x), y), 1);
   618    EXPECT_EQ(std::pow(x, y), 1);
   619    EXPECT_EQ(neb::math::pow(neb::floatxx_t(x), y), std::pow(x, y));
   620  
   621    std::random_device rd;
   622    std::mt19937 mt(rd());
   623    std::uniform_int_distribution<> dis(std::numeric_limits<int32_t>::min(),
   624                                        std::numeric_limits<int32_t>::max());
   625    for (auto i = 0; i < 1000; i++) {
   626      x = dis(mt);
   627      EXPECT_EQ(neb::math::pow(neb::floatxx_t(x), y), 1);
   628      EXPECT_EQ(std::pow(x, y), 1);
   629      EXPECT_EQ(neb::math::pow(neb::floatxx_t(x), y), std::pow(x, y));
   630    }
   631  
   632    x = 0.0;
   633    for (auto i = 0; i < 1000; i++) {
   634      std::uniform_int_distribution<> dis_y(1,
   635                                            std::numeric_limits<int32_t>::max());
   636      y = dis_y(mt);
   637      EXPECT_EQ(neb::math::pow(neb::floatxx_t(x), y), 0);
   638      EXPECT_EQ(std::pow(x, y), 0);
   639      EXPECT_EQ(neb::math::pow(neb::floatxx_t(x), y), std::pow(x, y));
   640    }
   641  
   642    for (auto i = 0; i < 1000; i++) {
   643      std::uniform_int_distribution<> dis_x(-80, 80);
   644      std::uniform_int_distribution<> dis_y(-15, 15);
   645      x = dis_x(mt);
   646      y = dis_y(mt);
   647      if (x == 0) {
   648        continue;
   649      }
   650  
   651      auto actual_pow = neb::math::pow(neb::floatxx_t(x), y);
   652      auto expect_pow = std::pow(x, y);
   653      EXPECT_TRUE(neb::math::abs(actual_pow, neb::floatxx_t(expect_pow)) <
   654                  precesion(expect_pow));
   655    }
   656  }
   657  
   658  TEST(test_common_math, sqrt) {
   659    float x = 0.0;
   660    auto actual_sqrt = neb::math::sqrt(neb::floatxx_t(x));
   661    auto expect_sqrt = std::sqrt(x);
   662    EXPECT_TRUE(neb::math::abs(actual_sqrt, neb::floatxx_t(expect_sqrt)) <
   663                precesion(1.0f, 1e-9 * PRECESION));
   664  
   665    x = 1.0;
   666    actual_sqrt = neb::math::sqrt(neb::floatxx_t(x));
   667    expect_sqrt = std::sqrt(x);
   668    EXPECT_TRUE(neb::math::abs(actual_sqrt, neb::floatxx_t(expect_sqrt)) <
   669                precesion(expect_sqrt, 1e-1 * PRECESION));
   670  
   671    x = 0.5;
   672    actual_sqrt = neb::math::sqrt(neb::floatxx_t(x));
   673    expect_sqrt = std::sqrt(x);
   674    EXPECT_TRUE(neb::math::abs(actual_sqrt, neb::floatxx_t(expect_sqrt)) <
   675                precesion(expect_sqrt, 1e-1 * PRECESION));
   676  
   677    x = 2.0;
   678    actual_sqrt = neb::math::sqrt(neb::floatxx_t(x));
   679    expect_sqrt = std::sqrt(x);
   680    EXPECT_TRUE(neb::math::abs(actual_sqrt, neb::floatxx_t(expect_sqrt)) <
   681                precesion(expect_sqrt, 1e-1 * PRECESION));
   682  
   683    float delta(0.001);
   684    for (x = delta; x < 1.0; x += delta) {
   685      actual_sqrt = neb::math::sqrt(neb::floatxx_t(x));
   686      expect_sqrt = std::sqrt(x);
   687      EXPECT_TRUE(neb::math::abs(actual_sqrt, neb::floatxx_t(expect_sqrt)) <
   688                  precesion(expect_sqrt, 1e-1 * PRECESION));
   689    }
   690  
   691    delta = 0.1;
   692    for (x = 1.0 + delta; x < 1000.0; x += delta) {
   693      actual_sqrt = neb::math::sqrt(neb::floatxx_t(x));
   694      expect_sqrt = std::sqrt(x);
   695      EXPECT_TRUE(neb::math::abs(actual_sqrt, neb::floatxx_t(expect_sqrt)) <
   696                  precesion(expect_sqrt, 1e-1 * PRECESION));
   697    }
   698  
   699    std::random_device rd;
   700    std::mt19937 mt(rd());
   701    std::uniform_int_distribution<> dis(0, std::numeric_limits<int32_t>::max());
   702    for (auto i = 0; i < 1000; i++) {
   703      x = dis(mt);
   704      actual_sqrt = neb::math::sqrt(neb::floatxx_t(x));
   705      expect_sqrt = std::sqrt(x);
   706      EXPECT_TRUE(neb::math::abs(actual_sqrt, neb::floatxx_t(expect_sqrt)) <
   707                  precesion(expect_sqrt, 1e-1 * PRECESION));
   708    }
   709  }