# Significand of IEEE float as an integer

**URL:** <https://discourse.julialang.org/t/significand-of-ieee-float-as-an-integer/3698>\
**Category:** Numerics\
**Tags:** numbers\
**Created:** [May 14, 2017, 3:21am UTC](https://discourse.julialang.org/t/significand-of-ieee-float-as-an-integer/3698 "2017-05-14T03:21:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fengyang.wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fengyang.wang/32/104_2.png) [@fengyang.wang](https://discourse.julialang.org/u/fengyang.wang)\
**Post date:** [May 14, 2017, 3:21am UTC](https://discourse.julialang.org/t/significand-of-ieee-float-as-an-integer/3698/1 "2017-05-14T03:21:57Z")

</div>

I want a function like `frexp`, but that returns an integer (of the same size of the floating point) and an exponent. Currently I have tried the following implementation:

```julia
julia> inexp(x::T) where T <: Base.Math.IEEEFloat = begin
           m, ex = frexp(x)
           b = Base.Math.significand_bits(T)
           mi = trunc(Integer, m * exp2(b))
           return mi, ex - b
       end
inexp (generic function with 1 method)

julia> inexp(2.3)
(2589569785738035, -50)

```

I am not too familiar with floating points, so I wonder if there anything I should look out for that I am not handling properly in my implementation here.

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [May 14, 2017, 8:23am UTC](https://discourse.julialang.org/t/significand-of-ieee-float-as-an-integer/3698/2 "2017-05-14T08:23:11Z")

</div>

This seems to always return `Int`, not “integers of the same size of the floating point”.

Also, I think you’re losing one bit of the significand. For positive, non-denormal, non-NaN, non-Inf, this will give your `mi` shifted one step left, and with one more bit of information.

```julia
Base.significand_mask(T) & reinterpret(Unsigned, x) | 1 << Base.significand_bits(T)

```

---

<div class="post-metadata">

**Author:** ![musm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/musm/32/3675_2.png) [@musm](https://discourse.julialang.org/u/musm)\
**Post date:** [May 14, 2017, 8:25pm UTC](https://discourse.julialang.org/t/significand-of-ieee-float-as-an-integer/3698/3 "2017-05-14T20:25:25Z")

</div>

```julia
import Base: sign_mask, significand_mask, exponent_mask, Math.significand_bits, Math.exponent_bias

function inexp(x::T) where {T}
    xu = reinterpret(Unsigned, x)
    xs = xu & ~sign_mask(T)
    xs >= exponent_mask(T) && return throw(DomainError())

    k = Int(xs >> significand_bits(T))
    if k == 0 # x is subnormal
        m = leading_zeros(xs) - exponent_bits(T)
        xs <<= unsigned(m)
        xu = xs | (xu & sign_mask(T))
        k = 1 - m
    end
    k -= exponent_bias(T)
    k -= significand_bits(T)

    xm = xu & significand_mask(T)
    int = Int(xm | 1 << significand_bits(T))
    return (signbit(x) ? -int : int), k
end

```

which handles NaN and denormals 🙂

```julia
x = realmin(Float64)/2
ret,k = inexp(x)
x == Float64(ret*big(2.0)^k) # true

```
