Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add support for ratios evaluating as infinity in math/base/tools/evalrational-compile-c #1970

Merged
merged 4 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -25,6 +25,8 @@ var readFile = require( '@stdlib/fs/read-file' ).sync;
var replace = require( '@stdlib/string/replace' );
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
var uppercase = require( '@stdlib/string/base/uppercase' );
var PINF = require( '@stdlib/constants/float64/pinf' );
var NINF = require( '@stdlib/constants/float64/ninf' );


// VARIABLES //
Expand All @@ -51,7 +53,14 @@ var NAN_TEMPLATE = readFile( join( dir, 'nan.c.txt' ), opts );
* @returns {string} serialized value
*/
function value2string( x ) {
var str = x.toString();
var str;
if ( x === PINF ) {
kgryte marked this conversation as resolved.
Show resolved Hide resolved
return '1.0' + {{dtype_suffix}} + ' / 0.0' + {{dtype_suffix}};
}
if ( x === NINF ) {
return '-1.0' + {{dtype_suffix}} + ' / 0.0' + {{dtype_suffix}};
kgryte marked this conversation as resolved.
Show resolved Hide resolved
}
str = x.toString();
if ( isInteger( x ) ) {
str += '.0';
}
Expand Down Expand Up @@ -224,7 +233,7 @@ function compile( P, Q, options ) {
if ( n > 500 ) {
str = replace( LOOP_TEMPLATE, '{{P}}', array2list( P ) );
str = replace( str, '{{Q}}', array2list( Q ) );
str = replace( str, '{{ratio}}', value2string( P[0]/Q[0] ) );
str = replace( str, '{{ratio}}', value2string( P[0] / Q[0] ) );
str = replace( str, '{{num_coefficients}}', n.toString() );
str = replace( str, '{{dtype}}', opts.dtype );
str = replace( str, '{{dtype_suffix}}', opts.suffix );
Expand All @@ -236,7 +245,7 @@ function compile( P, Q, options ) {
str = replace( str, '{{Q_ASCENDING}}', hornerAscending( Q ) );
str = replace( str, '{{P_DESCENDING}}', hornerDescending( P ) );
str = replace( str, '{{Q_DESCENDING}}', hornerDescending( Q ) );
str = replace( str, '{{ratio}}', value2string( P[0]/Q[0] ) );
str = replace( str, '{{ratio}}', value2string( P[0] / Q[0] ) );
str = replace( str, '{{dtype}}', opts.dtype );
str = replace( str, '{{dtype_suffix}}', opts.suffix );
return replace( str, '{{fname}}', opts.name );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,3 +439,31 @@ tape( 'the function returns a function string which evaluates a rational functio

t.end();
});

tape( 'the function returns a function string which handles positive infinity correctly', opts, function test( t ) {
var expected;
var str;
var P;
var Q;

expected = '1.0 / 0.0';
P = [ 1.0, 2.0 ];
Q = [ 0.0, 1.0 ];
str = compile( P, Q );
t.ok( str.includes( expected ), 'function string handles positive infinity correctly' );
t.end();
});

tape( 'the function returns a function string which handles negative infinity correctly', opts, function test( t ) {
var expected;
var str;
var P;
var Q;

expected = '-1.0 / 0.0';
P = [ -1.0, -2.0 ];
Q = [ 0.0, 1.0 ];
str = compile( P, Q );
t.ok( str.includes( expected ), 'function string handles negative infinity correctly' );
t.end();
});
Loading