log10.js (4111B)
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 * ## Notice 20 * 21 * The following copyright and license were part of the original implementation available as part of [FreeBSD]{@link https://svnweb.freebsd.org/base/release/9.3.0/lib/msun/src/e_log10.c}. The implementation follows the original, but has been modified for JavaScript. 22 * 23 * ```text 24 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 25 * 26 * Developed at SunPro, a Sun Microsystems, Inc. business. 27 * Permission to use, copy, modify, and distribute this 28 * software is freely granted, provided that this notice 29 * is preserved. 30 * ``` 31 */ 32 33 'use strict'; 34 35 // MODULES // 36 37 var getHighWord = require( '@stdlib/number/float64/base/get-high-word' ); 38 var setHighWord = require( '@stdlib/number/float64/base/set-high-word' ); 39 var setLowWord = require( '@stdlib/number/float64/base/set-low-word' ); 40 var isnan = require( './../../../../base/assert/is-nan' ); 41 var BIAS = require( '@stdlib/constants/float64/exponent-bias' ); 42 var NINF = require( '@stdlib/constants/float64/ninf' ); 43 var klog = require( './klog.js' ); 44 45 46 // VARIABLES // 47 48 var TWO54 = 1.80143985094819840000e+16; // 0x43500000, 0x00000000 49 var IVLN10HI = 4.34294481878168880939e-01; // 0x3fdbcb7b, 0x15200000 50 var IVLN10LO = 2.50829467116452752298e-11; // 0x3dbb9438, 0xca9aadd5 51 var LOG10_2HI = 3.01029995663611771306e-01; // 0x3FD34413, 0x509F6000 52 var LOG10_2LO = 3.69423907715893078616e-13; // 0x3D59FEF3, 0x11F12B36 53 54 // 0x000fffff = 1048575 => 0 00000000000 11111111111111111111 55 var HIGH_SIGNIFICAND_MASK = 0x000fffff|0; // asm type annotation 56 57 // 0x7ff00000 = 2146435072 => 0 11111111111 00000000000000000000 => biased exponent: 2047 = 1023+1023 => 2^1023 58 var HIGH_MAX_NORMAL_EXP = 0x7ff00000|0; // asm type annotation 59 60 // 0x00100000 = 1048576 => 0 00000000001 00000000000000000000 => biased exponent: 1 = -1022+1023 => 2^-1022 61 var HIGH_MIN_NORMAL_EXP = 0x00100000|0; // asm type annotation 62 63 // 0x3ff00000 = 1072693248 => 0 01111111111 00000000000000000000 => biased exponent: 1023 = 0+1023 => 2^0 = 1 64 var HIGH_BIASED_EXP_0 = 0x3ff00000|0; // asm type annotation 65 66 67 // MAIN // 68 69 /** 70 * Evaluates the common logarithm (base ten). 71 * 72 * @param {NonNegativeNumber} x - input value 73 * @returns {number} function value 74 * 75 * @example 76 * var v = log10( 4.0 ); 77 * // returns ~0.602 78 * 79 * @example 80 * var v = log10( 8.0 ); 81 * // returns ~0.903 82 * 83 * @example 84 * var v = log10( 0.0 ); 85 * // returns -Infinity 86 * 87 * @example 88 * var v = log10( Infinity ); 89 * // returns Infinity 90 * 91 * @example 92 * var v = log10( NaN ); 93 * // returns NaN 94 * 95 * @example 96 * var v = log10( -4.0 ); 97 * // returns NaN 98 */ 99 function log10( x ) { 100 var hi; 101 var hx; 102 var lo; 103 var f; 104 var i; 105 var k; 106 var y; 107 var z; 108 109 if ( isnan( x ) || x < 0.0 ) { 110 return NaN; 111 } 112 if ( x === 0.0 ) { 113 return NINF; 114 } 115 hx = getHighWord( x ); 116 k = 0|0; // asm type annotation 117 118 // Case: 0 < x < 2**-1022 119 if ( hx < HIGH_MIN_NORMAL_EXP ) { 120 // Subnormal number, scale up `x`... 121 k -= 54|0; // asm type annotation 122 x *= TWO54; 123 hx = getHighWord( x ); 124 } 125 if ( hx >= HIGH_MAX_NORMAL_EXP ) { 126 return x + x; 127 } 128 k += ((hx>>20) - BIAS)|0; // asm type annotation 129 hx &= HIGH_SIGNIFICAND_MASK; 130 i = ( (hx+0x95f64)&0x100000 )|0; // asm type annotation 131 132 // Normalize `x` or `x/2`... 133 x = setHighWord( x, hx|(i^HIGH_BIASED_EXP_0) ); 134 k += (i>>20)|0; // asm type annotation 135 y = k; 136 f = klog( x ); 137 x -= 1; 138 hi = setLowWord( x, 0.0 ); 139 lo = x - hi; 140 z = (y*LOG10_2LO) + ( (x+f)*IVLN10LO ); 141 z += ( (lo+f)*IVLN10HI ) + ( hi*IVLN10HI ); 142 return z + ( y*LOG10_2HI ); 143 } 144 145 146 // EXPORTS // 147 148 module.exports = log10;