add random sqrt tests
This commit is contained in:
parent
0a552c9f61
commit
18692d9baf
@ -149,7 +149,7 @@ exp_t get_exponent(const dec80* const x){
|
|||||||
#endif
|
#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
|
#ifdef EXP16
|
||||||
if (num_is_neg){
|
if (num_is_neg){
|
||||||
exponent |= 0x8000;
|
exponent |= 0x8000;
|
||||||
|
@ -60,6 +60,9 @@ typedef struct {
|
|||||||
//remove sign bit, and return 15 bit exponent sign-extended to 16 bits
|
//remove sign bit, and return 15 bit exponent sign-extended to 16 bits
|
||||||
exp_t get_exponent(const dec80* const x);
|
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);
|
void copy_decn(dec80* const dest, const dec80* const src);
|
||||||
|
|
||||||
extern dec80 AccDecn;
|
extern dec80 AccDecn;
|
||||||
|
@ -21,6 +21,7 @@
|
|||||||
|
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <random>
|
||||||
#include <boost/multiprecision/mpfr.hpp>
|
#include <boost/multiprecision/mpfr.hpp>
|
||||||
#include <catch.hpp>
|
#include <catch.hpp>
|
||||||
#include "decn.h"
|
#include "decn.h"
|
||||||
@ -341,22 +342,18 @@ TEST_CASE("division"){
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void sqrt_test(const char* x_str, int x_exp)
|
static void sqrt_test(){
|
||||||
{
|
|
||||||
CAPTURE(x_str); CAPTURE(x_exp);
|
|
||||||
build_dec80(x_str, x_exp);
|
|
||||||
// decn_to_str_complete(&AccDecn);
|
|
||||||
// printf(" acc: %s\n", Buf);
|
|
||||||
sqrt_decn();
|
|
||||||
decn_to_str_complete(&AccDecn);
|
decn_to_str_complete(&AccDecn);
|
||||||
CAPTURE(Buf); // sqrt(x)
|
CAPTURE(Buf);
|
||||||
|
//calculate result
|
||||||
//calculate actual result
|
sqrt_decn();
|
||||||
|
//build mpfr float
|
||||||
bmp::mpfr_float::default_precision(50);
|
bmp::mpfr_float::default_precision(50);
|
||||||
std::string x_full_str(x_str);
|
bmp::mpfr_float x_actual(Buf);
|
||||||
x_full_str += "e" + std::to_string(x_exp);
|
//print calc result
|
||||||
CAPTURE(x_full_str);
|
decn_to_str_complete(&AccDecn);
|
||||||
bmp::mpfr_float x_actual(x_full_str);
|
CAPTURE(Buf);
|
||||||
|
//calculate actual result
|
||||||
CAPTURE(x_actual);
|
CAPTURE(x_actual);
|
||||||
if (decn_is_nan(&AccDecn)){
|
if (decn_is_nan(&AccDecn)){
|
||||||
//check that NaN is from result of sqrt(-)
|
//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);
|
CHECK(x_actual == 0);
|
||||||
} else {
|
} else {
|
||||||
x_actual = sqrt(x_actual);
|
x_actual = sqrt(x_actual);
|
||||||
|
CAPTURE(x_actual);
|
||||||
bmp::mpfr_float calculated(Buf);
|
bmp::mpfr_float calculated(Buf);
|
||||||
bmp::mpfr_float rel_diff = abs((x_actual - calculated) / x_actual);
|
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"){
|
TEST_CASE("sqrt"){
|
||||||
sqrt_test("0", 0);
|
sqrt_test("0", 0);
|
||||||
sqrt_test("2", 0);
|
sqrt_test("2", 0);
|
||||||
@ -386,6 +391,21 @@ TEST_CASE("sqrt"){
|
|||||||
sqrt_test("123", 12345);
|
sqrt_test("123", 12345);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("sqrt random"){
|
||||||
|
std::default_random_engine generator;
|
||||||
|
std::uniform_int_distribution<int> distribution(0,99);
|
||||||
|
std::uniform_int_distribution<int> exp_distrib(-99,99);
|
||||||
|
std::uniform_int_distribution<int> 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(
|
static void log_test(
|
||||||
//input
|
//input
|
||||||
const char* x_str, int x_exp,
|
const char* x_str, int x_exp,
|
||||||
|
Loading…
Reference in New Issue
Block a user