torch.Tensor.Tensor.nextafter
Tensor.nextafter<O extends Shape>(other: Tensor<O>): Tensor<DynamicShape, D, Dev>Computes the next floating-point number after each element in this tensor towards other.
For each element, returns the next representable floating-point value in the direction of the corresponding element in other. Useful for:
- Numerical stability testing (perturbing values by machine epsilon)
- Interval arithmetic and conservative rounding
- Floating-point arithmetic debugging
- Rigorous mathematical computations
- Testing sensitivity to small perturbations
- Result moves towards other by the smallest representable step (machine epsilon)
- If self == other, the result is exactly equal to self
Parameters
otherTensor<O>- Direction tensor (determines which way to step)
Returns
Tensor<DynamicShape, D, Dev>– Tensor with next representable float value towards otherExamples
const x = torch.tensor([1.0, 2.0, 3.0]);
const y = torch.tensor([2.0, 1.0, 4.0]);
const result = x.nextafter(y);
// Step towards y: [1.0+ε, 2.0-ε, 3.0+ε] where ε is machine epsilon
// Testing numerical stability
const testValue = torch.tensor([1.0]);
const perturbed = testValue.nextafter(torch.tensor([2.0])); // Smallest incrementSee Also
- PyTorch torch.nextafter()
- frexp - Floating-point decomposition