Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How would frexp work for ABDKMathQuad in solidity #25

Closed
yashnaman opened this issue Apr 26, 2023 · 0 comments
Closed

How would frexp work for ABDKMathQuad in solidity #25

yashnaman opened this issue Apr 26, 2023 · 0 comments

Comments

@yashnaman
Copy link

I need a function that converts ABDKMathQuad number into mantissa and exponent where quad = mantissa * 2**exponent. The mantissa lies in the open interval(-1, 1), while the twos exponent is a signed integer. It isn't as straightforward as I thought. The function I have constructed is shown below. But it is obvious that it isn't correct. Can anyone help me with this?

I expect the output to be equivalent to what numpy.frexp would return for a float.

function frexp(
        bytes16 quad
    ) public pure returns (uint256 mantissa, int256 exponent) {
        uint256 exponent1 = (uint128(quad) >> 112) & 0x7FFF;
        mantissa = uint128(quad) & 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFF;

        mantissa = mantissa * 2 ** 63;
        exponent = -int256(exponent1);
        if (exponent == 0) {
            //it's a subnormal number
            exponent += 63;
        } else {
            //substract one from the whole number
            quad = quad.sub(ABDKMathQuad.fromUInt(1));
            //mantissat = mantissa of the original number 2**63
            //exponent = exponent of the substracted number - 16383 - 63
            exponent += 16383 + 63;
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant