time-to-botec

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

ceilsd.js (2806B)


      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 log10 = require( './../../../../base/special/log10' );
     27 var ln = require( './../../../../base/special/ln' );
     28 var abs = require( './../../../../base/special/abs' );
     29 var floor = require( './../../../../base/special/floor' );
     30 var exponent = require( '@stdlib/number/float64/base/exponent' );
     31 var ceil = require( './../../../../base/special/ceil' );
     32 
     33 
     34 // MAIN //
     35 
     36 /**
     37 * Rounds a numeric value to the nearest number toward positive infinity with \\(n\\) significant figures.
     38 *
     39 * @param {number} x - input value
     40 * @param {PositiveInteger} n - number of significant figures
     41 * @param {PositiveInteger} [b=10] - base
     42 * @returns {number} rounded value
     43 *
     44 * @example
     45 * var v = ceilsd( 3.141592653589793, 5 );
     46 * // returns 3.1416
     47 *
     48 * @example
     49 * var v = ceilsd( 3.141592653589793, 1 );
     50 * // returns 4.0
     51 *
     52 * @example
     53 * var v = ceilsd( 12368.0, 2 );
     54 * // returns 13000.0
     55 *
     56 * @example
     57 * var v = ceilsd( 0.0313, 2, 2 );
     58 * // returns 0.046875
     59 */
     60 function ceilsd( x, n, b ) {
     61 	var base;
     62 	var exp;
     63 	var s;
     64 	var y;
     65 	if (
     66 		isnan( x ) ||
     67 		isnan( n ) ||
     68 		n < 1 ||
     69 		isInfinite( n )
     70 	) {
     71 		return NaN;
     72 	}
     73 	if ( arguments.length > 2 ) {
     74 		if (
     75 			isnan( b ) ||
     76 			b <= 0 ||
     77 			isInfinite( b )
     78 		) {
     79 			return NaN;
     80 		}
     81 		base = b;
     82 	} else {
     83 		base = 10;
     84 	}
     85 	if ( isInfinite( x ) || x === 0.0 ) {
     86 		return x;
     87 	}
     88 	if ( base === 10 ) {
     89 		exp = log10( abs( x ) );
     90 	}
     91 	else if ( base === 2 ) {
     92 		exp = exponent( abs( x ) );
     93 	}
     94 	else {
     95 		exp = ln( abs(x) ) / ln( base );
     96 	}
     97 	exp = floor( exp - n + 1.0 );
     98 	s = pow( base, abs( exp ) );
     99 
    100 	// Check for overflow:
    101 	if ( isInfinite( s ) ) {
    102 		return x;
    103 	}
    104 	// To avoid numerical stability issues due to floating-point rounding error (e.g., 3.55/0.1-35.5 = -7.105427357601e-15 and 3.55*10-35.5 = 0), we must treat positive and negative exponents separately.
    105 	if ( exp < 0 ) {
    106 		y = ceil( x * s ) / s;
    107 	} else {
    108 		y = ceil( x / s ) * s;
    109 	}
    110 	// Check for overflow:
    111 	if ( isInfinite( y ) ) {
    112 		return x;
    113 	}
    114 	return y;
    115 }
    116 
    117 
    118 // EXPORTS //
    119 
    120 module.exports = ceilsd;