README.md (2949B)
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 # Bytes per Element 22 23 > Return the number of bytes per element provided an underlying [array data type][@stdlib/ndarray/dtypes]. 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 bytesPerElement = require( '@stdlib/ndarray/base/bytes-per-element' ); 41 ``` 42 43 #### bytesPerElement( dtype ) 44 45 Returns the number of bytes per element provided an underlying [array data type][@stdlib/ndarray/dtypes]. 46 47 ```javascript 48 var nbytes = bytesPerElement( 'float64' ); 49 // returns 8 50 51 nbytes = bytesPerElement( 'generic' ); 52 // returns null 53 ``` 54 55 If provided an unknown or unsupported data type, the function returns `null`. 56 57 ```javascript 58 var nbytes = bytesPerElement( 'foobar' ); 59 // returns null 60 ``` 61 62 </section> 63 64 <!-- /.usage --> 65 66 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 67 68 <section class="notes"> 69 70 </section> 71 72 <!-- /.notes --> 73 74 <!-- Package usage examples. --> 75 76 <section class="examples"> 77 78 ## Examples 79 80 <!-- eslint no-undef: "error" --> 81 82 ```javascript 83 var bytesPerElement = require( '@stdlib/ndarray/base/bytes-per-element' ); 84 85 var dtypes; 86 var nbytes; 87 var i; 88 89 dtypes = [ 90 'float64', 91 'float32', 92 'int8', 93 'uint8', 94 'uint8c', 95 'int16', 96 'uint16', 97 'int32', 98 'uint32', 99 'binary', 100 'generic', 101 'foobar' 102 ]; 103 104 for ( i = 0; i < dtypes.length; i++ ) { 105 nbytes = bytesPerElement( dtypes[ i ] ); 106 nbytes = ( nbytes ) ? nbytes+' bytes' : 'null'; 107 console.log( '%s => %s', dtypes[ i ], nbytes ); 108 } 109 ``` 110 111 </section> 112 113 <!-- /.examples --> 114 115 <!-- 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. --> 116 117 <section class="references"> 118 119 </section> 120 121 <!-- /.references --> 122 123 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 124 125 <section class="links"> 126 127 [@stdlib/ndarray/dtypes]: https://www.npmjs.com/package/@stdlib/ndarray/tree/main/dtypes 128 129 </section> 130 131 <!-- /.links -->