README.md (2160B)
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 # toUint32 22 23 > Convert a [double-precision floating-point number][ieee754] to an unsigned 32-bit integer. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var float64ToUint32 = require( '@stdlib/number/float64/base/to-uint32' ); 31 ``` 32 33 #### float64ToUint32( x ) 34 35 Converts a [double-precision floating-point number][ieee754] to an unsigned 32-bit integer. 36 37 ```javascript 38 var y = float64ToUint32( 4294967297.0 ); 39 // returns 1 40 41 y = float64ToUint32( 3.14 ); 42 // returns 3 43 44 y = float64ToUint32( -3.14 ); 45 // returns 4294967293 46 47 y = float64ToUint32( NaN ); 48 // returns 0 49 50 y = float64ToUint32( Infinity ); 51 // returns 0 52 53 y = float64ToUint32( -Infinity ); 54 // returns 0 55 ``` 56 57 </section> 58 59 <!-- /.usage --> 60 61 <section class="examples"> 62 63 ## Examples 64 65 <!-- eslint no-undef: "error" --> 66 67 ```javascript 68 var randu = require( '@stdlib/random/base/randu' ); 69 var round = require( '@stdlib/math/base/special/round' ); 70 var MAX_INT = require( '@stdlib/constants/uint32/max' ); 71 var float64ToUint32 = require( '@stdlib/number/float64/base/to-uint32' ); 72 73 var uint32; 74 var half; 75 var f64; 76 var i; 77 78 half = ( MAX_INT-1 ) / 2; 79 for ( i = 0; i < 500; i++ ) { 80 // Generate a random double-precision floating-point integer: 81 f64 = round( randu()*MAX_INT ) - half; 82 83 // Convert the double-precision floating-point value to an unsigned 32-bit integer: 84 uint32 = float64ToUint32( f64 ); 85 86 console.log( 'float64: %d => uint32: %d', f64, uint32 ); 87 } 88 ``` 89 90 </section> 91 92 <!-- /.examples --> 93 94 <section class="links"> 95 96 [ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985 97 98 </section> 99 100 <!-- /.links -->