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

docs(math/base/ops): improve math/base/ops README.md examples #1722

Merged
27 changes: 27 additions & 0 deletions lib/node_modules/@stdlib/math/base/ops/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,37 @@ The namespace contains the following functions:
<!-- eslint no-undef: "error" -->

```javascript
var Complex128 = require( '@stdlib/complex/float64' );
var objectKeys = require( '@stdlib/utils/keys' );
var ns = require( '@stdlib/math/base/ops' );

console.log( objectKeys( ns ) );
Planeshifter marked this conversation as resolved.
Show resolved Hide resolved

// Operations for floating point numbers (double-precision)
Planeshifter marked this conversation as resolved.
Show resolved Hide resolved
console.log( ns.add(1.25, 0.45) ); // => 1.7
console.log( ns.sub(1.25, 0.45) ); // => 0.8
Planeshifter marked this conversation as resolved.
Show resolved Hide resolved

// (single-precision)
Planeshifter marked this conversation as resolved.
Show resolved Hide resolved
console.log( ns.mulf(1.3, 1.2) ); // => ~ 1.56
console.log( ns.divf(1.2, 0.4) ); // => 3
Planeshifter marked this conversation as resolved.
Show resolved Hide resolved

// Operations for complex numbers
Planeshifter marked this conversation as resolved.
Show resolved Hide resolved
var z1 = new Complex128( 5.0, 3.0 );
var z2 = new Complex128( -2.0, 1.0 );

Planeshifter marked this conversation as resolved.
Show resolved Hide resolved
console.log( ns.cmul( z1, z2 ) ); // => Complex128 { re: -13.0, im: -1.0 }
Planeshifter marked this conversation as resolved.
Show resolved Hide resolved

// Operations for Signed 32-bit integers
Planeshifter marked this conversation as resolved.
Show resolved Hide resolved
// 2^30 * -5 = -5368709120 => 32-bit integer overflow
console.log( ns.imul( 1073741824|0, -5|0 ) ); // => -1073741824
Planeshifter marked this conversation as resolved.
Show resolved Hide resolved

// Operations for Unsigned 32-bit integers
Planeshifter marked this conversation as resolved.
Show resolved Hide resolved
// 2^31 * 5 = 10737418240 => 32-bit integer overflow
console.log( ns.umul( 2147483648>>>0, 5>>>0 ) ); // => 2147483648
Planeshifter marked this conversation as resolved.
Show resolved Hide resolved

// Operations for Double word product:
Planeshifter marked this conversation as resolved.
Show resolved Hide resolved
// -(2^31) * 2^30 = -2305843009213694000 => 32-bit integer overflow
console.log( ns.imuldw( 0x80000000|0, 0x40000000|0 ) ); // => returns [ -536870912, 0 ]
Planeshifter marked this conversation as resolved.
Show resolved Hide resolved
```

</section>
Expand Down
27 changes: 27 additions & 0 deletions lib/node_modules/@stdlib/math/base/ops/examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,34 @@

'use strict';

var Complex128 = require( '@stdlib/complex/float64' );
var objectKeys = require( '@stdlib/utils/keys' );
var ns = require( './../lib' );

console.log( objectKeys( ns ) );
Planeshifter marked this conversation as resolved.
Show resolved Hide resolved

// Operations for floating point numbers (double-precision)
Planeshifter marked this conversation as resolved.
Show resolved Hide resolved
console.log( ns.add(1.25, 0.45) ); // => 1.7
console.log( ns.sub(1.25, 0.45) ); // => 0.8
Planeshifter marked this conversation as resolved.
Show resolved Hide resolved

// (single-precision)
Planeshifter marked this conversation as resolved.
Show resolved Hide resolved
console.log( ns.mulf(1.3, 1.2) ); // => ~ 1.56
console.log( ns.divf(1.2, 0.4) ); // => 3
Planeshifter marked this conversation as resolved.
Show resolved Hide resolved

// Operations for complex numbers
Planeshifter marked this conversation as resolved.
Show resolved Hide resolved
var z1 = new Complex128( 5.0, 3.0 );
var z2 = new Complex128( -2.0, 1.0 );

console.log( ns.cmul( z1, z2 ) ); // => Complex128 { re: -13.0, im: -1.0 }
Planeshifter marked this conversation as resolved.
Show resolved Hide resolved

// Operations for Signed 32-bit integers
Planeshifter marked this conversation as resolved.
Show resolved Hide resolved
// 2^30 * -5 = -5368709120 => 32-bit integer overflow
console.log( ns.imul( 1073741824|0, -5|0 ) ); // => -1073741824
Planeshifter marked this conversation as resolved.
Show resolved Hide resolved

// Operations for Unsigned 32-bit integers
Planeshifter marked this conversation as resolved.
Show resolved Hide resolved
// 2^31 * 5 = 10737418240 => 32-bit integer overflow
console.log( ns.umul( 2147483648>>>0, 5>>>0 ) ); // => 2147483648
Planeshifter marked this conversation as resolved.
Show resolved Hide resolved

// Operations for Double word product:
Planeshifter marked this conversation as resolved.
Show resolved Hide resolved
// -(2^31) * 2^30 = -2305843009213694000 => 32-bit integer overflow
console.log( ns.imuldw( 0x80000000|0, 0x40000000|0 ) ); // => returns [ -536870912, 0 ]
Planeshifter marked this conversation as resolved.
Show resolved Hide resolved
Loading