Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 73942x 73942x 73942x 73942x 73942x 73942x 73942x 73942x 73942x 73942x 73942x 73942x 73942x 73942x 73942x 73942x 73942x 73942x 73942x 148x 148x 73942x 678x 678x 73942x 4x 4x 73112x 73112x 73942x 98x 98x 73942x 107x 107x 73942x 500x 500x 73942x 500x 500x 73942x 4117x 4117x 73942x 4096x 4096x 73942x 412x 412x 63282x 63282x 73942x 435x 435x 73942x 50x 50x 50x 50x 73942x 303x 103x 103x 103x 303x 100x 100x 100x 100x 62494x 73942x 1099x 73942x 100x 100x 100x 62394x 62394x 62394x 62394x 62394x 62394x 62394x 62394x 62394x 73942x 73942x 62394x 62394x 62394x 62394x 62394x 73942x 395x 395x 195x 195x 96x 96x 96x 99x 99x 99x 200x 200x 200x 100x 100x 100x 100x 100x 100x 61999x 61999x 61999x 61999x 61999x 61999x 61999x 61999x 61999x 61999x 61999x 61999x 61999x 73942x 25464x 25464x 25464x 36535x 73942x 2x 2x 2x 2x 2x 36533x 73942x 14707x 14707x 14707x 21826x 73942x 2x 2x 2x 2x 2x 21824x 21824x 21824x 21824x 73942x 1x 1x 1x 1x 1x | /** * @license Apache-2.0 * * Copyright (c) 2025 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * * ## Notice * * The following copyright and license were part of the original implementation available as part of [FreeBSD]{@link https://svnweb.freebsd.org/base/release/12.2.0/lib/msun/src/e_powf.c}. The implementation follows the original, but has been modified for JavaScript. * * ```text * Copyright (C) 2004 by Sun Microsystems, Inc. All rights reserved. * * Developed at SunPro, a Sun Microsystems, Inc. business. * Permission to use, copy, modify, and distribute this * software is freely granted, provided that this notice * is preserved. * ``` */ 'use strict'; // MODULES // var isnanf = require( '@stdlib/math/base/assert/is-nanf' ); var isOddf = require( '@stdlib/math/base/assert/is-oddf' ); var isInfinitef = require( '@stdlib/math/base/assert/is-infinitef' ); var isIntegerf = require( '@stdlib/math/base/assert/is-integerf' ); var sqrtf = require( '@stdlib/math/base/special/sqrtf' ); var absf = require( '@stdlib/math/base/special/absf' ); var fromWordf = require( '@stdlib/number/float32/base/from-word' ); var toWordf = require( '@stdlib/number/float32/base/to-word' ); var f32 = require( '@stdlib/number/float64/base/to-float32' ); var NINF = require( '@stdlib/constants/float32/ninf' ); var PINF = require( '@stdlib/constants/float32/pinf' ); var ABS_MASK = require( '@stdlib/constants/float32/abs-mask' ); var xIsZerof = require( './x_is_zerof.js' ); var yIsInfinitef = require( './y_is_infinitef.js' ); var log2axf = require( './log2axf.js' ); var logxf = require( './logxf.js' ); var pow2f = require( './pow2f.js' ); // VARIABLES // var ZERO = f32( 0.0 ); var HALF = f32( 0.5 ); var ONE = f32( 1.0 ); var TWO = f32( 2.0 ); var THREE = f32( 3.0 ); var NEG_ZERO = f32( -0.0 ); var NEG_HALF = f32( -0.5 ); var NEG_ONE = f32( -1.0 ); var HUGE = f32( 1.0e30 ); var TINY = f32( 1.0e-30 ); // -(128-log2(ovfl+.5ulp)) var OVT = f32( 4.2995665694e-08 ); // 2^27 = 134217728.0 => 1 0011010 00000000000000000000000 => 0x4d000000 = 1291845632 var Y_LARGE_WORD = 0x4d000000|0; // asm type annotation // 0.9999994039535522 => 0 01111110 11111111111111111110110 => 0x3f7ffff6 = 1065353206 var X_BELOW_ONE_WORD = 0x3f7ffff6|0; // asm type annotation // 1.0000008344650269 => 0 01111111 00000000000000000000111 => 0x3f800007 = 1065353223 var X_ABOVE_ONE_WORD = 0x3f800007|0; // asm type annotation // Mask to clear lower 12 significand bits: var TRUNC_MASK_12 = 0xfffff000|0; // asm type annotation // 128 => 0 10000110 00000000000000000000000 => 0x43000000 = 1124073472 var Z_OVF_WORD = 0x43000000|0; // asm type annotation // 150 => 0 10000110 00101100000000000000000 => 0x43160000 = 1125515264 var Z_UNF_WORD = 0x43160000|0; // asm type annotation // -150 => 1 10000110 00101100000000000000000 => 0xc3160000 = 3272998912 var Z_NEG_UNF_WORD = 0xc3160000|0; // asm type annotation // Log workspace: var LOG_WORKSPACE = [ 0.0, 0.0 ]; // MAIN // /** * Evaluates the exponential function for a single-precision floating-point number. * * @param {number} x - base * @param {number} y - exponent * @returns {number} function value * * @example * var v = powf( 2.0, 3.0 ); * // returns 8.0 * * @example * var v = powf( 4.0, 0.5 ); * // returns 2.0 * * @example * var v = powf( 100.0, 0.0 ); * // returns 1.0 * * @example * var v = powf( 3.1415927410125732, 5.0 ); * // returns ~306.0197 * * @example * var v = powf( 3.1415927410125732, -0.2 ); * // returns ~0.7954 * * @example * var v = powf( NaN, 3.0 ); * // returns NaN * * @example * var v = powf( 5.0, NaN ); * // returns NaN * * @example * var v = powf( NaN, NaN ); * // returns NaN */ function powf( x, y ) { var tmp; var ahx; // absolute value word `x` var ahy; // absolute value word `y` var ax; // absolute value `x` var hx; // word representation of `x` var hy; // word representation of `y` var sn; // sign of the result var y1; var hp; var lp; var t; var z; // y prime var j; x = f32( x ); y = f32( y ); if ( y === ZERO ) { return ONE; } if ( x === ONE ) { return ONE; } if ( isnanf( x ) || isnanf( y ) ) { return NaN; } // Other special cases `y`... if ( y === ONE ) { return x; } if ( y === NEG_ONE ) { return f32( ONE / x ); } if ( y === HALF ) { return sqrtf( x ); } if ( y === NEG_HALF ) { return f32( ONE / sqrtf( x ) ); } if ( y === TWO ) { return f32( x * x ); } if ( y === THREE ) { return f32( f32( x * x ) * x ); } if ( isInfinitef( y ) ) { // y is +-inf return yIsInfinitef( x, y ); } // Other special cases `x`... if ( x === ZERO ) { return xIsZerof( x, y ); } if ( x === NEG_ONE ) { if ( isIntegerf( y ) ) { return ( isOddf( y ) ) ? NEG_ONE : ONE; } } if ( isInfinitef( x ) ) { if ( x === NINF ) { // `pow( 1/x, -y )` return powf( NEG_ZERO, f32( -y ) ); } if ( y < ZERO ) { return ZERO; } return PINF; } if ( x < ZERO && isIntegerf( y ) === false ) { // Signal NaN... return NaN; // (-1)**non-int is NaN } ax = absf( x ); hx = toWordf( x ) | 0; // asm type annotation hy = toWordf( y ) | 0; // asm type annotation // Remove the sign bits (i.e., get absolute values): ahx = (hx & ABS_MASK) | 0; // asm type annotation ahy = (hy & ABS_MASK) | 0; // asm type annotation // Determine the sign of the result... if ( x < ZERO && isOddf( y ) ) { sn = NEG_ONE; } else { sn = ONE; } // Case 1: `|y|` is huge // If |y| > 2^27 if ( ahy > Y_LARGE_WORD ) { // Over- or underflow if `x` is not close to unity... if ( ahx < X_BELOW_ONE_WORD ) { // y < 0 if ( y < ZERO ) { // Signal overflow... return f32( f32( sn * HUGE ) * HUGE ); } // Signal underflow... return f32( f32( sn * TINY ) * TINY ); } if ( ahx > X_ABOVE_ONE_WORD ) { // y > 0 if ( y > ZERO ) { // Signal overflow... return f32( f32( sn * HUGE ) * HUGE ); } // Signal underflow... return f32( f32( sn * TINY ) * TINY ); } // At this point, `|1-x|` is tiny (`<= 2^-20`). Suffice to compute `log(x)` by `x - x^2/2 + x^3/3 - x^4/4`. t = logxf( LOG_WORKSPACE, ax ); } // Case 2: `|y|` is not huge... else { t = log2axf( LOG_WORKSPACE, ax, ahx ); } // Split `y` into `y1 + y2` and compute `(y1+y2) * (t1+t2)`... tmp = toWordf( y ) | 0; // asm type annotation y1 = fromWordf( tmp & TRUNC_MASK_12 ); lp = f32( f32( f32( y-y1 ) * t[0] ) + f32( y * t[1] ) ); hp = f32( y1 * t[0] ); z = f32( lp + hp ); j = toWordf( z ) | 0; // asm type annotation // z > 128 if ( j > Z_OVF_WORD ) { // Signal overflow... return f32( f32( sn * HUGE ) * HUGE ); } // z == 128 if ( j === Z_OVF_WORD ) { if ( f32( lp+OVT ) > f32( z-hp ) ) { // Signal overflow... return f32( f32( sn * HUGE ) * HUGE ); } } // z < -150 if ( (j&ABS_MASK) > Z_UNF_WORD ) { // Signal underflow... return f32( f32( sn * TINY ) * TINY ); } // z == -150 if ( j === Z_NEG_UNF_WORD ) { if ( lp <= f32( z-hp ) ) { // Signal underflow... return f32( f32( sn * TINY ) * TINY ); } } // Compute `2^(hp+lp)`... z = pow2f( j, hp, lp ); return f32( sn * z ); } // EXPORTS // module.exports = powf; |