|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2025 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 | +# frexpf |
| 22 | + |
| 23 | +> Split a [single-precision floating-point number][ieee754] into a normalized fraction and an integer power of two. |
| 24 | +
|
| 25 | +<section class="usage"> |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +```javascript |
| 30 | +var frexpf = require( '@stdlib/math/base/special/frexpf' ); |
| 31 | +``` |
| 32 | + |
| 33 | +#### frexpf( x ) |
| 34 | + |
| 35 | +Splits a [single-precision floating-point number][ieee754] into a normalized fraction and an integer power of two. |
| 36 | + |
| 37 | +```javascript |
| 38 | +var out = frexpf( 4.0 ); |
| 39 | +// returns [ 0.5, 3 ] |
| 40 | +``` |
| 41 | + |
| 42 | +By default, the function returns the normalized fraction and the exponent as a two-element `array`. The normalized fraction and exponent satisfy the relation `x = frac * 2^exp`. |
| 43 | + |
| 44 | +```javascript |
| 45 | +var pow = require( '@stdlib/math/base/special/pow' ); |
| 46 | + |
| 47 | +var x = 4.0; |
| 48 | +var out = frexpf( x ); |
| 49 | +// returns [ 0.5, 3 ] |
| 50 | + |
| 51 | +var frac = out[ 0 ]; |
| 52 | +var exp = out[ 1 ]; |
| 53 | + |
| 54 | +var bool = ( x === frac * pow(2.0, exp) ); |
| 55 | +// returns true |
| 56 | +``` |
| 57 | + |
| 58 | +If provided positive or negative zero, `NaN`, or positive or negative `infinity`, the function returns a two-element `array` containing the input value and an exponent equal to `0`. |
| 59 | + |
| 60 | +```javascript |
| 61 | +var out = frexpf( 0.0 ); |
| 62 | +// returns [ 0.0, 0 ] |
| 63 | + |
| 64 | +out = frexpf( -0.0 ); |
| 65 | +// returns [ -0.0, 0 ] |
| 66 | + |
| 67 | +out = frexpf( NaN ); |
| 68 | +// returns [ NaN, 0 ] |
| 69 | + |
| 70 | +out = frexpf( Infinity ); |
| 71 | +// returns [ Infinity, 0 ] |
| 72 | + |
| 73 | +out = frexpf( -Infinity ); |
| 74 | +// returns [ -Infinity, 0 ] |
| 75 | +``` |
| 76 | + |
| 77 | +For all other numeric input values, the [absolute value][@stdlib/math/base/special/absf] of the normalized fraction resides on the interval `[0.5,1)`. |
| 78 | + |
| 79 | +#### frexpf.assign( x, out, stride, offset ) |
| 80 | + |
| 81 | +Splits a [single-precision floating-point number][ieee754] into a normalized fraction and an integer power of two and assigns results to a provided output array. |
| 82 | + |
| 83 | +```javascript |
| 84 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 85 | + |
| 86 | +var out = new Float32Array( 2 ); |
| 87 | + |
| 88 | +var y = frexpf.assign( 4.0, out, 1, 0 ); |
| 89 | +// returns <Float32Array>[ 0.5, 3 ] |
| 90 | + |
| 91 | +var bool = ( y === out ); |
| 92 | +// returns true |
| 93 | +``` |
| 94 | + |
| 95 | +</section> |
| 96 | + |
| 97 | +<!-- /.usage --> |
| 98 | + |
| 99 | +<section class="notes"> |
| 100 | + |
| 101 | +## Notes |
| 102 | + |
| 103 | +- Care should be taken when reconstituting a [single-precision floating-point number][ieee754] from a normalized fraction and an exponent. For example, |
| 104 | + |
| 105 | + ```javascript |
| 106 | + var pow = require( '@stdlib/math/base/special/pow' ); |
| 107 | + var f32 = require( '@stdlib/number/float64/base/to-float32' ); |
| 108 | + |
| 109 | + var x = 1.7014118346046923e+38; // x ~ 2^127 |
| 110 | + |
| 111 | + var out = frexpf( x ); |
| 112 | + // returns [ 0.5, 128 ] |
| 113 | + |
| 114 | + // Naive reconstitution: |
| 115 | + var y = f32( out[ 0 ] * f32( pow( 2.0, out[ 1 ] ) ) ); |
| 116 | + // returns Infinity |
| 117 | + |
| 118 | + // Account for 2^128 evaluating as infinity by recognizing 2^128 = 2^1 * 2^127: |
| 119 | + y = f32( out[ 0 ] * f32( pow( 2.0, out[1]-127 ) ) * f32( pow( 2.0, 127 ) ) ); |
| 120 | + // returns 1.7014118346046923e+38 |
| 121 | + ``` |
| 122 | + |
| 123 | +</section> |
| 124 | + |
| 125 | +<!-- /.notes --> |
| 126 | + |
| 127 | +<section class="examples"> |
| 128 | + |
| 129 | +## Examples |
| 130 | + |
| 131 | +<!-- eslint no-undef: "error" --> |
| 132 | + |
| 133 | +```javascript |
| 134 | +var randu = require( '@stdlib/random/base/randu' ); |
| 135 | +var roundf = require( '@stdlib/math/base/special/roundf' ); |
| 136 | +var pow = require( '@stdlib/math/base/special/pow' ); |
| 137 | +var f32 = require( '@stdlib/number/float64/base/to-float32' ); |
| 138 | +var BIAS = require( '@stdlib/constants/float32/exponent-bias' ); |
| 139 | +var frexpf = require( '@stdlib/math/base/special/frexpf' ); |
| 140 | +
|
| 141 | +var sign; |
| 142 | +var frac; |
| 143 | +var exp; |
| 144 | +var x; |
| 145 | +var f; |
| 146 | +var v; |
| 147 | +var i; |
| 148 | +
|
| 149 | +// Generate random numbers and break each into a normalized fraction and an integer power of two... |
| 150 | +for ( i = 0; i < 100; i++ ) { |
| 151 | + if ( randu() < 0.5 ) { |
| 152 | + sign = f32( -1.0 ); |
| 153 | + } else { |
| 154 | + sign = f32( 1.0 ); |
| 155 | + } |
| 156 | + frac = f32( randu()*10.0 ); |
| 157 | + exp = roundf( randu()*76.0 ) - 38; |
| 158 | + x = f32( sign * frac * f32( pow( 10.0, exp ) ) ); |
| 159 | + f = frexpf( x ); |
| 160 | + if ( f[ 1 ] > BIAS ) { |
| 161 | + v = f32( f[ 0 ] * f32( pow(2.0, f[1]-BIAS) ) * f32( pow(2.0, BIAS) ) ); |
| 162 | + } else { |
| 163 | + v = f32( f[ 0 ] * f32( pow( 2.0, f[ 1 ] ) ) ); |
| 164 | + } |
| 165 | + console.log( '%d = %d * 2^%d = %d', x, f[ 0 ], f[ 1 ], v ); |
| 166 | +} |
| 167 | +``` |
| 168 | + |
| 169 | +</section> |
| 170 | + |
| 171 | +<!-- /.examples --> |
| 172 | + |
| 173 | +<!-- C interface documentation. --> |
| 174 | + |
| 175 | +* * * |
| 176 | + |
| 177 | +<section class="c"> |
| 178 | + |
| 179 | +## C APIs |
| 180 | + |
| 181 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 182 | + |
| 183 | +<section class="intro"> |
| 184 | + |
| 185 | +</section> |
| 186 | + |
| 187 | +<!-- /.intro --> |
| 188 | + |
| 189 | +<!-- C usage documentation. --> |
| 190 | + |
| 191 | +<section class="usage"> |
| 192 | + |
| 193 | +### Usage |
| 194 | + |
| 195 | +```c |
| 196 | +#include "stdlib/math/base/special/frexpf.h" |
| 197 | +``` |
| 198 | + |
| 199 | +#### stdlib_base_frexpf( x, frac, exp ) |
| 200 | + |
| 201 | +Splits a [single-precision floating-point number][ieee754] into a normalized fraction and an integer power of two. |
| 202 | + |
| 203 | +```c |
| 204 | +#include <stdint.h> |
| 205 | +
|
| 206 | +float frac; |
| 207 | +int32_t exp; |
| 208 | +stdlib_base_frexpf( 4.0f, &frac, &exp ); |
| 209 | +``` |
| 210 | + |
| 211 | +The function accepts the following arguments: |
| 212 | + |
| 213 | +- **x**: `[in] float` input value. |
| 214 | +- **frac**: `[out] float*` destination for the normalized fraction. |
| 215 | +- **exp**: `[out] int32_t*` destination for the integer power of two. |
| 216 | + |
| 217 | +```c |
| 218 | +void stdlib_base_frexpf( const float x, float *frac, int32_t *exp ); |
| 219 | +``` |
| 220 | +
|
| 221 | +</section> |
| 222 | +
|
| 223 | +<!-- /.usage --> |
| 224 | +
|
| 225 | +<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 226 | +
|
| 227 | +<section class="notes"> |
| 228 | +
|
| 229 | +</section> |
| 230 | +
|
| 231 | +<!-- /.notes --> |
| 232 | +
|
| 233 | +<!-- C API usage examples. --> |
| 234 | +
|
| 235 | +<section class="examples"> |
| 236 | +
|
| 237 | +### Examples |
| 238 | +
|
| 239 | +```c |
| 240 | +#include "stdlib/math/base/special/frexpf.h" |
| 241 | +#include <stdint.h> |
| 242 | +#include <stdio.h> |
| 243 | +#include <inttypes.h> |
| 244 | + |
| 245 | +int main( void ) { |
| 246 | + const float x[] = { 4.0f, 0.0f, -0.0f, 1.0f, -1.0f, 3.14f, -3.14f, 1.0e38f, -1.0e38f, 1.0f/0.0f, -1.0f/0.0f, 0.0f/0.0f }; |
| 247 | + |
| 248 | + float frac; |
| 249 | + int32_t exp; |
| 250 | + int i; |
| 251 | + for ( i = 0; i < 12; i++ ) { |
| 252 | + stdlib_base_frexpf( x[i], &frac, &exp ); |
| 253 | + printf( "x: %f => frac: %f, exp: %" PRId32 "\n", x[i], frac, exp ); |
| 254 | + } |
| 255 | +} |
| 256 | +``` |
| 257 | +
|
| 258 | +</section> |
| 259 | +
|
| 260 | +<!-- /.examples --> |
| 261 | +
|
| 262 | +</section> |
| 263 | +
|
| 264 | +<!-- /.c --> |
| 265 | +
|
| 266 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 267 | +
|
| 268 | +<section class="related"> |
| 269 | +
|
| 270 | +</section> |
| 271 | +
|
| 272 | +<!-- /.related --> |
| 273 | +
|
| 274 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 275 | +
|
| 276 | +<section class="links"> |
| 277 | +
|
| 278 | +[ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985 |
| 279 | +
|
| 280 | +[@stdlib/math/base/special/absf]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/absf |
| 281 | +
|
| 282 | +<!-- <related-links> --> |
| 283 | +
|
| 284 | +<!-- </related-links> --> |
| 285 | +
|
| 286 | +</section> |
| 287 | +
|
| 288 | +<!-- /.links --> |
0 commit comments