README.md (2079B)
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 # toInt32 22 23 > Convert a [double-precision floating-point number][ieee754] to a signed 32-bit integer. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var float64ToInt32 = require( '@stdlib/number/float64/base/to-int32' ); 31 ``` 32 33 #### float64ToInt32( x ) 34 35 Converts a [double-precision floating-point number][ieee754] to a signed 32-bit integer. 36 37 ```javascript 38 var y = float64ToInt32( 4294967295.0 ); 39 // returns -1 40 41 y = float64ToInt32( 3.14 ); 42 // returns 3 43 44 y = float64ToInt32( -3.14 ); 45 // returns -3 46 47 y = float64ToInt32( NaN ); 48 // returns 0 49 50 y = float64ToInt32( Infinity ); 51 // returns 0 52 53 y = float64ToInt32( -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 float64ToInt32 = require( '@stdlib/number/float64/base/to-int32' ); 72 73 var int32; 74 var f64; 75 var i; 76 77 for ( i = 0; i < 500; i++ ) { 78 // Generate a random double-precision floating-point integer: 79 f64 = round( randu()*MAX_INT ); 80 81 // Convert the double-precision floating-point integer to a signed integer: 82 int32 = float64ToInt32( f64 ); 83 84 console.log( 'float64: %d => int32: %d', f64, int32 ); 85 } 86 ``` 87 88 </section> 89 90 <!-- /.examples --> 91 92 <section class="links"> 93 94 [ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985 95 96 </section> 97 98 <!-- /.links -->