PyTorch torch.frexp function


Pytorch torch 参考手册Pytorch torch Reference Manual

torch.frexpis a function in PyTorch used to decompose floating-point numbers into mantissa and 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 Example

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)
print("Mantissa:", mantissa)
print("Exponent:", exponent)

# Verify: x = mantissa * 2^exponent
print("Verification:", mantissa * 2 ** exponent)

The output result is:

尾数: tensor([0.5000, 0.5625, 0.5000])
指数: tensor([4, 3, 2])
验证: tensor([8., 4.5, 2.])

Pytorch torch 参考手册Pytorch torch Reference Manual

Other Extensions