{"version":3,"file":"LogarithmMapping.cjs","names":["MappingError"],"sources":["../../../../src/aggregator/exponential-histogram/mapping/LogarithmMapping.ts"],"sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\nimport * as ieee754 from './ieee754';\nimport * as util from '../util';\nimport type { Mapping } from './types';\nimport { MappingError } from './types';\n\n/**\n * LogarithmMapping implements exponential mapping functions for scale > 0.\n * For scales <= 0 the exponent mapping should be used.\n */\nexport class LogarithmMapping implements Mapping {\n  private readonly _scale: number;\n  private readonly _scaleFactor: number;\n  private readonly _inverseFactor: number;\n\n  constructor(scale: number) {\n    this._scale = scale;\n    this._scaleFactor = util.ldexp(Math.LOG2E, scale);\n    this._inverseFactor = util.ldexp(Math.LN2, -scale);\n  }\n\n  /**\n   * Maps positive floating point values to indexes corresponding to scale\n   * @param value\n   * @returns {number} index for provided value at the current scale\n   */\n  mapToIndex(value: number): number {\n    if (value <= ieee754.MIN_VALUE) {\n      return this._minNormalLowerBoundaryIndex() - 1;\n    }\n\n    // exact power of two special case\n    if (ieee754.isPowerOfTwo(value)) {\n      const exp = ieee754.getNormalBase2(value);\n      return (exp << this._scale) - 1;\n    }\n\n    // non-power of two cases. use Math.floor to round the scaled logarithm\n    const index = Math.floor(Math.log(value) * this._scaleFactor);\n    const maxIndex = this._maxNormalLowerBoundaryIndex();\n    if (index >= maxIndex) {\n      return maxIndex;\n    }\n\n    return index;\n  }\n\n  /**\n   * Returns the lower bucket boundary for the given index for scale\n   *\n   * @param index\n   * @returns {number}\n   */\n  lowerBoundary(index: number): number {\n    const maxIndex = this._maxNormalLowerBoundaryIndex();\n    if (index >= maxIndex) {\n      if (index === maxIndex) {\n        return 2 * Math.exp((index - (1 << this._scale)) / this._scaleFactor);\n      }\n      throw new MappingError(\n        `overflow: ${index} is > maximum lower boundary: ${maxIndex}`\n      );\n    }\n\n    const minIndex = this._minNormalLowerBoundaryIndex();\n    if (index <= minIndex) {\n      if (index === minIndex) {\n        return ieee754.MIN_VALUE;\n      } else if (index === minIndex - 1) {\n        return Math.exp((index + (1 << this._scale)) / this._scaleFactor) / 2;\n      }\n      throw new MappingError(\n        `overflow: ${index} is < minimum lower boundary: ${minIndex}`\n      );\n    }\n\n    return Math.exp(index * this._inverseFactor);\n  }\n\n  /**\n   * The scale used by this mapping\n   * @returns {number}\n   */\n  get scale(): number {\n    return this._scale;\n  }\n\n  private _minNormalLowerBoundaryIndex(): number {\n    return ieee754.MIN_NORMAL_EXPONENT << this._scale;\n  }\n\n  private _maxNormalLowerBoundaryIndex(): number {\n    return ((ieee754.MAX_NORMAL_EXPONENT + 1) << this._scale) - 1;\n  }\n}\n"],"mappings":";;;;;;;;AAaA,IAAa,mBAAb,MAAiD;CAC/C;CACA;CACA;CAEA,YAAY,OAAe;EACzB,KAAK,SAAS;EACd,KAAK,eAAA,aAAA,MAA0B,KAAK,OAAO,KAAK;EAChD,KAAK,iBAAA,aAAA,MAA4B,KAAK,KAAK,CAAC,KAAK;CACnD;;;;;;CAOA,WAAW,OAAuB;EAChC,IAAI,SAAA,gBAAA,WACF,OAAO,KAAK,6BAA6B,IAAI;EAI/C,IAAA,gBAAA,aAAyB,KAAK,GAE5B,QAAA,gBAAA,eADmC,KACzB,KAAK,KAAK,UAAU;EAIhC,MAAM,QAAQ,KAAK,MAAM,KAAK,IAAI,KAAK,IAAI,KAAK,YAAY;EAC5D,MAAM,WAAW,KAAK,6BAA6B;EACnD,IAAI,SAAS,UACX,OAAO;EAGT,OAAO;CACT;;;;;;;CAQA,cAAc,OAAuB;EACnC,MAAM,WAAW,KAAK,6BAA6B;EACnD,IAAI,SAAS,UAAU;GACrB,IAAI,UAAU,UACZ,OAAO,IAAI,KAAK,KAAK,SAAS,KAAK,KAAK,WAAW,KAAK,YAAY;GAEtE,MAAM,IAAIA,cAAAA,aACR,aAAa,MAAM,gCAAgC,UACrD;EACF;EAEA,MAAM,WAAW,KAAK,6BAA6B;EACnD,IAAI,SAAS,UAAU;GACrB,I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