Skip to content

Commit

Permalink
chore: apply review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
aman-095 committed Apr 18, 2024
1 parent 50fd6f6 commit b669a75
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 43 deletions.
10 changes: 5 additions & 5 deletions lib/node_modules/@stdlib/math/base/special/gcd/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,14 @@ double stdlib_base_gcd( const double a, const double b );
#include <stdio.h>
int main( void ) {
const double a = { 24.0, 32.0, 48.0, 116.0, 33.0 };
const double b = { 12.0, 6.0, 15.0, 52.0, 22.0 };
const double a[ 5 ] = { 24.0, 32.0, 48.0, 116.0, 33.0 };
const double b[ 5 ] = { 12.0, 6.0, 15.0, 52.0, 22.0 };
double out;
int i;
for ( i = 0; i < 5; i++ ) {
out = stdlib_base_gcd( a, b );
printf( "gcd(%lf, %lf) = %lf\n", a, b, out );
out = stdlib_base_gcd( a[ i ], b[ i ] );
printf( "gcd(%lf, %lf) = %lf\n", a[ i ], b[ i ], out );
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ double benchmark() {

t = tic();
for ( i = 0; i < ITERATIONS; i++ ) {
a = ( 50.0*rand_double() );
b = ( 50.0*rand_double() );
a = round( 500.0*rand_double() );
b = round( 500.0*rand_double() );
y = stdlib_base_gcd( a, b );
if ( y != y ) {
printf( "should not return NaN\n" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@
#include <stdio.h>

int main( void ) {
double out;
double a;
double b;
int i;
const double a[ 5 ] = { 24.0, 32.0, 48.0, 116.0, 33.0 };
const double b[ 5 ] = { 12.0, 6.0, 15.0, 52.0, 22.0 };

for ( i = 0; i < 100; i++ ) {
a = ( ( (double)rand() / (double)RAND_MAX ) * 1000.0 );
b = ( ( (double)rand() / (double)RAND_MAX ) * 500.0 );
out = stdlib_base_gcd( a, b );
printf( "gcd(%lf, %lf) = %lf\n", a, b, out );
}
double out;
int i;
for ( i = 0; i < 5; i++ ) {
out = stdlib_base_gcd( a[ i ], b[ i ] );
printf( "gcd(%lf, %lf) = %lf\n", a[ i ], b[ i ], out );
}
}
9 changes: 3 additions & 6 deletions lib/node_modules/@stdlib/math/base/special/gcd/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@
"@stdlib/math/base/assert/is-nan",
"@stdlib/math/base/assert/is-integer",
"@stdlib/constants/float64/pinf",
"@stdlib//constants/float64/ninf",
"@stdlib/constants/int32/max"
"@stdlib/constants/float64/ninf"
]
},
{
Expand All @@ -58,8 +57,7 @@
"@stdlib/math/base/assert/is-nan",
"@stdlib/math/base/assert/is-integer",
"@stdlib/constants/float64/pinf",
"@stdlib//constants/float64/ninf",
"@stdlib/constants/int32/max"
"@stdlib/constants/float64/ninf"
]
},
{
Expand All @@ -76,8 +74,7 @@
"@stdlib/math/base/assert/is-nan",
"@stdlib/math/base/assert/is-integer",
"@stdlib/constants/float64/pinf",
"@stdlib//constants/float64/ninf",
"@stdlib/constants/int32/max"
"@stdlib/constants/float64/ninf"
]
}
]
Expand Down
41 changes: 21 additions & 20 deletions lib/node_modules/@stdlib/math/base/special/gcd/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
#include "stdlib/math/base/assert/is_integer.h"
#include "stdlib/constants/float64/pinf.h"
#include "stdlib//constants/float64/ninf.h"
#include "stdlib/constants/int32/max.h"
#include <stdint.h>
#include <math.h>

static const int64_t STDLIB_CONSTANT_INT64_MAX = 9223372036854775807;

/**
* Computes the greatest common divisor (gcd) using the binary GCD algorithm.
Expand All @@ -37,36 +39,36 @@
*/

double largeIntegers( const double a, const double b ) {
int64_t ac;
int64_t bc;
int64_t k;
int64_t t;
double ac;
double bc;
double k;
double t;

ac = (int64_t)a;
bc = (int64_t)b;
k = 1;
ac = a;
bc = b;
k = 1.0;
// Simple cases:
if ( ac == 0 ) {
if ( a == 0.0 ) {
return b;
}
if ( bc == 0 ) {
if ( b == 0.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 ( ac%2 == 0 && bc%2 == 0 ) {
ac /= 2; // right shift
bc /= 2; // right shift
k *= 2; // left shift
while ( fmod( ac, 2.0 ) == 0.0 && fmod( bc, 2.0 ) == 0.0 ) {
ac /= 2.0; // right shift
bc /= 2.0; // right shift
k *= 2.0; // left shift
}
// Reduce `a` to an odd number...
while ( ac%2 == 0 ) {
ac /= 2; // right shift
while ( fmod( ac, 2.0 ) == 0.0 ) {
ac /= 2.0; // right shift
}
// Henceforth, `a` is always odd...
while ( bc ) {
// Remove all factors of 2 in `b`, as they are not common...
while ( bc%2 == 0 ) {
bc /= 2; // right shift
while ( fmod( bc, 2.0 ) == 0.0 ) {
bc /= 2.0; // 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 ) {
Expand Down Expand Up @@ -147,7 +149,6 @@ double bitwise( const double a, const double b ) {
* double out = stdlib_base_gcd( 48.0, 18.0 );
* // returns 6.0
*/

double stdlib_base_gcd( const double a, const double b ) {
double ac;
double bc;
Expand All @@ -174,7 +175,7 @@ double stdlib_base_gcd( const double a, const double b ) {
if ( bc < 0 ) {
bc = -bc;
}
if ( ac <= STDLIB_CONSTANT_INT32_MAX && bc <= STDLIB_CONSTANT_INT32_MAX ) {
if ( ac <= STDLIB_CONSTANT_INT64_MAX && bc <= STDLIB_CONSTANT_INT64_MAX ) {
return bitwise( ac, bc );
}
return largeIntegers( ac, bc );
Expand Down

0 comments on commit b669a75

Please sign in to comment.