diff --git a/CHANGELOG.md b/CHANGELOG.md index 75b61f3..da8ce87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@
-## Unreleased (2024-11-11) +## Unreleased (2024-11-17)
@@ -22,6 +22,7 @@
+- [`163a3e7`](https://github.com/stdlib-js/stdlib/commit/163a3e7fa2c7429f88b2dd69df42572f9ff0af9d) - **refactor:** update benchmark, add `f` suffixes, missing spaces in `math/base/special/gcdf` [(#3121)](https://github.com/stdlib-js/stdlib/pull/3121) _(by Gunj Joshi, Athan Reines)_ - [`f8bcfd8`](https://github.com/stdlib-js/stdlib/commit/f8bcfd832483d46068c710b6854d5f97bcb778fd) - **feat:** add `math/base/special/gcdf` [(#2997)](https://github.com/stdlib-js/stdlib/pull/2997) _(by Aayush Khanna, Philipp Burckhardt)_
@@ -34,9 +35,11 @@ ### Contributors -A total of 2 people contributed to this release. Thank you to the following contributors: +A total of 4 people contributed to this release. Thank you to the following contributors: - Aayush Khanna +- Athan Reines +- Gunj Joshi - Philipp Burckhardt
diff --git a/CONTRIBUTORS b/CONTRIBUTORS index c9f811c..b2ba387 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -59,6 +59,7 @@ Mohammad Kaif <98884589+Kaif987@users.noreply.github.com> Momtchil Momtchev Muhammad Haris Naresh Jagadeesan +Neeraj Pathak NightKnight Nithin Katta <88046362+nithinkatta@users.noreply.github.com> Nourhan Hasan <109472010+TheNourhan@users.noreply.github.com> @@ -69,6 +70,7 @@ Prajwal Kulkarni Pranav Goswami Praneki <97080887+PraneGIT@users.noreply.github.com> Pratik <97464067+Pratik772846@users.noreply.github.com> +Pratyush Kumar Chouhan Priyansh <88396544+itsspriyansh@users.noreply.github.com> Pushpendra Chandravanshi RISHAV <115060907+rishav2404@users.noreply.github.com> @@ -79,6 +81,7 @@ Ridam Garg <67867319+RidamGarg@users.noreply.github.com> Robert Gislason Roman Stetsyk <25715951+romanstetsyk@users.noreply.github.com> Rutam <138517416+performant23@users.noreply.github.com> +Ruthwik Chikoti <145591715+ruthwikchikoti@users.noreply.github.com> Ryan Seal Sai Srikar Dumpeti <80447788+the-r3aper7@users.noreply.github.com> SarthakPaandey <145528240+SarthakPaandey@users.noreply.github.com> diff --git a/README.md b/README.md index f948b88..4f3a041 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ limitations under the License. [![NPM version][npm-image]][npm-url] [![Build Status][test-image]][test-url] [![Coverage Status][coverage-image]][coverage-url] -> Compute the [greatest common divisor][gcd] (gcd) of two single-precision floating point numbers. +> Compute the [greatest common divisor][gcd] (gcd) of two single-precision floating-point numbers. @@ -77,7 +77,7 @@ var gcdf = require( '@stdlib/math-base-special-gcdf' ); #### gcdf( a, b ) -Computes the [greatest common divisor][gcd] (gcd). +Computes the [greatest common divisor][gcd] (gcd) of two single-precision floating-point numbers. ```javascript var v = gcdf( 48, 18 ); @@ -172,7 +172,7 @@ for ( i = 0; i < a.length; i++ ) { #### stdlib_base_gcdf( a, b ) -Computes the greatest common divisor (gcd). +Computes the greatest common divisor (gcd) of two single-precision floating-point numbers. ```c float v = stdlib_base_gcdf( 48.0f, 18.0f ); diff --git a/benchmark/c/native/benchmark.c b/benchmark/c/native/benchmark.c index db4620f..3c8a187 100644 --- a/benchmark/c/native/benchmark.c +++ b/benchmark/c/native/benchmark.c @@ -91,17 +91,20 @@ static float rand_float( void ) { */ static double benchmark( void ) { double elapsed; - float a; - float b; - float y; + float a[ 100 ]; + float b[ 100 ]; double t; + float y; int i; + for ( i = 0; i < 100; i++ ) { + a[ i ] = roundf( 500.0f * rand_float() ); + b[ i ] = roundf( 500.0f * rand_float() ); + } + t = tic(); for ( i = 0; i < ITERATIONS; i++ ) { - a = round( 500.0f * rand_float() ); - b = round( 500.0f * rand_float() ); - y = stdlib_base_gcdf( a, b ); + y = stdlib_base_gcdf( a[ i % 100 ], b[ i % 100 ] ); if ( y != y ) { printf( "should not return NaN\n" ); break; diff --git a/dist/index.js.map b/dist/index.js.map index 850c73a..9c37f74 100644 --- a/dist/index.js.map +++ b/dist/index.js.map @@ -1,7 +1,7 @@ { "version": 3, "sources": ["../lib/bitwise_binary_gcd.js", "../lib/binary_gcd.js", "../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2024 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Computes the greatest common divisor (gcd) of two single-precision floating point numbers using the binary GCD algorithm and bitwise operations.\n*\n* ## References\n*\n* - Stein, Josef. 1967. \"Computational problems associated with Racah algebra.\" _Journal of Computational Physics_ 1 (3): 397\u2013405. doi:[10.1016/0021-9991(67)90047-2][@stein:1967].\n*\n* [@stein:1967]: https://doi.org/10.1016/0021-9991(67)90047-2\n*\n* @private\n* @param {integer32} a - first number\n* @param {integer32} b - second number\n* @returns {integer32} greatest common divisor\n*\n* @example\n* var v = gcdf( 48, 18 );\n* // returns 6\n*/\nfunction gcdf( a, b ) {\n\tvar k = 0;\n\tvar t;\n\n\t// Simple cases:\n\tif ( a === 0 ) {\n\t\treturn b;\n\t}\n\tif ( b === 0 ) {\n\t\treturn a;\n\t}\n\t// Reduce `a` and/or `b` to odd numbers and keep track of the greatest power of 2 dividing both `a` and `b`...\n\twhile ( (a & 1) === 0 && (b & 1) === 0 ) {\n\t\ta >>>= 1; // right shift\n\t\tb >>>= 1; // right shift\n\t\tk += 1;\n\t}\n\t// Reduce `a` to an odd number...\n\twhile ( (a & 1) === 0 ) {\n\t\ta >>>= 1; // right shift\n\t}\n\t// Henceforth, `a` is always odd...\n\twhile ( b ) {\n\t\t// Remove all factors of 2 in `b`, as they are not common...\n\t\twhile ( (b & 1) === 0 ) {\n\t\t\tb >>>= 1; // right shift\n\t\t}\n\t\t// `a` and `b` are both odd. Swap values such that `b` is the larger of the two values, and then set `b` to the difference (which is even)...\n\t\tif ( a > b ) {\n\t\t\tt = b;\n\t\t\tb = a;\n\t\t\ta = t;\n\t\t}\n\t\tb -= a; // b=0 iff b=a\n\t}\n\t// Restore common factors of 2...\n\treturn a << k;\n}\n\n\n// EXPORTS //\n\nmodule.exports = gcdf;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2024 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Computes the greatest common divisor (gcd) of two single-precision floating point numbers using the binary GCD algorithm.\n*\n* ## References\n*\n* - Stein, Josef. 1967. \"Computational problems associated with Racah algebra.\" _Journal of Computational Physics_ 1 (3): 397\u2013405. doi:[10.1016/0021-9991(67)90047-2][@stein:1967].\n*\n* [@stein:1967]: https://doi.org/10.1016/0021-9991(67)90047-2\n*\n* @private\n* @param {integer} a - first number\n* @param {integer} b - second number\n* @returns {integer} greatest common divisor\n*\n* @example\n* var v = gcdf( 16777216.0, 65536.0 );\n* // returns 65536\n*/\nfunction gcdf( a, b ) {\n\tvar k = 1;\n\tvar t;\n\n\t// Simple cases:\n\tif ( a === 0 ) {\n\t\treturn b;\n\t}\n\tif ( b === 0 ) {\n\t\treturn a;\n\t}\n\t// Reduce `a` and/or `b` to odd numbers and keep track of the greatest power of 2 dividing both `a` and `b`...\n\twhile ( a%2 === 0 && b%2 === 0 ) {\n\t\ta /= 2; // right shift\n\t\tb /= 2; // right shift\n\t\tk *= 2; // left shift\n\t}\n\t// Reduce `a` to an odd number...\n\twhile ( a%2 === 0 ) {\n\t\ta /= 2; // right shift\n\t}\n\t// Henceforth, `a` is always odd...\n\twhile ( b ) {\n\t\t// Remove all factors of 2 in `b`, as they are not common...\n\t\twhile ( b%2 === 0 ) {\n\t\t\tb /= 2; // right shift\n\t\t}\n\t\t// `a` and `b` are both odd. Swap values such that `b` is the larger of the two values, and then set `b` to the difference (which is even)...\n\t\tif ( a > b ) {\n\t\t\tt = b;\n\t\t\tb = a;\n\t\t\ta = t;\n\t\t}\n\t\tb -= a; // b=0 iff b=a\n\t}\n\t// Restore common factors of 2...\n\treturn k * a;\n}\n\n\n// EXPORTS //\n\nmodule.exports = gcdf;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2024 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isnanf = require( '@stdlib/math-base-assert-is-nanf' );\nvar isIntegerf = require( '@stdlib/math-base-assert-is-integerf' );\nvar PINF = require( '@stdlib/constants-float32-pinf' );\nvar NINF = require( '@stdlib/constants-float32-ninf' );\nvar INT32_MAX = require( '@stdlib/constants-int32-max' );\nvar bitwisef = require( './bitwise_binary_gcd.js' );\nvar largeIntegersf = require( './binary_gcd.js' );\n\n\n// MAIN //\n\n/**\n* Computes the greatest common divisor (gcd) of two single-precision floating point numbers.\n*\n* @param {integer} a - first number\n* @param {integer} b - second number\n* @returns {integer} greatest common divisor\n*\n* @example\n* var v = gcdf( 48, 18 );\n* // returns 6\n*\n* @example\n* var v = gcdf( 3.14, 18 );\n* // returns NaN\n*\n* @example\n* var v = gcdf( NaN, 18 );\n* // returns NaN\n*/\nfunction gcdf( a, b ) {\n\tif ( isnanf( a ) || isnanf( b ) ) {\n\t\treturn NaN;\n\t}\n\tif (\n\t\ta === PINF ||\n\t\tb === PINF ||\n\t\ta === NINF ||\n\t\tb === NINF\n\t) {\n\t\treturn NaN;\n\t}\n\tif ( !( isIntegerf( a ) && isIntegerf( b ) ) ) {\n\t\treturn NaN;\n\t}\n\tif ( a < 0 ) {\n\t\ta = -a;\n\t}\n\tif ( b < 0 ) {\n\t\tb = -b;\n\t}\n\tif ( a <= INT32_MAX && b <= INT32_MAX ) {\n\t\treturn bitwisef( a, b );\n\t}\n\treturn largeIntegersf( a, b );\n}\n\n\n// EXPORTS //\n\nmodule.exports = gcdf;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2024 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Compute the greatest common divisor (gcd) of two single-precision floating point numbers.\n*\n* @module @stdlib/math-base-special-gcdf\n*\n* @example\n* var gcd = require( '@stdlib/math-base-special-gcdf' );\n*\n* var v = gcdf( 48, 18 );\n* // returns 6\n*/\n\n// MODULES //\n\nvar main = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsCA,SAASC,EAAMC,EAAGC,EAAI,CACrB,IAAIC,EAAI,EACJ,EAGJ,GAAKF,IAAM,EACV,OAAOC,EAER,GAAKA,IAAM,E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I,CAACA,GAEDD,GAAKJ,GAAaK,GAAKL,EACpBC,EAAUG,EAAGC,CAAE,EAEhBH,EAAgBE,EAAGC,CAAE,GAXpB,GAYT,CAKAV,EAAO,QAAUQ,IChDjB,IAAIG,EAAO,IAKX,OAAO,QAAUA", + "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2024 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Computes the greatest common divisor (gcd) of two single-precision floating-point numbers using the binary GCD algorithm and bitwise operations.\n*\n* ## References\n*\n* - Stein, Josef. 1967. \"Computational problems associated with Racah algebra.\" _Journal of Computational Physics_ 1 (3): 397\u2013405. doi:[10.1016/0021-9991(67)90047-2][@stein:1967].\n*\n* [@stein:1967]: https://doi.org/10.1016/0021-9991(67)90047-2\n*\n* @private\n* @param {integer32} a - first number\n* @param {integer32} b - second number\n* @returns {integer32} greatest common divisor\n*\n* @example\n* var v = gcdf( 48, 18 );\n* // returns 6\n*/\nfunction gcdf( a, b ) {\n\tvar k = 0;\n\tvar t;\n\n\t// Simple cases:\n\tif ( a === 0 ) {\n\t\treturn b;\n\t}\n\tif ( b === 0 ) {\n\t\treturn a;\n\t}\n\n\t// Reduce `a` and/or `b` to odd numbers and keep track of the greatest power of 2 dividing both `a` and `b`...\n\twhile ( ( a & 1 ) === 0 && ( b & 1 ) === 0 ) {\n\t\ta >>>= 1; // right shift\n\t\tb >>>= 1; // right shift\n\t\tk += 1;\n\t}\n\n\t// Reduce `a` to an odd number...\n\twhile ( ( a & 1 ) === 0 ) {\n\t\ta >>>= 1; // right shift\n\t}\n\n\t// Henceforth, `a` is always odd...\n\twhile ( b ) {\n\t\t// Remove all factors of 2 in `b`, as they are not common...\n\t\twhile ( ( b & 1 ) === 0 ) {\n\t\t\tb >>>= 1; // right shift\n\t\t}\n\n\t\t// `a` and `b` are both odd. Swap values such that `b` is the larger of the two values, and then set `b` to the difference (which is even)...\n\t\tif ( a > b ) {\n\t\t\tt = b;\n\t\t\tb = a;\n\t\t\ta = t;\n\t\t}\n\t\tb -= a; // b=0 iff b=a\n\t}\n\n\t// Restore common factors of 2...\n\treturn a << k;\n}\n\n\n// EXPORTS //\n\nmodule.exports = gcdf;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2024 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Computes the greatest common divisor (gcd) of two single-precision floating-point numbers using the binary GCD algorithm.\n*\n* ## References\n*\n* - Stein, Josef. 1967. \"Computational problems associated with Racah algebra.\" _Journal of Computational Physics_ 1 (3): 397\u2013405. doi:[10.1016/0021-9991(67)90047-2][@stein:1967].\n*\n* [@stein:1967]: https://doi.org/10.1016/0021-9991(67)90047-2\n*\n* @private\n* @param {integer} a - first number\n* @param {integer} b - second number\n* @returns {integer} greatest common divisor\n*\n* @example\n* var v = gcdf( 16777216, 65536 );\n* // returns 65536\n*/\nfunction gcdf( a, b ) {\n\tvar k = 1;\n\tvar t;\n\n\t// Simple cases:\n\tif ( a === 0 ) {\n\t\treturn b;\n\t}\n\tif ( b === 0 ) {\n\t\treturn a;\n\t}\n\n\t// Reduce `a` and/or `b` to odd numbers and keep track of the greatest power of 2 dividing both `a` and `b`...\n\twhile ( a % 2 === 0 && b % 2 === 0 ) {\n\t\ta /= 2; // right shift\n\t\tb /= 2; // right shift\n\t\tk *= 2; // left shift\n\t}\n\n\t// Reduce `a` to an odd number...\n\twhile ( a % 2 === 0 ) {\n\t\ta /= 2; // right shift\n\t}\n\n\t// Henceforth, `a` is always odd...\n\twhile ( b ) {\n\t\t// Remove all factors of 2 in `b`, as they are not common...\n\t\twhile ( b % 2 === 0 ) {\n\t\t\tb /= 2; // right shift\n\t\t}\n\n\t\t// `a` and `b` are both odd. Swap values such that `b` is the larger of the two values, and then set `b` to the difference (which is even)...\n\t\tif ( a > b ) {\n\t\t\tt = b;\n\t\t\tb = a;\n\t\t\ta = t;\n\t\t}\n\t\tb -= a; // b=0 iff b=a\n\t}\n\n\t// Restore common factors of 2...\n\treturn k * a;\n}\n\n\n// EXPORTS //\n\nmodule.exports = gcdf;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2024 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isnanf = require( '@stdlib/math-base-assert-is-nanf' );\nvar isIntegerf = require( '@stdlib/math-base-assert-is-integerf' );\nvar PINF = require( '@stdlib/constants-float32-pinf' );\nvar NINF = require( '@stdlib/constants-float32-ninf' );\nvar INT32_MAX = require( '@stdlib/constants-int32-max' );\nvar bitwisef = require( './bitwise_binary_gcd.js' );\nvar largeIntegersf = require( './binary_gcd.js' );\n\n\n// MAIN //\n\n/**\n* Computes the greatest common divisor (gcd) of two single-precision floating-point numbers.\n*\n* @param {integer} a - first number\n* @param {integer} b - second number\n* @returns {integer} greatest common divisor\n*\n* @example\n* var v = gcdf( 48, 18 );\n* // returns 6\n*\n* @example\n* var v = gcdf( 3.14, 18 );\n* // returns NaN\n*\n* @example\n* var v = gcdf( NaN, 18 );\n* // returns NaN\n*/\nfunction gcdf( a, b ) {\n\tif ( isnanf( a ) || isnanf( b ) ) {\n\t\treturn NaN;\n\t}\n\tif (\n\t\ta === PINF ||\n\t\tb === PINF ||\n\t\ta === NINF ||\n\t\tb === NINF\n\t) {\n\t\treturn NaN;\n\t}\n\tif ( !( isIntegerf( a ) && isIntegerf( b ) ) ) {\n\t\treturn NaN;\n\t}\n\tif ( a < 0 ) {\n\t\ta = -a;\n\t}\n\tif ( b < 0 ) {\n\t\tb = -b;\n\t}\n\tif ( a <= INT32_MAX && b <= INT32_MAX ) {\n\t\treturn bitwisef( a, b );\n\t}\n\treturn largeIntegersf( a, b );\n}\n\n\n// EXPORTS //\n\nmodule.exports = gcdf;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2024 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Compute the greatest common divisor (gcd) of two single-precision floating-point numbers.\n*\n* @module @stdlib/math-base-special-gcdf\n*\n* @example\n* var gcd = require( '@stdlib/math-base-special-gcdf' );\n*\n* var v = gcdf( 48, 18 );\n* // returns 6\n*/\n\n// MODULES //\n\nvar main = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"], + "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsCA,SAASC,EAAMC,EAAGC,EAAI,CAC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GAAKP,EAAYQ,CAAE,GAGpCD,EAAI,IACRA,EAAI,CAACA,GAEDC,EAAI,IACRA,EAAI,CAACA,GAEDD,GAAKJ,GAAaK,GAAKL,EACpBC,EAAUG,EAAGC,CAAE,EAEhBH,EAAgBE,EAAGC,CAAE,GAXpB,GAYT,CAKAV,EAAO,QAAUQ,IChDjB,IAAIG,EAAO,IAKX,OAAO,QAAUA", "names": ["require_bitwise_binary_gcd", "__commonJSMin", "exports", "module", "gcdf", "a", "b", "k", "require_binary_gcd", "__commonJSMin", "exports", "module", "gcdf", "a", "b", "k", "require_main", "__commonJSMin", "exports", "module", "isnanf", "isIntegerf", "PINF", "NINF", "INT32_MAX", "bitwisef", "largeIntegersf", "gcdf", "a", "b", "main"] } diff --git a/docs/repl.txt b/docs/repl.txt index 5efee3c..6b48e41 100644 --- a/docs/repl.txt +++ b/docs/repl.txt @@ -1,7 +1,7 @@ {{alias}}( a, b ) Computes the greatest common divisor (gcd) of two single-precision - floating point numbers. + floating-point numbers. If both `a` and `b` are `0`, the function returns `0`. diff --git a/docs/types/index.d.ts b/docs/types/index.d.ts index 2de00fc..14e8d11 100644 --- a/docs/types/index.d.ts +++ b/docs/types/index.d.ts @@ -19,7 +19,7 @@ // TypeScript Version: 4.1 /** -* Computes the greatest common divisor (gcd) of two single-precision floating point numbers. +* Computes the greatest common divisor (gcd) of two single-precision floating-point numbers. * * ## Notes * diff --git a/include/stdlib/math/base/special/gcdf.h b/include/stdlib/math/base/special/gcdf.h index 38eff6f..d0138eb 100644 --- a/include/stdlib/math/base/special/gcdf.h +++ b/include/stdlib/math/base/special/gcdf.h @@ -27,7 +27,7 @@ extern "C" { #endif /** -* Computes the greatest common divisor (gcd) of two single-precision floating point numbers. +* Computes the greatest common divisor (gcd) of two single-precision floating-point numbers. */ float stdlib_base_gcdf( const float a, const float b ); diff --git a/lib/binary_gcd.js b/lib/binary_gcd.js index 8ea085d..8010937 100644 --- a/lib/binary_gcd.js +++ b/lib/binary_gcd.js @@ -19,7 +19,7 @@ 'use strict'; /** -* Computes the greatest common divisor (gcd) of two single-precision floating point numbers using the binary GCD algorithm. +* Computes the greatest common divisor (gcd) of two single-precision floating-point numbers using the binary GCD algorithm. * * ## References * @@ -33,7 +33,7 @@ * @returns {integer} greatest common divisor * * @example -* var v = gcdf( 16777216.0, 65536.0 ); +* var v = gcdf( 16777216, 65536 ); * // returns 65536 */ function gcdf( a, b ) { @@ -47,22 +47,26 @@ function gcdf( a, b ) { if ( b === 0 ) { return a; } + // Reduce `a` and/or `b` to odd numbers and keep track of the greatest power of 2 dividing both `a` and `b`... - while ( a%2 === 0 && b%2 === 0 ) { + while ( a % 2 === 0 && b % 2 === 0 ) { a /= 2; // right shift b /= 2; // right shift k *= 2; // left shift } + // Reduce `a` to an odd number... - while ( a%2 === 0 ) { + while ( a % 2 === 0 ) { a /= 2; // right shift } + // Henceforth, `a` is always odd... while ( b ) { // Remove all factors of 2 in `b`, as they are not common... - while ( b%2 === 0 ) { + while ( b % 2 === 0 ) { b /= 2; // right shift } + // `a` and `b` are both odd. Swap values such that `b` is the larger of the two values, and then set `b` to the difference (which is even)... if ( a > b ) { t = b; @@ -71,6 +75,7 @@ function gcdf( a, b ) { } b -= a; // b=0 iff b=a } + // Restore common factors of 2... return k * a; } diff --git a/lib/bitwise_binary_gcd.js b/lib/bitwise_binary_gcd.js index 94084fa..35c50d4 100644 --- a/lib/bitwise_binary_gcd.js +++ b/lib/bitwise_binary_gcd.js @@ -19,7 +19,7 @@ 'use strict'; /** -* Computes the greatest common divisor (gcd) of two single-precision floating point numbers using the binary GCD algorithm and bitwise operations. +* Computes the greatest common divisor (gcd) of two single-precision floating-point numbers using the binary GCD algorithm and bitwise operations. * * ## References * @@ -47,22 +47,26 @@ function gcdf( a, b ) { if ( b === 0 ) { return a; } + // Reduce `a` and/or `b` to odd numbers and keep track of the greatest power of 2 dividing both `a` and `b`... - while ( (a & 1) === 0 && (b & 1) === 0 ) { + while ( ( a & 1 ) === 0 && ( b & 1 ) === 0 ) { a >>>= 1; // right shift b >>>= 1; // right shift k += 1; } + // Reduce `a` to an odd number... - while ( (a & 1) === 0 ) { + while ( ( a & 1 ) === 0 ) { a >>>= 1; // right shift } + // Henceforth, `a` is always odd... while ( b ) { // Remove all factors of 2 in `b`, as they are not common... - while ( (b & 1) === 0 ) { + while ( ( b & 1 ) === 0 ) { b >>>= 1; // right shift } + // `a` and `b` are both odd. Swap values such that `b` is the larger of the two values, and then set `b` to the difference (which is even)... if ( a > b ) { t = b; @@ -71,6 +75,7 @@ function gcdf( a, b ) { } b -= a; // b=0 iff b=a } + // Restore common factors of 2... return a << k; } diff --git a/lib/index.js b/lib/index.js index 83f0a65..807f179 100644 --- a/lib/index.js +++ b/lib/index.js @@ -19,7 +19,7 @@ 'use strict'; /** -* Compute the greatest common divisor (gcd) of two single-precision floating point numbers. +* Compute the greatest common divisor (gcd) of two single-precision floating-point numbers. * * @module @stdlib/math-base-special-gcdf * diff --git a/lib/main.js b/lib/main.js index 2f8f3a5..90b080c 100644 --- a/lib/main.js +++ b/lib/main.js @@ -32,7 +32,7 @@ var largeIntegersf = require( './binary_gcd.js' ); // MAIN // /** -* Computes the greatest common divisor (gcd) of two single-precision floating point numbers. +* Computes the greatest common divisor (gcd) of two single-precision floating-point numbers. * * @param {integer} a - first number * @param {integer} b - second number diff --git a/lib/native.js b/lib/native.js index 67ab26a..dc56388 100644 --- a/lib/native.js +++ b/lib/native.js @@ -26,7 +26,7 @@ var addon = require( './../src/addon.node' ); // MAIN // /** -* Computes the greatest common divisor (gcd) of two single-precision floating point numbers. +* Computes the greatest common divisor (gcd) of two single-precision floating-point numbers. * * @private * @param {number} a - first number diff --git a/package.json b/package.json index fc02baa..20f8745 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@stdlib/math-base-special-gcdf", "version": "0.0.0", - "description": "Compute the greatest common divisor (gcd).", + "description": "Compute the greatest common divisor (gcd) of two single-precision floating-point numbers.", "license": "Apache-2.0", "author": { "name": "The Stdlib Authors", @@ -97,7 +97,7 @@ "scaffold": { "$schema": "math/base@v1.0", "base_alias": "gcd", - "alias": "gcd", + "alias": "gcdf", "pkg_desc": "compute the greatest common divisor (gcd)", "desc": "computes the greatest common divisor (gcd)", "short_desc": "greatest common divisor (gcd)", @@ -108,8 +108,8 @@ "type": { "javascript": "integer", "jsdoc": "integer", - "c": "double", - "dtype": "float64" + "c": "float", + "dtype": "float32" }, "domain": [ { @@ -153,8 +153,8 @@ "type": { "javascript": "integer", "jsdoc": "integer", - "c": "double", - "dtype": "float64" + "c": "float", + "dtype": "float32" }, "domain": [ { @@ -198,8 +198,8 @@ "type": { "javascript": "number", "jsdoc": "number", - "c": "double", - "dtype": "float64" + "c": "float", + "dtype": "float32" } }, "keywords": [ diff --git a/src/main.c b/src/main.c index 41667d0..efa96f2 100644 --- a/src/main.c +++ b/src/main.c @@ -26,15 +26,15 @@ #include /** -* Computes the greatest common divisor (gcd) using the binary GCD algorithm. +* Computes the greatest common divisor (gcd) of two single-precision floating-point numbers using the binary GCD algorithm. * -* @param a first number -* @param b second number -* @return greatest common divisor +* @param a first number +* @param b second number +* @return greatest common divisor * * @example -* float out = largeIntegersf(16777216.0, 65536.0); -* // returns 65536.0 +* float out = largeIntegersf( 16777216.0f, 65536.0f ); +* // returns 65536.0f */ static float largeIntegersf( const float a, const float b ) { float ac; @@ -43,32 +43,35 @@ static float largeIntegersf( const float a, const float b ) { float t; // Simple cases: - if ( a == 0.0 ) { + if ( a == 0.0f ) { return b; } - if ( b == 0.0 ) { + if ( b == 0.0f ) { return a; } ac = a; bc = b; - k = 1.0; + k = 1.0f; // Reduce `a` and/or `b` to odd numbers and keep track of the greatest power of 2 dividing both `a` and `b`... - while ( stdlib_base_fmodf( ac, 2.0 ) == 0.0 && stdlib_base_fmodf( bc, 2.0 ) == 0.0 ) { - ac /= 2.0; // right shift - bc /= 2.0; // right shift - k *= 2.0; // left shift + while ( stdlib_base_fmodf( ac, 2.0f ) == 0.0f && stdlib_base_fmodf( bc, 2.0f ) == 0.0f ) { + ac /= 2.0f; // right shift + bc /= 2.0f; // right shift + k *= 2.0f; // left shift } + // Reduce `a` to an odd number... - while ( stdlib_base_fmodf( ac, 2.0 ) == 0.0 ) { - ac /= 2.0; // right shift + while ( stdlib_base_fmodf( ac, 2.0f ) == 0.0f ) { + ac /= 2.0f; // right shift } + // Henceforth, `a` is always odd... while ( bc ) { // Remove all factors of 2 in `b`, as they are not common... - while ( stdlib_base_fmodf( bc, 2.0 ) == 0.0 ) { - bc /= 2.0; // right shift + while ( stdlib_base_fmodf( bc, 2.0f ) == 0.0f ) { + bc /= 2.0f; // right shift } + // `a` and `b` are both odd. Swap values such that `b` is the larger of the two values, and then set `b` to the difference (which is even)... if ( ac > bc ) { t = bc; @@ -77,20 +80,21 @@ static float largeIntegersf( const float a, const float b ) { } bc -= ac; // b=0 iff b=a } + // Restore common factors of 2... return k * ac; } /** -* Computes the greatest common divisor (gcd) using the binary GCD algorithm and bitwise operations. +* Computes the greatest common divisor (gcd) of two single-precision floating-point numbers using the binary GCD algorithm and bitwise operations. * * @param a first number * @param b second number * @return greatest common divisor * * @example -* float out = bitwisef( 48.0, 18.0 ); -* // returns 6.0 +* float out = bitwisef( 48.0f, 18.0f ); +* // returns 6.0f */ static float bitwisef( const float a, const float b ) { int32_t ac; @@ -99,10 +103,10 @@ static float bitwisef( const float a, const float b ) { int32_t t; // Simple cases: - if ( a == 0.0 ) { + if ( a == 0.0f ) { return b; } - if ( b == 0.0 ) { + if ( b == 0.0f ) { return a; } ac = (int32_t)a; @@ -110,21 +114,24 @@ static float bitwisef( const float a, const float b ) { k = 0; // Reduce `a` and/or `b` to odd numbers and keep track of the greatest power of 2 dividing both `a` and `b`... - while ( (ac & 1) == 0 && (bc & 1) == 0 ) { + while ( ( ac & 1 ) == 0 && ( bc & 1 ) == 0 ) { ac >>= 1; // right shift bc >>= 1; // right shift k += 1; } + // Reduce `a` to an odd number... - while ( (ac & 1) == 0 ) { + while ( ( ac & 1 ) == 0 ) { ac >>= 1; // right shift } + // Henceforth, `a` is always odd... while ( bc ) { // Remove all factors of 2 in `b`, as they are not common... - while ( (bc & 1) == 0 ) { + while ( ( bc & 1 ) == 0 ) { bc >>= 1; // right shift } + // `a` and `b` are both odd. Swap values such that `b` is the larger of the two values, and then set `b` to the difference (which is even)... if ( ac > bc ) { t = bc; @@ -133,27 +140,28 @@ static float bitwisef( const float a, const float b ) { } bc -= ac; // b=0 iff b=a } + // Restore common factors of 2... return ac << k; } /** -* Computes the greatest common divisor (gcd). +* Computes the greatest common divisor (gcd) of two single-precision floating-point numbers. * * @param a first number * @param b second number * @return greatest common divisor * * @example -* float out = stdlib_base_gcdf( 48.0, 18.0 ); -* // returns 6.0 +* float out = stdlib_base_gcdf( 48.0f, 18.0f ); +* // returns 6.0f */ float stdlib_base_gcdf( const float a, const float b ) { float ac; float bc; if ( stdlib_base_is_nanf( a ) || stdlib_base_is_nanf( b ) ) { - return 0.0/0.0; // NaN + return 0.0f / 0.0f; // NaN } if ( a == STDLIB_CONSTANT_FLOAT32_PINF || @@ -161,10 +169,10 @@ float stdlib_base_gcdf( const float a, const float b ) { a == STDLIB_CONSTANT_FLOAT32_NINF || b == STDLIB_CONSTANT_FLOAT32_NINF ) { - return 0.0/0.0; // NaN + return 0.0f / 0.0f; // NaN } if ( !( stdlib_base_is_integerf( a ) && stdlib_base_is_integerf( b ) ) ) { - return 0.0/0.0; // NaN + return 0.0f / 0.0f; // NaN } ac = a; bc = b;