time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

round10.js (2391B)


      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 'use strict';
     20 
     21 // MODULES //
     22 
     23 var isnan = require( './../../../../base/assert/is-nan' );
     24 var isInfinite = require( './../../../../base/assert/is-infinite' );
     25 var pow = require( './../../../../base/special/pow' );
     26 var floor = require( './../../../../base/special/floor' );
     27 var ceil = require( './../../../../base/special/ceil' );
     28 var log10 = require( './../../../../base/special/log10' );
     29 var MAX_EXP = require( '@stdlib/constants/float64/max-base10-exponent' );
     30 var MIN_EXP_SUBNORMAL = require( '@stdlib/constants/float64/min-base10-exponent-subnormal' );
     31 
     32 
     33 // VARIABLES //
     34 
     35 // 10^308:
     36 var HUGE = 1.0e308;
     37 
     38 // 10^-323
     39 var TINY = 1.0e-323;
     40 
     41 
     42 // MAIN //
     43 
     44 /**
     45 * Rounds a numeric value to the nearest power of `10` on a linear scale.
     46 *
     47 * @param {number} x - input value
     48 * @returns {number} rounded value
     49 *
     50 * @example
     51 * var v = round10( 3.141592653589793 );
     52 * // returns 1.0
     53 *
     54 * @example
     55 * var v = round10( 13.0 );
     56 * // returns 10.0
     57 *
     58 * @example
     59 * var v = round10( -0.314 );
     60 * // returns -0.1
     61 */
     62 function round10( x ) {
     63 	var sign;
     64 	var half;
     65 	var p1;
     66 	var p2;
     67 	var y1;
     68 	var y2;
     69 	var p;
     70 	if (
     71 		isnan( x ) ||
     72 		isInfinite( x ) ||
     73 		x === 0.0
     74 	) {
     75 		return x;
     76 	}
     77 	if ( x < 0 ) {
     78 		x = -x;
     79 		sign = -1.0;
     80 	} else {
     81 		sign = 1.0;
     82 	}
     83 	// Solve the equation `10^p = x` for `p`:
     84 	p = log10( x );
     85 
     86 	// Find the previous and next integer powers:
     87 	p1 = floor( p );
     88 	p2 = ceil( p );
     89 
     90 	// Handle tiny:
     91 	if ( p1 === MIN_EXP_SUBNORMAL ) {
     92 		return sign * TINY;
     93 	}
     94 	// Handle huge:
     95 	if ( p1 === MAX_EXP ) {
     96 		return sign * HUGE;
     97 	}
     98 	// Compute previous and next powers of 10:
     99 	y1 = pow( 10.0, p1 );
    100 	y2 = pow( 10.0, p2 );
    101 
    102 	// Find the closest power of 10:
    103 	half = ( y2 - y1 ) / 2.0;
    104 	if ( y1+half > x ) {
    105 		return sign * y1;
    106 	}
    107 	return sign * y2;
    108 }
    109 
    110 
    111 // EXPORTS //
    112 
    113 module.exports = round10;