diff --git a/src/decn/decn.c b/src/decn/decn.c index c44a87a..7b51bbd 100644 --- a/src/decn/decn.c +++ b/src/decn/decn.c @@ -149,7 +149,7 @@ exp_t get_exponent(const dec80* const x){ #endif } -static void set_exponent(dec80* acc, exp_t exponent, uint8_t num_is_neg){ +void set_exponent(dec80* acc, exp_t exponent, uint8_t num_is_neg){ #ifdef EXP16 if (num_is_neg){ exponent |= 0x8000; diff --git a/src/decn/decn.h b/src/decn/decn.h index 46f649e..22e8f48 100644 --- a/src/decn/decn.h +++ b/src/decn/decn.h @@ -60,6 +60,9 @@ typedef struct { //remove sign bit, and return 15 bit exponent sign-extended to 16 bits exp_t get_exponent(const dec80* const x); +void set_exponent(dec80* acc, exp_t exponent, uint8_t num_is_neg); + + void copy_decn(dec80* const dest, const dec80* const src); extern dec80 AccDecn; diff --git a/src/decn/decn_tests.cpp b/src/decn/decn_tests.cpp index 912a2ab..24965d9 100644 --- a/src/decn/decn_tests.cpp +++ b/src/decn/decn_tests.cpp @@ -21,6 +21,7 @@ #include +#include #include #include #include "decn.h" @@ -341,22 +342,18 @@ TEST_CASE("division"){ ); } -static void sqrt_test(const char* x_str, int x_exp) -{ - CAPTURE(x_str); CAPTURE(x_exp); - build_dec80(x_str, x_exp); - // decn_to_str_complete(&AccDecn); - // printf(" acc: %s\n", Buf); - sqrt_decn(); +static void sqrt_test(){ decn_to_str_complete(&AccDecn); - CAPTURE(Buf); // sqrt(x) - - //calculate actual result + CAPTURE(Buf); + //calculate result + sqrt_decn(); + //build mpfr float bmp::mpfr_float::default_precision(50); - std::string x_full_str(x_str); - x_full_str += "e" + std::to_string(x_exp); - CAPTURE(x_full_str); - bmp::mpfr_float x_actual(x_full_str); + bmp::mpfr_float x_actual(Buf); + //print calc result + decn_to_str_complete(&AccDecn); + CAPTURE(Buf); + //calculate actual result CAPTURE(x_actual); if (decn_is_nan(&AccDecn)){ //check that NaN is from result of sqrt(-) @@ -366,12 +363,20 @@ static void sqrt_test(const char* x_str, int x_exp) CHECK(x_actual == 0); } else { x_actual = sqrt(x_actual); + CAPTURE(x_actual); bmp::mpfr_float calculated(Buf); bmp::mpfr_float rel_diff = abs((x_actual - calculated) / x_actual); - CHECK(rel_diff < 3e-16); //TODO + CHECK(rel_diff < 2e-17); } } +static void sqrt_test(const char* x_str, int x_exp) +{ + CAPTURE(x_str); CAPTURE(x_exp); + build_dec80(x_str, x_exp); + sqrt_test(); +} + TEST_CASE("sqrt"){ sqrt_test("0", 0); sqrt_test("2", 0); @@ -386,6 +391,21 @@ TEST_CASE("sqrt"){ sqrt_test("123", 12345); } +TEST_CASE("sqrt random"){ + std::default_random_engine generator; + std::uniform_int_distribution distribution(0,99); + std::uniform_int_distribution exp_distrib(-99,99); + std::uniform_int_distribution sign_distrib(0,1); + for (int j = 0; j < 12345; j++){ + for (int i = 0; i < DEC80_NUM_LSU; i++){ + AccDecn.lsu[i] = distribution(generator); + } + int sign = sign_distrib(generator); + set_exponent(&AccDecn, exp_distrib(generator), sign); + sqrt_test(); + } +} + static void log_test( //input const char* x_str, int x_exp,