README.md (3578B)
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 # Promotion Rules 22 23 > Return the array [data type][@stdlib/array/dtypes] with the smallest size and closest "kind" to which array [data types][@stdlib/array/dtypes] can be **safely** cast. 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 promotionRules = require( '@stdlib/array/promotion-rules' ); 41 ``` 42 43 #### promotionRules( \[dtype1, dtype2] ) 44 45 If provided [data types][@stdlib/array/dtypes], returns the array [data type][@stdlib/array/dtypes] with the smallest size and closest "kind" to which array [data types][@stdlib/array/dtypes] can be **safely** cast. 46 47 ```javascript 48 var out = promotionRules( 'float32', 'uint32' ); 49 // returns 'float64' 50 ``` 51 52 If not provided [data types][@stdlib/array/dtypes], the function returns a promotion table. 53 54 ```javascript 55 var out = promotionRules(); 56 // returns {...} 57 58 var f32 = out[ 'float32' ]; 59 // returns {...} 60 61 var rule = f32[ 'uint32' ]; 62 // returns 'float64' 63 ``` 64 65 If provided an unrecognized or unsupported `dtype`, the function returns `null`. 66 67 ```javascript 68 var out = promotionRules( 'foo', 'generic' ); 69 // returns null 70 ``` 71 72 </section> 73 74 <!-- /.usage --> 75 76 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 77 78 <section class="notes"> 79 80 ## Notes 81 82 - If a [data type][@stdlib/array/dtypes] to which [data types][@stdlib/array/dtypes] can be safely cast does **not** exist (or is not supported), the function returns `-1`. 83 84 </section> 85 86 <!-- /.notes --> 87 88 <!-- Package usage examples. --> 89 90 <section class="examples"> 91 92 ## Examples 93 94 <!-- eslint no-undef: "error" --> 95 96 ```javascript 97 var dtypes = require( '@stdlib/array/dtypes' ); 98 var promotionRules = require( '@stdlib/array/promotion-rules' ); 99 100 var DTYPES; 101 var dt1; 102 var dt2; 103 var dt; 104 var i; 105 var j; 106 107 // Get the list of supported array data types: 108 DTYPES = dtypes(); 109 110 // Print the promotion rule for each pair of array data types... 111 for ( i = 0; i < DTYPES.length; i++ ) { 112 dt1 = DTYPES[ i ]; 113 for ( j = 0; j < DTYPES.length; j++ ) { 114 dt2 = DTYPES[ j ]; 115 dt = promotionRules( dt1, dt2 ); 116 console.log( '(%s, %s) => %s', dt1, dt2, dt ); 117 } 118 } 119 ``` 120 121 </section> 122 123 <!-- /.examples --> 124 125 <!-- 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. --> 126 127 <section class="references"> 128 129 </section> 130 131 <!-- /.references --> 132 133 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 134 135 <section class="links"> 136 137 [@stdlib/array/dtypes]: https://www.npmjs.com/package/@stdlib/array/tree/main/dtypes 138 139 </section> 140 141 <!-- /.links -->