diff --git a/CHANGELOG.md b/CHANGELOG.md index 53c2cc7..ec050f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,7 @@ This release closes the following issue:
+- [`0ba282b`](https://github.com/stdlib-js/stdlib/commit/0ba282b89c384f06bbe3ff8ecd71982f05209606) - **chore:** minor clean-up _(by Philipp Burckhardt)_ - [`603c766`](https://github.com/stdlib-js/stdlib/commit/603c76686fc8de480628e5b96efd9733b1f059b5) - **test:** only conditionally run tests _(by Philipp Burckhardt)_ - [`b884459`](https://github.com/stdlib-js/stdlib/commit/b8844596b7f57a356ff38333c0c5da9b63401874) - **feat:** add C implementation for `stats/base/dists/triangular/mean` [(#4006)](https://github.com/stdlib-js/stdlib/pull/4006) _(by Prashant Kumar Yadav, Philipp Burckhardt)_ diff --git a/README.md b/README.md index 049323a..4205667 100644 --- a/README.md +++ b/README.md @@ -256,8 +256,8 @@ int main( void ) { for ( i = 0; i < 25; i++ ) { a = random_uniform( 0.0, 10.0 ); - b = random_uniform( 0.0, 10.0 ) + a; - c = a + (b - a) * random_uniform( 0.0, 1.0 ); // mode between a and b + b = random_uniform( a, 10.0 + a ); + c = random_uniform( a, b ); // mode between a and b y = stdlib_base_dists_triangular_mean( a, b, c ); printf( "a: %lf, b: %lf, c: %lf, E(X;a,b,c): %lf\n", a, b, c, y ); } diff --git a/benchmark/c/benchmark.c b/benchmark/c/benchmark.c index 44921d2..44de7c0 100644 --- a/benchmark/c/benchmark.c +++ b/benchmark/c/benchmark.c @@ -75,13 +75,15 @@ static double tic( void ) { } /** -* Generates a random number on the interval [0,20). +* Generates a random number on the interval [min,max). * -* @return random number +* @param min minimum value (inclusive) +* @param max maximum value (exclusive) +* @return random number */ -static double rand_double( void ) { - int r = rand(); - return 20.0*(double)r / ( (double)RAND_MAX + 1.0 ); +static double random_uniform( const double min, const double max ) { + double v = (double)rand() / ( (double)RAND_MAX + 1.0 ); + return min + ( v*(max-min) ); } /** @@ -96,11 +98,11 @@ static double benchmark( void ) { double t; int i; - // Generate random parameters for the triangular distribution + // Generate random parameters for the triangular distribution: for ( i = 0; i < 100; i++ ) { - a[ i ] = rand_double() * 20.0; // Lower bound - b[ i ] = ( rand_double() * 20.0 ) + a[ i ]; // Upper bound - c[ i ] = ( rand_double() * ( b[i] - a[i] ) ) + a[i]; // Mode + a[ i ] = random_uniform( 0.0, 20.0 ); // Lower bound + b[ i ] = random_uniform( a[i], a[i]+20.0 ); // Upper bound + c[ i ] = random_uniform( a[i], b[i] ); // Mode } t = tic(); diff --git a/examples/c/example.c b/examples/c/example.c index 855158d..353aff4 100644 --- a/examples/c/example.c +++ b/examples/c/example.c @@ -34,8 +34,8 @@ int main( void ) { for ( i = 0; i < 25; i++ ) { a = random_uniform( 0.0, 10.0 ); - b = random_uniform( 0.0, 10.0 ) + a; - c = a + (b - a) * random_uniform( 0.0, 1.0 ); // mode between a and b + b = random_uniform( a, a+10.0 ); + c = random_uniform( a, b ); // mode between a and b y = stdlib_base_dists_triangular_mean( a, b, c ); printf( "a: %lf, b: %lf, c: %lf, E(X;a,b,c): %lf\n", a, b, c, y ); } diff --git a/lib/native.js b/lib/native.js index 2d7c2a6..46c4325 100644 --- a/lib/native.js +++ b/lib/native.js @@ -40,7 +40,7 @@ var addon = require( './../src/addon.node' ); * * @example * var v = mean( 2.0, 8.0, 4.0 ); -* // returns 4.666666666666667 +* // returns ~4.667 * * @example * var v = mean( -1.0, 5.0, 6.0 ); diff --git a/src/main.c b/src/main.c index 2dc9c86..5290e49 100644 --- a/src/main.c +++ b/src/main.c @@ -20,12 +20,12 @@ #include "stdlib/math/base/assert/is_nan.h" /** -* Evaluates the expected value (mean) of a triangular distribution with lower bound a, upper bound b and mode (peak) c. +* Returns the expected value of a triangular distribution. * -* @param a lower bound -* @param b upper bound -* @param c mode (peak) -* @returns expected value (mean) +* @param a minimum support +* @param b maximum support +* @param c mode +* @returns expected value * * @example * double v = mean( 0.0, 10.0, 5.0 );