PyTorch torch.frexp function
PyTorch torch reference manual
torch.frexp A function in PyTorch for decomposing a floating-point number into a mantissa and an exponent.
Function definition
torch.frexp(input)
Returns two tensors:
- mantissa - the mantissa part, in the range [-0.5, 1)
- exponent - the exponent part, an integer
Usage examples
Example
import
torch
Create a floating-point tensor
x
=
torch.
tensor
(
[
8.0
,
4.5
,
2.0
]
)
Decompose into mantissa and exponent
mantissa
,
exponent
=
torch.
frexp
(
x
)
(
"Mantissa:"
,
mantissa
)
(
"Exponent:"
,
exponent
)
Verify: x = mantissa * 2^exponent
(
"Verify:"
,
mantissa *
2
** exponent
)
The output is:
尾数: tensor([0.5000, 0.5625, 0.5000])
指数: tensor([4, 3, 2])
验证: tensor([8., 4.5, 2.])