docs-v2/content/flux/v0/stdlib/math/frexp.md

3.7 KiB

title description menu weight
math.frexp() function `math.frexp()` breaks `f` into a normalized fraction and an integral part of two.
flux_v0_ref
name parent identifier
math.frexp math math/frexp
101

math.frexp() breaks f into a normalized fraction and an integral part of two.

It returns frac and exp satisfying f == frac x 2**exp, with the absolute value of frac in the interval [1/2, 1).

Function type signature
(f: float) => {frac: float, exp: int}

{{% caption %}} For more information, see Function type signatures. {{% /caption %}}

Parameters

f

({{< req >}}) Value to operate on.

Examples

Return the normalize fraction and integral of a value

import "math"

math.frexp(f: 22.0)// {exp: 5, frac: 0.6875}


Use math.frexp in map

import "sampledata"
import "math"

sampledata.float()
    |> map(
        fn: (r) => {
            result = math.frexp(f: r._value)

            return {r with exp: result.exp, frac: result.frac}
        },
    )

{{< expand-wrapper >}} {{% expand "View example input and output" %}}

Input data

_time *tag _value
2021-01-01T00:00:00Z t1 -2.18
2021-01-01T00:00:10Z t1 10.92
2021-01-01T00:00:20Z t1 7.35
2021-01-01T00:00:30Z t1 17.53
2021-01-01T00:00:40Z t1 15.23
2021-01-01T00:00:50Z t1 4.43
_time *tag _value
2021-01-01T00:00:00Z t2 19.85
2021-01-01T00:00:10Z t2 4.97
2021-01-01T00:00:20Z t2 -3.75
2021-01-01T00:00:30Z t2 19.77
2021-01-01T00:00:40Z t2 13.86
2021-01-01T00:00:50Z t2 1.86

Output data

_time _value exp frac *tag
2021-01-01T00:00:00Z -2.18 2 -0.545 t1
2021-01-01T00:00:10Z 10.92 4 0.6825 t1
2021-01-01T00:00:20Z 7.35 3 0.91875 t1
2021-01-01T00:00:30Z 17.53 5 0.5478125 t1
2021-01-01T00:00:40Z 15.23 4 0.951875 t1
2021-01-01T00:00:50Z 4.43 3 0.55375 t1
_time _value exp frac *tag
2021-01-01T00:00:00Z 19.85 5 0.6203125 t2
2021-01-01T00:00:10Z 4.97 3 0.62125 t2
2021-01-01T00:00:20Z -3.75 2 -0.9375 t2
2021-01-01T00:00:30Z 19.77 5 0.6178125 t2
2021-01-01T00:00:40Z 13.86 4 0.86625 t2
2021-01-01T00:00:50Z 1.86 1 0.93 t2

{{% /expand %}} {{< /expand-wrapper >}}