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