Test name | Executions per second |
---|---|
bigint | 6354857.0 Ops/sec |
parts | 7351464.0 Ops/sec |
double | 1700735.8 Ops/sec |
Прямое масштабирование с фиксированной точностью: | 2922840.0 Ops/sec |
Битовые операции (самый быстрый для целых чисел с автоматическим переходом на масштабирование): | 9470969.0 Ops/sec |
Комбинированный подход с кэшированием степеней: | 20886314.0 Ops/sec |
<!--your preparation HTML code goes here-->
/*your preparation JavaScript code goes here
To execute async code during the script preparation, wrap it as function globalMeasureThatScriptPrepareFunction, example:*/
async function globalMeasureThatScriptPrepareFunction() {
// This function is optional, feel free to remove it.
// await someThing();
}
/*When writing async/deferred tests, use `deferred.resolve()` to mark test as done*/
function numberToBigInt(num) {
const str = num.toString();
const decimalIndex = str.indexOf('.');
const scale = decimalIndex === -1 ? 0 : str.length - decimalIndex - 1;
const factor = Math.pow(10, scale);
const scaledValue = Math.round(Math.abs(num) * factor);
return {
value: BigInt(scaledValue),
exp: -scale
};
}
numberToBigInt(1230.1234);
function numberToBigInt(num) {
let intPart = Math.trunc(num);
let fracPart = Math.abs(num - intPart);
let scale = 0;
while (fracPart !== 0 && scale < 20) {
fracPart *= 10;
intPart = intPart * 10 + Math.trunc(fracPart);
fracPart = fracPart - Math.trunc(fracPart);
scale++;
}
return {
value: BigInt(Math.round(intPart)),
exp: -scale
};
}
numberToBigInt(1230.1234);
function numberToBigInt(num) {
const buffer = new ArrayBuffer(8);
const view = new DataView(buffer);
view.setFloat64(0, num);
const sign = view.getUint8(0) >> 7;
const exponent = ((view.getUint8(0) & 0x7F) << 4) | (view.getUint8(1) >> 4);
const mantissa = BigInt(view.getUint32(4)) |
(BigInt(view.getUint16(2) & 0x0FFF) << 32n);
const bias = 1023;
const scale = exponent - bias - 52;
const value = mantissa | (1n << 52n);
return {
value: sign ? -value : value,
exp: scale
};
}
numberToBigInt(1230.1234);
function numberToBigInt(num) {
const PRECISION = 15; // Максимальная точность IEEE 754
const scale = BigInt(10 ** PRECISION);
const scaledValue = BigInt(Math.round(num * Number(scale)));
// Оптимизация: убираем лишние нули в конце
let value = scaledValue;
let exp = -PRECISION;
while (value !== 0n && value % 10n === 0n) {
value /= 10n;
exp++;
}
return { value, exp };
}
numberToBigInt(1230.1234);
function numberToBigInt(num) {
// Быстрая проверка на целое число через битовые операции
if ((num | 0) === num) {
return {
value: BigInt(num),
exp: 0
};
}
// Для дробных - используем масштабирование
const scale = 1000000n; // 10^6 для достаточной точности
const scaledValue = BigInt(Math.round(num * 1000000));
// Оптимизация хвостовых нулей
let value = scaledValue;
let exp = -6;
while (value % 10n === 0n) {
value /= 10n;
exp++;
}
return { value, exp };
}
numberToBigInt(1230.1234);
function numberToBigInt(num) {
// Кэш степеней 10 для оптимизации
const POWERS = [1, 10, 100, 1000, 10000, 100000, 1000000];
// Определяем количество знаков после запятой
const magnitude = Math.abs(num);
let scale = 0;
if (magnitude !== Math.floor(magnitude)) {
const fraction = magnitude - Math.floor(magnitude);
scale = Math.min(6, -Math.floor(Math.log10(fraction)));
}
const scaledValue = Math.round(num * POWERS[scale]);
return {
value: BigInt(scaledValue),
exp: -scale
};
}
numberToBigInt(1230.1234);