time-to-botec

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

ceil10.js (2221B)


      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 var PINF = require( '@stdlib/constants/float64/pinf' );
     32 
     33 
     34 // MAIN //
     35 
     36 /**
     37 * Rounds a numeric value to the nearest power of `10` toward positive infinity.
     38 *
     39 * @param {number} x - input value
     40 * @returns {number} rounded value
     41 *
     42 * @example
     43 * var v = ceil10( 3.141592653589793 );
     44 * // returns 10.0
     45 *
     46 * @example
     47 * var v = ceil10( 9.0 );
     48 * // returns 10.0
     49 *
     50 * @example
     51 * var v = ceil10( -0.314 );
     52 * // returns -0.1
     53 */
     54 function ceil10( x ) {
     55 	var sign;
     56 	var p;
     57 	if (
     58 		isnan( x ) ||
     59 		isInfinite( x ) ||
     60 		x === 0.0
     61 	) {
     62 		return x;
     63 	}
     64 	if ( x < 0 ) {
     65 		x = -x;
     66 		sign = -1.0;
     67 	} else {
     68 		sign = 1.0;
     69 	}
     70 	// Solve the equation `10^p = x` for `p`:
     71 	p = log10( x );
     72 
     73 	// Determine a power of 10 which rounds the input value toward positive infinity:
     74 	if ( sign === -1.0 ) {
     75 		p = floor( p );
     76 	} else {
     77 		p = ceil( p );
     78 	}
     79 	// Handle underflow:
     80 	if ( p <= MIN_EXP_SUBNORMAL ) {
     81 		return sign * 0.0; // sign-preserving
     82 	}
     83 	// Handle overflow:
     84 	if ( p > MAX_EXP ) {
     85 		return PINF;
     86 	}
     87 	return sign * pow( 10.0, p );
     88 }
     89 
     90 
     91 // EXPORTS //
     92 
     93 module.exports = ceil10;