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

Enhanced README Examples for number/int32 Namespace #2703

Closed
wants to merge 2 commits into from
Closed
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
148 changes: 144 additions & 4 deletions lib/node_modules/@stdlib/number/int32/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,157 @@ The namespace contains the following:

## Examples

<!-- TODO: better examples -->

<!-- eslint no-undef: "error" -->

```javascript
var objectKeys = require( '@stdlib/utils/keys' );
var ns = require( '@stdlib/number/int32' );

console.log( objectKeys( ns ) );
```

### Example: Base Utilities

The `base` namespace contains basic utilities for working with signed 32-bit integers.

```javascript
var base = require( '@stdlib/number/int32/base' );

// Convert a number to a signed 32-bit integer:
var int32 = base.toInt32( 3.14 );
console.log( int32 );
// => 3

// Convert a number to a signed 32-bit integer:
int32 = base.toInt32( -3.14 );
console.log( int32 );
// => -3

// Bitwise AND of two signed 32-bit integers:
var and = base.and( -1, 3 );
console.log( and );
// => 3

// Bitwise OR of two signed 32-bit integers:
var or = base.or( -1, 3 );
console.log( or );
// => -1

// Bitwise XOR of two signed 32-bit integers:
var xor = base.xor( -1, 3 );
console.log( xor );
// => -4

// Bitwise NOT of a signed 32-bit integer:
var not = base.not( 3 );
console.log( not );
// => -4

// Left shift of a signed 32-bit integer:
var shl = base.shl( 3, 2 );
console.log( shl );
// => 12

// Right shift of a signed 32-bit integer:
var shr = base.shr( 12, 2 );
console.log( shr );
// => 3

// Arithmetic right shift of a signed 32-bit integer:
var ashr = base.ashr( -16, 2 );
console.log( ashr );
// => -4
```

### Example: Checking and Manipulating Integers

The `base` namespace also provides functions for checking and manipulating signed 32-bit integers.

```javascript
// Check if a value is a signed 32-bit integer:
var bool = base.isInt32( 3 );
console.log( bool );
// => true

bool = base.isInt32( 3.14 );
console.log( bool );
// => false

bool = base.isInt32( -2147483649 );
console.log( bool );
// => false

bool = base.isInt32( 2147483647 );
console.log( bool );
// => true

// Check if a value is a safe integer:
bool = base.isSafeInteger( 3 );
console.log( bool );
// => true

bool = base.isSafeInteger( Math.pow( 2, 53 ) );
console.log( bool );
// => false

// Convert a value to a safe integer:
var safeInt = base.toSafeInteger( 3.14 );
console.log( safeInt );
// => 3

safeInt = base.toSafeInteger( Math.pow( 2, 53 ) );
console.log( safeInt );
// => 9007199254740991
```

### Example: Math Operations

The `base` namespace provides various mathematical operations for signed 32-bit integers.

```javascript
// Add two signed 32-bit integers:
var sum = base.add( 2147483647, 1 );
console.log( sum );
// => -2147483648 (overflow)

// Subtract two signed 32-bit integers:
var diff = base.sub( -2147483648, 1 );
console.log( diff );
// => 2147483647 (underflow)

// Multiply two signed 32-bit integers:
var prod = base.mul( 10000, 10000 );
console.log( prod );
// => -727379968 (overflow)

// Divide two signed 32-bit integers:
var quot = base.div( 10, 3 );
console.log( quot );
// => 3

// Modulo of two signed 32-bit integers:
var mod = base.mod( 10, 3 );
console.log( mod );
// => 1

// Negate a signed 32-bit integer:
var neg = base.neg( 123 );
console.log( neg );
// => -123
```

### Example: Using Other Packages

To demonstrate how utilities from other packages can be used in conjunction with `number/int32`, consider the following:

```javascript
var round = require( '@stdlib/math/base/special/round' );
var int32 = require( '@stdlib/number/int32/base' );

// Round a number and convert to signed 32-bit integer:
var roundedInt32 = int32.toInt32( round( 3.75 ) );
console.log( roundedInt32 );
// => 4
```

</section>

<!-- /.examples -->
Expand Down
118 changes: 118 additions & 0 deletions lib/node_modules/@stdlib/number/int32/examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,121 @@ var objectKeys = require( '@stdlib/utils/keys' );
var ns = require( './../lib' );

console.log( objectKeys( ns ) );

// Example: Base Utilities
var base = ns.base;

// Convert a number to a signed 32-bit integer:
var int32 = base.toInt32( 3.14 );
console.log( int32 );
// => 3

int32 = base.toInt32( -3.14 );
console.log( int32 );
// => -3

// Bitwise AND of two signed 32-bit integers:
var and = base.and( -1, 3 );
console.log( and );
// => 3

// Bitwise OR of two signed 32-bit integers:
var or = base.or( -1, 3 );
console.log( or );
// => -1

// Bitwise XOR of two signed 32-bit integers:
var xor = base.xor( -1, 3 );
console.log( xor );
// => -4

// Bitwise NOT of a signed 32-bit integer:
var not = base.not( 3 );
console.log( not );
// => -4

// Left shift of a signed 32-bit integer:
var shl = base.shl( 3, 2 );
console.log( shl );
// => 12

// Right shift of a signed 32-bit integer:
var shr = base.shr( 12, 2 );
console.log( shr );
// => 3

// Arithmetic right shift of a signed 32-bit integer:
var ashr = base.ashr( -16, 2 );
console.log( ashr );
// => -4

// Check if a value is a signed 32-bit integer:
var bool = base.isInt32( 3 );
console.log( bool );
// => true

bool = base.isInt32( 3.14 );
console.log( bool );
// => false

bool = base.isInt32( -2147483649 );
console.log( bool );
// => false

bool = base.isInt32( 2147483647 );
console.log( bool );
// => true

// Check if a value is a safe integer:
bool = base.isSafeInteger( 3 );
console.log( bool );
// => true

bool = base.isSafeInteger( Math.pow( 2, 53 ) );
console.log( bool );
// => false

// Convert a value to a safe integer:
var safeInt = base.toSafeInteger( 3.14 );
console.log( safeInt );
// => 3

safeInt = base.toSafeInteger( Math.pow( 2, 53 ) );
console.log( safeInt );
// => 9007199254740991

// Add two signed 32-bit integers:
var sum = base.add( 2147483647, 1 );
console.log( sum );
// => -2147483648 (overflow)

// Subtract two signed 32-bit integers:
var diff = base.sub( -2147483648, 1 );
console.log( diff );
// => 2147483647 (underflow)

// Multiply two signed 32-bit integers:
var prod = base.mul( 10000, 10000 );
console.log( prod );
// => -727379968 (overflow)

// Divide two signed 32-bit integers:
var quot = base.div( 10, 3 );
console.log( quot );
// => 3

// Modulo of two signed 32-bit integers:
var mod = base.mod( 10, 3 );
console.log( mod );
// => 1

// Negate a signed 32-bit integer:
var neg = base.neg( 123 );
console.log( neg );
// => -123

// Using other packages with int32
var round = require( '@stdlib/math/base/special/round' );
var roundedInt32 = base.toInt32( round( 3.75 ) );
console.log( roundedInt32 );
// => 4
Loading