/* Given an array of doubles, return the skewness 'df' is degrees-of-freedom. Use DF_POPULATION or DF_SAMPLE (see above). */ long double skewness_value (const long double * const values, size_t n, int df) { long double moment2=0; long double moment3=0; long double mean; long double skewness; if (n<=1) return nanl (""); mean = arithmetic_mean_value (values, n); for (size_t i = 0; i < n; i++) { const long double t = (values[i] - mean); moment2 += t*t; moment3 += t*t*t; } moment2 /= n; moment3 /= n; /* can't use 'powl (moment2,3.0/2.0)' - not all systems have powl */ skewness = moment3 / sqrtl (moment2*moment2*moment2); if ( df == DF_SAMPLE ) { if (n<=2) return nanl (""); skewness = ( sqrtl (n*(n-1)) / (n-2) ) * skewness; } return skewness; }
/* Given an array of doubles, return the excess kurtosis 'df' is degrees-of-freedom. Use DF_POPULATION or DF_SAMPLE (see above). */ long double _GL_ATTRIBUTE_PURE excess_kurtosis_value (const long double * const values, size_t n, int df) { long double moment2=0; long double moment4=0; long double mean; long double excess_kurtosis; if (n<=1) return nanl (""); mean = arithmetic_mean_value (values, n); for (size_t i = 0; i < n; i++) { const long double t = (values[i] - mean); moment2 += t*t; moment4 += t*t*t*t; } moment2 /= n; moment4 /= n; excess_kurtosis = moment4 / (moment2*moment2) - 3; if ( df == DF_SAMPLE ) { if (n<=3) return nanl (""); excess_kurtosis = ( ((long double)n-1) / (((long double)n-2)*((long double)n-3)) ) * ( (n+1)*excess_kurtosis + 6 ) ; } return excess_kurtosis; }
TEST(math, logbl) { ASSERT_EQ(-HUGE_VAL, logbl(0.0L)); ASSERT_TRUE(isnan(logbl(nanl("")))); ASSERT_TRUE(isinf(logbl(HUGE_VALL))); ASSERT_EQ(0.0L, logbl(1.0L)); ASSERT_EQ(3.0L, logbl(10.0L)); }
TEST(math, ilogbl) { ASSERT_EQ(FP_ILOGB0, ilogbl(0.0L)); ASSERT_EQ(FP_ILOGBNAN, ilogbl(nanl(""))); ASSERT_EQ(INT_MAX, ilogbl(HUGE_VALL)); ASSERT_EQ(0L, ilogbl(1.0L)); ASSERT_EQ(3L, ilogbl(10.0L)); }
TEST(math, __fpclassifyl) { EXPECT_EQ(FP_INFINITE, __fpclassifyl(HUGE_VALL)); EXPECT_EQ(FP_NAN, __fpclassifyl(nanl(""))); EXPECT_EQ(FP_NORMAL, __fpclassifyl(1.0L)); EXPECT_EQ(FP_SUBNORMAL, __fpclassifyl(ldouble_subnormal())); EXPECT_EQ(FP_ZERO, __fpclassifyl(0.0L)); }
TEST(math, isnan) { ASSERT_FALSE(test_capture_isnan(123.0f)); ASSERT_FALSE(test_capture_isnan(123.0)); ASSERT_FALSE(test_capture_isnan(123.0L)); ASSERT_TRUE(test_capture_isnan(nanf(""))); ASSERT_TRUE(test_capture_isnan(nan(""))); ASSERT_TRUE(test_capture_isnan(nanl(""))); }
/* Standard error of skewness (SES), given the sample size 'n' */ long double SES_value (size_t n) { if (n<=2) return nanl (""); return sqrtl ( (long double)(6.0*n*(n-1)) / ((long double)(n-2)*(n+1)*(n+3)) ); }
/* Skewness Test statistics Z = ( sample skewness / SES ) */ long double skewnessZ_value (const long double * const values, size_t n) { const long double skew = skewness_value (values,n,DF_SAMPLE); const long double SES = SES_value (n); if (isnan (skew) || isnan (SES) ) return nanl (""); return skew/SES; }
TEST(math, roundl) { fesetround(FE_TOWARDZERO); // roundl ignores the rounding mode and always rounds away from zero. ASSERT_FLOAT_EQ(1.0, roundl(0.5)); ASSERT_FLOAT_EQ(-1.0, roundl(-0.5)); ASSERT_FLOAT_EQ(0.0, roundl(0.0)); ASSERT_FLOAT_EQ(-0.0, roundl(-0.0)); ASSERT_TRUE(isnan(roundl(nanl("")))); ASSERT_FLOAT_EQ(HUGE_VALL, roundl(HUGE_VALL)); }
/* Kurtosis Test statistics Z = ( sample kurtosis / SEK ) */ long double kurtosisZ_value (const long double * const values, size_t n) { const long double kurt = excess_kurtosis_value (values,n,DF_SAMPLE); const long double SEK = SEK_value (n); if (isnan (kurt) || isnan (SEK) ) return nanl (""); return kurt/SEK; }
long double acoshl(long double x) { if(x < 1.0) { errno = EDOM; return nanl(NULL); } /* FIXME implement */ return 0.0; }
long double atanhl(long double x) { if(abs(x) >= 1.0) { errno = EDOM; return nanl(NULL); } /* FIXME implement */ return 0.0; }
void testValues() { f = 2; long double result; result = nanl(NULL); // assert result == 0 || \is_NaN(result); //@ assert f == 2; //@ assert vacuous: \false; }
/* Given an array of doubles, return the p-Value Of the Jarque-Bera Test for normality http://en.wikipedia.org/wiki/Jarque%E2%80%93Bera_test Equivalent to R's "jarque.test ()" function in the "moments" library. */ long double jarque_bera_pvalue (const long double * const values, size_t n ) { const long double k = excess_kurtosis_value (values,n,DF_POPULATION); const long double s = skewness_value (values,n,DF_POPULATION); const long double jb = (long double)(n*(s*s + k*k/4))/6.0 ; const long double pval = 1.0 - pchisq_df2 (jb); if (n<=1 || isnan (k) || isnan (s)) return nanl (""); return pval; }
/* Standard error of kurtisos (SEK), given the sample size 'n' */ long double SEK_value (size_t n) { const long double ses = SES_value (n); if (n<=3) return nanl (""); return 2 * ses * sqrtl ( (long double)(n*n-1) / ((long double)((n-3)*(n+5))) ); }
TEST(math, __fpclassifyl) { #if defined(__BIONIC__) EXPECT_EQ(FP_INFINITE, __fpclassifyl(HUGE_VALL)); EXPECT_EQ(FP_NAN, __fpclassifyl(nanl(""))); EXPECT_EQ(FP_NORMAL, __fpclassifyl(1.0)); EXPECT_EQ(FP_SUBNORMAL, __fpclassifyl(double_subnormal())); EXPECT_EQ(FP_ZERO, __fpclassifyl(0.0)); #else // __BIONIC__ GTEST_LOG_(INFO) << "This test does nothing.\n"; #endif // __BIONIC__ }
TEST(math, roundl) { auto guard = make_scope_guard([]() { fesetenv(FE_DFL_ENV); }); fesetround(FE_TOWARDZERO); // roundl ignores the rounding mode and always rounds away from zero. ASSERT_DOUBLE_EQ(1.0L, roundl(0.5L)); ASSERT_DOUBLE_EQ(-1.0L, roundl(-0.5L)); ASSERT_DOUBLE_EQ(0.0L, roundl(0.0L)); ASSERT_DOUBLE_EQ(-0.0L, roundl(-0.0L)); ASSERT_TRUE(isnan(roundl(nanl("")))); ASSERT_DOUBLE_EQ(HUGE_VALL, roundl(HUGE_VALL)); }
/* D'Agostino-Perason omnibus test for normality. returns the p-value, where the null-hypothesis is normal distribution. */ long double dagostino_pearson_omnibus_pvalue (const long double * const values, size_t n) { const long double z_skew = skewnessZ_value (values, n); const long double z_kurt = kurtosisZ_value (values, n); const long double DP = z_skew*z_skew + z_kurt*z_kurt; const long double pval = 1.0 - pchisq_df2 (DP); if (isnan (z_skew) || isnan (z_kurt)) return nanl (""); return pval; }
long double _GL_ATTRIBUTE_PURE variance_value (const long double * const values, size_t n, int df) { long double sum=0; long double mean; long double variance; assert (df>=0); /* LCOV_EXCL_LINE */ if ( (size_t)df == n ) return nanl (""); mean = arithmetic_mean_value (values, n); sum = 0 ; for (size_t i = 0; i < n; i++) sum += (values[i] - mean) * (values[i] - mean); variance = sum / ( n - df ); return variance; }
/* acosh(x) = log (x + sqrt(x * x - 1)) */ long double acoshl (long double x) { if (isnan (x)) return x; if (x < 1.0L) { errno = EDOM; return nanl(""); } if (x > 0x1p32L) /* Avoid overflow (and unnecessary calculation when sqrt (x * x - 1) == x). The M_LN2 define doesn't have enough precison for long double so use this one. GCC optimizes by replacing the const with a fldln2 insn. */ return __fast_logl (x) + 6.9314718055994530941723E-1L; /* Since x >= 1, the arg to log will always be greater than the fyl2xp1 limit (approx 0.29) so just use logl. */ return __fast_logl (x + __fast_sqrtl((x + 1.0L) * (x - 1.0L))); }
TEST(math, fpclassify) { ASSERT_EQ(FP_INFINITE, fpclassify(INFINITY)); ASSERT_EQ(FP_INFINITE, fpclassify(HUGE_VALF)); ASSERT_EQ(FP_INFINITE, fpclassify(HUGE_VAL)); ASSERT_EQ(FP_INFINITE, fpclassify(HUGE_VALL)); ASSERT_EQ(FP_NAN, fpclassify(nanf(""))); ASSERT_EQ(FP_NAN, fpclassify(nan(""))); ASSERT_EQ(FP_NAN, fpclassify(nanl(""))); ASSERT_EQ(FP_NORMAL, fpclassify(1.0f)); ASSERT_EQ(FP_NORMAL, fpclassify(1.0)); ASSERT_EQ(FP_NORMAL, fpclassify(1.0L)); ASSERT_EQ(FP_SUBNORMAL, fpclassify(float_subnormal())); ASSERT_EQ(FP_SUBNORMAL, fpclassify(double_subnormal())); ASSERT_EQ(FP_SUBNORMAL, fpclassify(ldouble_subnormal())); ASSERT_EQ(FP_ZERO, fpclassify(0.0f)); ASSERT_EQ(FP_ZERO, fpclassify(0.0)); ASSERT_EQ(FP_ZERO, fpclassify(0.0L)); }
/* atanh (x) = 0.5 * log ((1.0 + x)/(1.0 - x)) */ long double atanhl (long double x) { long double z; if (isnan (x)) return x; z = fabsl (x); if (z == 1.0L) { errno = ERANGE; return (x > 0 ? INFINITY : -INFINITY); } if ( z > 1.0L) { errno = EDOM; return nanl(""); } /* Rearrange formula to avoid precision loss for small x. atanh(x) = 0.5 * log ((1.0 + x)/(1.0 - x)) = 0.5 * log1p ((1.0 + x)/(1.0 - x) - 1.0) = 0.5 * log1p ((1.0 + x - 1.0 + x) /(1.0 - x)) = 0.5 * log1p ((2.0 * x ) / (1.0 - x)) */ z = 0.5L * __fast_log1pl ((z + z) / (1.0L - z)); return x >= 0 ? z : -z; }
void testnan(const char *nan_format) { char nan_str[128]; char *end; long double ald[4]; double ad[4]; float af[4]; int i; snprintf(nan_str, sizeof(nan_str), "nan(%s)", nan_format); for (i = 0; i < 4; i++) { /* * x86 has an 80-bit long double stored in 96 bits, * so we need to initialize the memory for the memcmp() * checks below to work. */ bzero(&af[i], sizeof(float)); bzero(&ad[i], sizeof(double)); bzero(&ald[i], sizeof(long double)); } af[0] = nanf(nan_format); assert(isnan(af[0])); af[1] = strtof(nan_str, &end); assert(end == nan_str + strlen(nan_str)); assert(sscanf(nan_str, "%e", &af[2]) == 1); assert(memcmp(&af[0], &af[1], sizeof(float)) == 0); #if !__gnu_linux__ assert(memcmp(&af[1], &af[2], sizeof(float)) == 0); #endif /* !__gnu_linux__ */ if (*nan_format == '\0') { /* nanf("") == strtof("nan") */ af[3] = strtof("nan", NULL); assert(memcmp(&af[2], &af[3], sizeof(float)) == 0); } ad[0] = nan(nan_format); assert(isnan(ad[0])); ad[1] = strtod(nan_str, &end); assert(end == nan_str + strlen(nan_str)); assert(sscanf(nan_str, "%le", &ad[2]) == 1); assert(memcmp(&ad[0], &ad[1], sizeof(double)) == 0); #if !__gnu_linux__ assert(memcmp(&ad[1], &ad[2], sizeof(double)) == 0); #endif /* !__gnu_linux__ */ if (*nan_format == '\0') { /* nan("") == strtod("nan") */ ad[3] = strtod("nan", NULL); assert(memcmp(&ad[2], &ad[3], sizeof(double)) == 0); } ald[0] = nanl(nan_format); assert(isnan(ald[0])); ald[1] = strtold(nan_str, &end); assert(end == nan_str + strlen(nan_str)); assert(sscanf(nan_str, "%Le", &ald[2]) == 1); assert(memcmp(&ald[0], &ald[1], sizeof(long double)) == 0); #if 0 assert(memcmp(&ald[1], &ald[2], sizeof(long double)) == 0); if (*nan_format == '\0') { /* nanl("") == strtold("nan") */ ald[3] = strtold("nan", NULL); assert(memcmp(&ald[2], &ald[3], sizeof(long double)) == 0); } #endif }
void domathl (void) { #ifndef NO_LONG_DOUBLE long double f1; long double f2; int i1; f1 = acosl(0.0); fprintf( stdout, "acosl : %Lf\n", f1); f1 = acoshl(0.0); fprintf( stdout, "acoshl : %Lf\n", f1); f1 = asinl(1.0); fprintf( stdout, "asinl : %Lf\n", f1); f1 = asinhl(1.0); fprintf( stdout, "asinhl : %Lf\n", f1); f1 = atanl(M_PI_4); fprintf( stdout, "atanl : %Lf\n", f1); f1 = atan2l(2.3, 2.3); fprintf( stdout, "atan2l : %Lf\n", f1); f1 = atanhl(1.0); fprintf( stdout, "atanhl : %Lf\n", f1); f1 = cbrtl(27.0); fprintf( stdout, "cbrtl : %Lf\n", f1); f1 = ceill(3.5); fprintf( stdout, "ceill : %Lf\n", f1); f1 = copysignl(3.5, -2.5); fprintf( stdout, "copysignl : %Lf\n", f1); f1 = cosl(M_PI_2); fprintf( stdout, "cosl : %Lf\n", f1); f1 = coshl(M_PI_2); fprintf( stdout, "coshl : %Lf\n", f1); f1 = erfl(42.0); fprintf( stdout, "erfl : %Lf\n", f1); f1 = erfcl(42.0); fprintf( stdout, "erfcl : %Lf\n", f1); f1 = expl(0.42); fprintf( stdout, "expl : %Lf\n", f1); f1 = exp2l(0.42); fprintf( stdout, "exp2l : %Lf\n", f1); f1 = expm1l(0.00042); fprintf( stdout, "expm1l : %Lf\n", f1); f1 = fabsl(-1.123); fprintf( stdout, "fabsl : %Lf\n", f1); f1 = fdiml(1.123, 2.123); fprintf( stdout, "fdiml : %Lf\n", f1); f1 = floorl(0.5); fprintf( stdout, "floorl : %Lf\n", f1); f1 = floorl(-0.5); fprintf( stdout, "floorl : %Lf\n", f1); f1 = fmal(2.1, 2.2, 3.01); fprintf( stdout, "fmal : %Lf\n", f1); f1 = fmaxl(-0.42, 0.42); fprintf( stdout, "fmaxl : %Lf\n", f1); f1 = fminl(-0.42, 0.42); fprintf( stdout, "fminl : %Lf\n", f1); f1 = fmodl(42.0, 3.0); fprintf( stdout, "fmodl : %Lf\n", f1); /* no type-specific variant */ i1 = fpclassify(1.0); fprintf( stdout, "fpclassify : %d\n", i1); f1 = frexpl(42.0, &i1); fprintf( stdout, "frexpl : %Lf\n", f1); f1 = hypotl(42.0, 42.0); fprintf( stdout, "hypotl : %Lf\n", f1); i1 = ilogbl(42.0); fprintf( stdout, "ilogbl : %d\n", i1); /* no type-specific variant */ i1 = isfinite(3.0); fprintf( stdout, "isfinite : %d\n", i1); /* no type-specific variant */ i1 = isgreater(3.0, 3.1); fprintf( stdout, "isgreater : %d\n", i1); /* no type-specific variant */ i1 = isgreaterequal(3.0, 3.1); fprintf( stdout, "isgreaterequal : %d\n", i1); /* no type-specific variant */ i1 = isinf(3.0); fprintf( stdout, "isinf : %d\n", i1); /* no type-specific variant */ i1 = isless(3.0, 3.1); fprintf( stdout, "isless : %d\n", i1); /* no type-specific variant */ i1 = islessequal(3.0, 3.1); fprintf( stdout, "islessequal : %d\n", i1); /* no type-specific variant */ i1 = islessgreater(3.0, 3.1); fprintf( stdout, "islessgreater : %d\n", i1); /* no type-specific variant */ i1 = isnan(0.0); fprintf( stdout, "isnan : %d\n", i1); /* no type-specific variant */ i1 = isnormal(3.0); fprintf( stdout, "isnormal : %d\n", i1); /* no type-specific variant */ f1 = isunordered(1.0, 2.0); fprintf( stdout, "isunordered : %d\n", i1); f1 = j0l(1.2); fprintf( stdout, "j0l : %Lf\n", f1); f1 = j1l(1.2); fprintf( stdout, "j1l : %Lf\n", f1); f1 = jnl(2,1.2); fprintf( stdout, "jnl : %Lf\n", f1); f1 = ldexpl(1.2,3); fprintf( stdout, "ldexpl : %Lf\n", f1); f1 = lgammal(42.0); fprintf( stdout, "lgammal : %Lf\n", f1); f1 = llrintl(-0.5); fprintf( stdout, "llrintl : %Lf\n", f1); f1 = llrintl(0.5); fprintf( stdout, "llrintl : %Lf\n", f1); f1 = llroundl(-0.5); fprintf( stdout, "lroundl : %Lf\n", f1); f1 = llroundl(0.5); fprintf( stdout, "lroundl : %Lf\n", f1); f1 = logl(42.0); fprintf( stdout, "logl : %Lf\n", f1); f1 = log10l(42.0); fprintf( stdout, "log10l : %Lf\n", f1); f1 = log1pl(42.0); fprintf( stdout, "log1pl : %Lf\n", f1); f1 = log2l(42.0); fprintf( stdout, "log2l : %Lf\n", f1); f1 = logbl(42.0); fprintf( stdout, "logbl : %Lf\n", f1); f1 = lrintl(-0.5); fprintf( stdout, "lrintl : %Lf\n", f1); f1 = lrintl(0.5); fprintf( stdout, "lrintl : %Lf\n", f1); f1 = lroundl(-0.5); fprintf( stdout, "lroundl : %Lf\n", f1); f1 = lroundl(0.5); fprintf( stdout, "lroundl : %Lf\n", f1); f1 = modfl(42.0,&f2); fprintf( stdout, "lmodfl : %Lf\n", f1); f1 = nanl(""); fprintf( stdout, "nanl : %Lf\n", f1); f1 = nearbyintl(1.5); fprintf( stdout, "nearbyintl : %Lf\n", f1); f1 = nextafterl(1.5,2.0); fprintf( stdout, "nextafterl : %Lf\n", f1); f1 = powl(3.01, 2.0); fprintf( stdout, "powl : %Lf\n", f1); f1 = remainderl(3.01,2.0); fprintf( stdout, "remainderl : %Lf\n", f1); f1 = remquol(29.0,3.0,&i1); fprintf( stdout, "remquol : %Lf\n", f1); f1 = rintl(0.5); fprintf( stdout, "rintl : %Lf\n", f1); f1 = rintl(-0.5); fprintf( stdout, "rintl : %Lf\n", f1); f1 = roundl(0.5); fprintf( stdout, "roundl : %Lf\n", f1); f1 = roundl(-0.5); fprintf( stdout, "roundl : %Lf\n", f1); f1 = scalblnl(1.2,3); fprintf( stdout, "scalblnl : %Lf\n", f1); f1 = scalbnl(1.2,3); fprintf( stdout, "scalbnl : %Lf\n", f1); /* no type-specific variant */ i1 = signbit(1.0); fprintf( stdout, "signbit : %i\n", i1); f1 = sinl(M_PI_4); fprintf( stdout, "sinl : %Lf\n", f1); f1 = sinhl(M_PI_4); fprintf( stdout, "sinhl : %Lf\n", f1); f1 = sqrtl(9.0); fprintf( stdout, "sqrtl : %Lf\n", f1); f1 = tanl(M_PI_4); fprintf( stdout, "tanl : %Lf\n", f1); f1 = tanhl(M_PI_4); fprintf( stdout, "tanhl : %Lf\n", f1); f1 = tgammal(2.1); fprintf( stdout, "tgammal : %Lf\n", f1); f1 = truncl(3.5); fprintf( stdout, "truncl : %Lf\n", f1); f1 = y0l(1.2); fprintf( stdout, "y0l : %Lf\n", f1); f1 = y1l(1.2); fprintf( stdout, "y1l : %Lf\n", f1); f1 = ynl(3,1.2); fprintf( stdout, "ynl : %Lf\n", f1); #endif }
void runSuccess() { char buf[] = ""; //@ assert \valid(buf+(0..\block_length(buf)-1)); //@ assert \exists integer x; buf[x] == '\0'; nanl(buf); }
void test_nan() { static_assert((std::is_same<decltype(nan("")), double>::value), ""); static_assert((std::is_same<decltype(nanf("")), float>::value), ""); static_assert((std::is_same<decltype(nanl("")), long double>::value), ""); }
void runSuccess1() { nanl(NULL); }
TEST(math, log1pl) { ASSERT_EQ(-HUGE_VALL, log1pl(-1.0L)); ASSERT_TRUE(isnan(log1pl(nanl("")))); ASSERT_TRUE(isinf(log1pl(HUGE_VALL))); ASSERT_DOUBLE_EQ(1.0L, log1pl(M_E - 1.0L)); }
TEST(math, powl) { ASSERT_TRUE(__isnanl(powl(nanl(""), 3.0L))); ASSERT_DOUBLE_EQ(1.0L, (powl(1.0L, nanl("")))); ASSERT_TRUE(__isnanl(powl(2.0L, nanl("")))); ASSERT_DOUBLE_EQ(8.0L, powl(2.0L, 3.0L)); }
TEST(math, fminl) { ASSERT_DOUBLE_EQ(10.0L, fminl(12.0L, 10.0L)); ASSERT_DOUBLE_EQ(12.0L, fminl(12.0L, nanl(""))); ASSERT_DOUBLE_EQ(12.0L, fminl(nanl(""), 12.0L)); }