Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,9 @@

'use strict';

// MODULES //

var sqrt = require( '@stdlib/math/base/special/sqrt' );


// VARIABLES //

var SQRT1O12 = sqrt( 1.0/12.0 );
var SQRT1O12 = 0.28867513459481287; // sqrt( 1.0/12.0 );


// MAIN //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

#include "stdlib/stats/base/dists/uniform/stdev.h"
#include "stdlib/math/base/assert/is_nan.h"
#include "stdlib/math/base/special/sqrt.h"

/**
* Returns the standard deviation of a uniform distribution.
Expand All @@ -31,6 +30,9 @@
* double v = stdev( 4.0, 12.0 );
* // returns ~2.309
*/

static const double SQRT_ONE_TWELFTH = 0.28867513459481287; // stdlib_base_sqrt( 1.0/12.0 )

double stdlib_base_dists_uniform_stdev( const double a, const double b ) {
if (
stdlib_base_is_nan( a ) ||
Expand All @@ -39,5 +41,5 @@ double stdlib_base_dists_uniform_stdev( const double a, const double b ) {
) {
return 0.0 / 0.0; // NaN
}
return stdlib_base_sqrt( 1.0/12.0 ) * ( b-a );
return SQRT_ONE_TWELFTH * ( b-a );
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,9 @@ tape( 'the function returns the standard deviation of a uniform distribution', f
b = data.b;
for ( i = 0; i < expected.length; i++ ) {
y = stdev( a[i], b[i] );
if ( y === expected[i] ) {
t.equal( y, expected[i], 'a: '+a[i]+', b: '+b[i]+', y: '+y+', expected: '+expected[i] );
} else {
delta = abs( y - expected[ i ] );
tol = 1.0 * EPS * abs( expected[ i ] );
t.ok( delta <= tol, 'within tolerance. a: '+a[i]+'. b: '+b[i]+'. y: '+y+'. E: '+expected[ i ]+'. Δ: '+delta+'. tol: '+tol+'.' );
}
delta = abs( y - expected[ i ] );
tol = 1.0 * EPS * abs( expected[ i ] );
t.ok( delta <= tol, 'within tolerance. a: '+a[i]+'. b: '+b[i]+'. y: '+y+'. E: '+expected[ i ]+'. Δ: '+delta+'. tol: '+tol+'.' );
}
t.end();
});
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ tape( 'if provided `a >= b`, the function returns `NaN`', opts, function test( t
t.end();
});

tape( 'the function returns the standard deviation of a uniform distribution', function test( t ) {
tape( 'the function returns the standard deviation of a uniform distribution', opts, function test( t ) {
var expected;
var delta;
var tol;
Expand All @@ -93,13 +93,9 @@ tape( 'the function returns the standard deviation of a uniform distribution', f
b = data.b;
for ( i = 0; i < expected.length; i++ ) {
y = stdev( a[i], b[i] );
if ( y === expected[i] ) {
t.equal( y, expected[i], 'a: '+a[i]+', b: '+b[i]+', y: '+y+', expected: '+expected[i] );
} else {
delta = abs( y - expected[ i ] );
tol = 1.0 * EPS * abs( expected[ i ] );
t.ok( delta <= tol, 'within tolerance. a: '+a[i]+'. b: '+b[i]+'. y: '+y+'. E: '+expected[ i ]+'. Δ: '+delta+'. tol: '+tol+'.' );
}
delta = abs( y - expected[ i ] );
tol = 1.0 * EPS * abs( expected[ i ] );
t.ok( delta <= tol, 'within tolerance. a: '+a[i]+'. b: '+b[i]+'. y: '+y+'. E: '+expected[ i ]+'. Δ: '+delta+'. tol: '+tol+'.' );
}
t.end();
});
Loading