README.md (3146B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # Convert 22 23 > Convert an array to an array of a different data type. 24 25 <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> 26 27 <section class="intro"> 28 29 </section> 30 31 <!-- /.intro --> 32 33 <!-- Package usage documentation. --> 34 35 <section class="usage"> 36 37 ## Usage 38 39 ```javascript 40 var convertArray = require( '@stdlib/array/convert' ); 41 ``` 42 43 #### convertArray( arr, dtype ) 44 45 Converts an `array` to an array of a different data type. 46 47 ```javascript 48 var arr = [ 1.0, 2.0, 3.0 ]; 49 var out = convertArray( arr, 'float32' ); 50 // returns <Float32Array>[ 1.0, 2.0, 3.0 ] 51 ``` 52 53 The function supports the following data types: 54 55 - `float32`: single-precision floating-point numbers. 56 - `float64`: double-precision floating-point numbers. 57 - `generic`: values of any type. 58 - `int16`: signed 16-bit integers. 59 - `int32`: signed 32-bit integers. 60 - `int8`: signed 8-bit integers. 61 - `uint16`: unsigned 16-bit integers. 62 - `uint32`: unsigned 32-bit integers. 63 - `uint8`: unsigned 8-bit integers. 64 - `uint8c`: unsigned clamped 8-bit integers. 65 66 </section> 67 68 <!-- /.usage --> 69 70 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 71 72 <section class="notes"> 73 74 </section> 75 76 <!-- /.notes --> 77 78 <!-- Package usage examples. --> 79 80 <section class="examples"> 81 82 ## Examples 83 84 <!-- eslint no-undef: "error" --> 85 86 ```javascript 87 var dtypes = require( '@stdlib/array/dtypes' ); 88 var randu = require( '@stdlib/random/base/randu' ); 89 var floor = require( '@stdlib/math/base/special/floor' ); 90 var convertArray = require( '@stdlib/array/convert' ); 91 92 // Create a generic array: 93 var arr = []; 94 var i; 95 for ( i = 0; i < 5; i++ ) { 96 arr.push( floor( randu()*1.0e25 ) - 5.0e24 ); 97 } 98 99 // Get a list of array data types: 100 var DTYPES = dtypes(); 101 102 // Convert the generic array to each array data type: 103 var out; 104 for ( i = 0; i < DTYPES.length; i++ ) { 105 out = convertArray( arr, DTYPES[ i ] ); 106 console.log( out ); 107 } 108 ``` 109 110 </section> 111 112 <!-- /.examples --> 113 114 <!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 115 116 <section class="references"> 117 118 </section> 119 120 <!-- /.references --> 121 122 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 123 124 <section class="links"> 125 126 </section> 127 128 <!-- /.links -->