README.md (2073B)
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 # toFloat32 22 23 > Convert a [double-precision floating-point number][ieee754] to the nearest [single-precision floating-point number][ieee754]. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' ); 31 ``` 32 33 #### float64ToFloat32( x ) 34 35 Converts a [double-precision floating-point number][ieee754] to the nearest [single-precision floating-point number][ieee754]. 36 37 ```javascript 38 var y = float64ToFloat32( 1.337 ); 39 // returns 1.3370000123977661 40 ``` 41 42 </section> 43 44 <!-- /.usage --> 45 46 <section class="notes"> 47 48 ## Notes 49 50 - This function may be used as a polyfill for the ES2015 built-in [`Math.fround`][math-fround]. 51 52 </section> 53 54 <!-- /.notes --> 55 56 <section class="examples"> 57 58 ## Examples 59 60 <!-- eslint no-undef: "error" --> 61 62 ```javascript 63 var randu = require( '@stdlib/random/base/randu' ); 64 var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' ); 65 66 var f64; 67 var f32; 68 var i; 69 70 // Convert random double-precision floating-point numbers to the nearest single-precision floating-point number... 71 for ( i = 0; i < 1000; i++ ) { 72 f64 = randu() * 100.0; 73 f32 = float64ToFloat32( f64 ); 74 console.log( 'float64: %d => float32: %d', f64, f32 ); 75 } 76 ``` 77 78 </section> 79 80 <!-- /.examples --> 81 82 <section class="links"> 83 84 [ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985 85 86 [math-fround]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/fround 87 88 </section> 89 90 <!-- /.links -->