main.js (2003B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 'use strict'; 20 21 // MAIN // 22 23 /** 24 * Converts a single-precision floating-point number to a signed 32-bit integer. 25 * 26 * @param {number} x - single-precision floating-point number 27 * @returns {integer32} signed 32-bit integer 28 * 29 * @example 30 * var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' ); 31 * var y = float32ToInt32( float64ToFloat32( 4294967295.0 ) ); 32 * // returns 0 33 * 34 * @example 35 * var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' ); 36 * var y = float32ToInt32( float64ToFloat32( 3.14 ) ); 37 * // returns 3 38 * 39 * @example 40 * var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' ); 41 * var y = float32ToInt32( float64ToFloat32( -3.14 ) ); 42 * // returns -3 43 * 44 * @example 45 * var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' ); 46 * var y = float32ToInt32( float64ToFloat32( NaN ) ); 47 * // returns 0 48 * 49 * @example 50 * var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' ); 51 * var y = float32ToInt32( float64ToFloat32( Infinity ) ); 52 * // returns 0 53 * 54 * @example 55 * var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' ); 56 * var y = float32ToInt32( float64ToFloat32( -Infinity ) ); 57 * // returns 0 58 */ 59 function float32ToInt32( x ) { 60 // NOTE: we could also use typed-arrays to achieve the same end. 61 return x|0; // asm type annotation 62 } 63 64 65 // EXPORTS // 66 67 module.exports = float32ToInt32;