Skip to content
Open
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
7 changes: 7 additions & 0 deletions lib/node_modules/@stdlib/math/base/special/cosh/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

var isnan = require( '@stdlib/math/base/assert/is-nan' );
var exp = require( '@stdlib/math/base/special/exp' );
var MAX_LN = require( '@stdlib/constants/float64/max-ln' );


// MAIN //
Expand Down Expand Up @@ -78,13 +79,19 @@ var exp = require( '@stdlib/math/base/special/exp' );
* // returns NaN
*/
function cosh( x ) {
var t;
if ( isnan( x ) ) {
return x;
}
if ( x < 0.0 ) {
x = -x;
}
if ( x > 21.0 ) {
// Avoid premature overflow by evaluating `exp(x/2) * exp(x/2) / 2`:
if ( x > MAX_LN ) {
t = exp( 0.5 * x );
return ( 0.5 * t ) * t;
}
return exp( x ) / 2.0;
}
return ( exp(x) + exp(-x) ) / 2.0;
Expand Down
9 changes: 6 additions & 3 deletions lib/node_modules/@stdlib/math/base/special/cosh/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"dependencies": [
"@stdlib/math/base/napi/unary",
"@stdlib/math/base/assert/is-nan",
"@stdlib/math/base/special/exp"
"@stdlib/math/base/special/exp",
"@stdlib/constants/float64/max-ln"
]
},
{
Expand All @@ -53,7 +54,8 @@
"libpath": [],
"dependencies": [
"@stdlib/math/base/assert/is-nan",
"@stdlib/math/base/special/exp"
"@stdlib/math/base/special/exp",
"@stdlib/constants/float64/max-ln"
]
},
{
Expand All @@ -68,7 +70,8 @@
"libpath": [],
"dependencies": [
"@stdlib/math/base/assert/is-nan",
"@stdlib/math/base/special/exp"
"@stdlib/math/base/special/exp",
"@stdlib/constants/float64/max-ln"
]
}
]
Expand Down
7 changes: 7 additions & 0 deletions lib/node_modules/@stdlib/math/base/special/cosh/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "stdlib/math/base/special/cosh.h"
#include "stdlib/math/base/assert/is_nan.h"
#include "stdlib/math/base/special/exp.h"
#include "stdlib/constants/float64/max_ln.h"

/**
* Computes the hyperbolic cosine of a double-precision floating-point number.
Expand Down Expand Up @@ -61,6 +62,7 @@
*/
double stdlib_base_cosh( const double x ) {
double xc;
double t;
if ( stdlib_base_is_nan( x ) ) {
return x;
}
Expand All @@ -70,6 +72,11 @@ double stdlib_base_cosh( const double x ) {
xc = x;
}
if ( xc > 21.0 ) {
// Avoid premature overflow by evaluating `exp(x/2) * exp(x/2) / 2`:
if ( xc > STDLIB_CONSTANT_FLOAT64_MAX_LN ) {
t = stdlib_base_exp( 0.5 * xc );
return ( 0.5 * t ) * t;
}
return stdlib_base_exp( xc ) / 2.0;
}
return ( stdlib_base_exp( xc ) + stdlib_base_exp( -xc ) ) / 2.0;
Expand Down
22 changes: 22 additions & 0 deletions lib/node_modules/@stdlib/math/base/special/cosh/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,25 @@ tape( 'the function returns `+infinity` if provided a `-infinity`', function tes
t.strictEqual( v, PINF, 'returns expected value' );
t.end();
});

tape( 'the function does not overflow for values whose hyperbolic cosine is representable', function test( t ) {
var expected;
var x;
var v;
var i;

x = [ 709.9, 710.0, 710.4 ];
expected = [
1.010701028059782e+308,
1.1169973830808555e+308,
1.6663642832806496e+308
];
for ( i = 0; i < x.length; i++ ) {
v = cosh( x[ i ] );
t.strictEqual( isAlmostSameValue( v, expected[ i ], 2 ), true, 'returns expected value' );
v = cosh( -x[ i ] );
t.strictEqual( isAlmostSameValue( v, expected[ i ], 2 ), true, 'returns expected value' );
}
t.strictEqual( cosh( 711.0 ), PINF, 'returns expected value' );
t.end();
});
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,25 @@ tape( 'the function returns `+infinity` if provided a `-infinity`', opts, functi
t.strictEqual( v, PINF, 'returns expected value' );
t.end();
});

tape( 'the function does not overflow for values whose hyperbolic cosine is representable', opts, function test( t ) {
var expected;
var x;
var v;
var i;

x = [ 709.9, 710.0, 710.4 ];
expected = [
1.010701028059782e+308,
1.1169973830808555e+308,
1.6663642832806496e+308
];
for ( i = 0; i < x.length; i++ ) {
v = cosh( x[ i ] );
t.strictEqual( isAlmostSameValue( v, expected[ i ], 2 ), true, 'returns expected value' );
v = cosh( -x[ i ] );
t.strictEqual( isAlmostSameValue( v, expected[ i ], 2 ), true, 'returns expected value' );
}
t.strictEqual( cosh( 711.0 ), PINF, 'returns expected value' );
t.end();
});
Loading