README.md (4427B)
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 # toWord 22 23 > Return an unsigned 32-bit integer corresponding to the [IEEE 754][ieee754] binary representation of a [single-precision floating-point number][ieee754]. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var toWordf = require( '@stdlib/number/float32/base/to-word' ); 31 ``` 32 33 #### toWordf( x ) 34 35 Returns an unsigned 32-bit `integer` corresponding to the [IEEE 754][ieee754] binary representation of a [single-precision floating-point number][ieee754]. 36 37 ```javascript 38 var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' ); 39 40 var f32 = float64ToFloat32( 1.337 ); 41 // returns 1.3370000123977661 42 43 var w = toWordf( f32 ); // => 0 01111111 01010110010001011010001 44 // returns 1068180177 45 ``` 46 47 </section> 48 49 <!-- /.usage --> 50 51 <section class="notes"> 52 53 ## Notes 54 55 - The equivalent of this function in C/C++, 56 57 ```c 58 unsigned int toWordf(float x) { 59 return *(unsigned int*)&x; 60 } 61 ``` 62 63 </section> 64 65 <!-- /.notes --> 66 67 <section class="examples"> 68 69 ## Examples 70 71 <!-- eslint no-undef: "error" --> 72 73 ```javascript 74 var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' ); 75 var randu = require( '@stdlib/random/base/randu' ); 76 var toWordf = require( '@stdlib/number/float32/base/to-word' ); 77 78 var word; 79 var f64; 80 var f32; 81 var i; 82 83 // Convert single-precision floating-point numbers to integers representing the binary literal... 84 for ( i = 0; i < 1000; i++ ) { 85 f64 = (randu()*100.0) - 50.0; 86 f32 = float64ToFloat32( f64 ); 87 word = toWordf( f32 ); 88 console.log( 'float64: %d => float32: %d => word: %d', f64, f32, word ); 89 } 90 ``` 91 92 </section> 93 94 <!-- /.examples --> 95 96 <!-- C interface documentation. --> 97 98 * * * 99 100 <section class="c"> 101 102 ## C APIs 103 104 <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> 105 106 <section class="intro"> 107 108 </section> 109 110 <!-- /.intro --> 111 112 <!-- C usage documentation. --> 113 114 <section class="usage"> 115 116 ### Usage 117 118 ```c 119 #include "stdlib/number/float32/base/to_word.h" 120 ``` 121 122 #### stdlib_base_float32_to_word( x, \*word ) 123 124 Converts a [single-precision floating-point number][ieee754] to an unsigned 32-bit integer corresponding to the number's [IEEE 754][ieee754] binary representation. 125 126 ```c 127 #include <stdint.h> 128 129 uint32_t word; 130 stdlib_base_float32_to_word( 3.14f, &word ); 131 ``` 132 133 The function accepts the following arguments: 134 135 - **x**: `[in] float` input value. 136 - **word**: `[out] uint32_t*` destination. 137 138 ```c 139 void stdlib_base_float32_to_word( const float x, uint32_t *word ); 140 ``` 141 142 #### stdlib_base_float32_word_t 143 144 An opaque type definition for a union for converting between a [single-precision floating-point number][ieee754] and an unsigned 32-bit integer. 145 146 ```c 147 #include <stdint.h> 148 149 stdlib_base_float32_word_t w; 150 151 // Assign a single-precision floating-point number: 152 w.value = 3.14f; 153 154 // Retrieve the word: 155 uint32_t word = w.word; 156 ``` 157 158 The union has the following members: 159 160 - **value**: `float` [single-precision floating-point number][ieee754]. 161 - **word**: `uint32_t` word. 162 163 </section> 164 165 <!-- /.usage --> 166 167 <!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 168 169 <section class="notes"> 170 171 </section> 172 173 <!-- /.notes --> 174 175 <!-- C API usage examples. --> 176 177 <section class="examples"> 178 179 ### Examples 180 181 ```c 182 #include "stdlib/number/float32/base/to_word.h" 183 #include <stdint.h> 184 #include <stdio.h> 185 186 int main() { 187 float x[] = { 3.14f, -3.14f, 0.0f, 0.0f/0.0f }; 188 189 uint32_t word; 190 int i; 191 for ( i = 0; i < 4; i++ ) { 192 stdlib_base_float32_to_word( x[ i ], &word ); 193 printf( "%f => word: %u\n", x[ i ], word ); 194 } 195 } 196 ``` 197 198 </section> 199 200 <!-- /.examples --> 201 202 </section> 203 204 <!-- /.c --> 205 206 <section class="links"> 207 208 [ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985 209 210 </section> 211 212 <!-- /.links -->