提取 Javascript 数字的指数和尾数 [英] Extracting the exponent and mantissa of a Javascript Number

查看:35
本文介绍了提取 Javascript 数字的指数和尾数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种相当快速的方法可以从 Javascript 中的数字中提取指数和尾数?

Is there a reasonably fast way to extract the exponent and mantissa from a Number in Javascript?

AFAIK 没有办法在 Javascript 中获取数字背后的位,这让我觉得我在看一个分解问题:找到 mn 使得 2^n * m = k 对于给定的 k.由于整数分解是在 NP 中,我只能假设这将是一个相当困难的问题.

AFAIK there's no way to get at the bits behind a Number in Javascript, which makes it seem to me that I'm looking at a factorization problem: finding m and n such that 2^n * m = k for a given k. Since integer factorization is in NP, I can only assume that this would be a fairly hard problem.

我正在实现一个用于生成 Javascript 的 GHC 插件,需要实现 decodeFloat_Int#decodeDouble_2Int# 原始操作;我想我可以重写基础库的部分,这些部分使用该操作来做他们正在以其他方式做的事情(这应该不会太难,因为无论如何所有数字类型都将 Number 作为它们的表示,)但是它'如果我不需要,我会很好的.

I'm implementing a GHC plugin for generating Javascript and need to implement the decodeFloat_Int# and decodeDouble_2Int# primitive operations; I guess I could just rewrite the parts of the base library that uses the operation to do wahtever they're doing in some other way (which shouldn't be too hard since all numeric types have Number as their representation anyway,) but it'd be nice if I didn't have to.

有没有办法以一种甚至远程性能的方式做到这一点,通过一些黑暗的 Javascript 伏都教、聪明的数学或其他一些方法,或者我应该安全地使用基础库?

Is there any way to do this in an even remotely performant way, by some dark Javascript voodoo, clever mathematics or some other means, or should I just buckle down and have at the base library?

编辑根据 ruakh 和 Louis Wasserman 的出色回答,我提出了以下实现,它似乎运行良好:

EDIT Based on ruakh's and Louis Wasserman's excellent answers, I came up with the following implementation, which seems to work well enough:

function getNumberParts(x) {
    if(isNaN(x)) {
        return {mantissa: -6755399441055744, exponent: 972};
    }
    var sig = x > 0 ? 1 : -1;
    if(!isFinite(x)) {
        return {mantissa: sig * 4503599627370496, exponent: 972};
    }
    x = Math.abs(x);
    var exp = Math.floor(Math.log(x)*Math.LOG2E)-52;
    var man = x/Math.pow(2, exp);
    return {mantissa: sig*man, exponent: exp};
}

推荐答案

ECMAScript 没有定义任何直接的方法来做到这一点;但就其价值而言,这不是与质数分解相同意义上的分解问题".

ECMAScript doesn't define any straightforward way to do this; but for what it's worth, this isn't a "factorization problem" in the same sense as prime factorization.

首先处理符号,然后使用二叉树方法(或对数)求指数,最后除以相关的 2 次幂得到尾数,理论上可以很快完成您想要的操作;但不幸的是,在实践中实现这一点可能有些棘手(对于非规范化数字等特殊情况).我建议您通读 ECMAScript 规范的第 8.5 节,以了解您必须处理的情况.

What you want can theoretically be done very quickly by first handling the sign, then using a binary-tree approach (or logarithm) to find the exponent, and lastly dividing by the relevant power of two to get the mantissa; but unfortunately, it can be somewhat tricky to implement this in practice (what with special cases such as denormalized numbers). I recommend you read through section 8.5 of the ECMAScript specification to get a sense of what cases you'll have to handle.

这篇关于提取 Javascript 数字的指数和尾数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆