DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

The arithmetic operations with `nan` and `inf` in PyTorch

Buy Me a Coffee

*Memos:

  • My post explains how to create nan and inf in PyTorch.
  • My post explains the comparisons with nan and inf in PyTorch.

The arithmetic operations with torch.nan and 100.0, torch.nan or torch.inf as shown below:

*Memos:

  • The arithmetic operations with nan are always nan:
  • complex type cannot do mod(modulo) calculation with %.
import torch

""" `float` type """
torch.nan + 100.0      # nan
torch.nan + torch.nan  # nan
torch.nan + -torch.nan # nan
torch.nan + torch.inf  # nan
torch.nan + -torch.inf # nan

torch.nan - 100.0      # nan
torch.nan - torch.nan  # nan
torch.nan - -torch.nan # nan
torch.nan - torch.inf  # nan
torch.nan - -torch.inf # nan

torch.nan * 100.0      # nan
torch.nan * torch.nan  # nan
torch.nan * -torch.nan # nan
torch.nan * torch.inf  # nan
torch.nan * -torch.inf # nan

torch.nan / 100.0      # nan
torch.nan / torch.nan  # nan
torch.nan / -torch.nan # nan
torch.nan / torch.inf  # nan
torch.nan / -torch.inf # nan

torch.nan % 100.0      # nan
torch.nan % torch.nan  # nan
torch.nan % -torch.nan # nan
torch.nan % torch.inf  # nan
torch.nan % -torch.inf # nan

""" `complex` type """
complex(torch.nan, torch.nan) + (100.0+0.j)                     # (nan+nanj)
complex(torch.nan, torch.nan) + complex(torch.nan, torch.nan)   # (nan+nanj)
complex(torch.nan, torch.nan) + complex(torch.nan, -torch.nan)  # (nan+nanj)
complex(torch.nan, torch.nan) + complex(-torch.nan, torch.nan)  # (nan+nanj)
complex(torch.nan, torch.nan) + complex(-torch.nan, -torch.nan) # (nan+nanj)
complex(torch.nan, torch.nan) + complex(torch.inf, torch.inf)   # (nan+nanj)
complex(torch.nan, torch.nan) + complex(torch.inf, -torch.inf)  # (nan+nanj)
complex(torch.nan, torch.nan) + complex(-torch.inf, torch.inf)  # (nan+nanj)
complex(torch.nan, torch.nan) + complex(-torch.inf, -torch.inf) # (nan+nanj)

complex(torch.nan, torch.nan) - (100.0+0.j)                     # (nan+nanj)
complex(torch.nan, torch.nan) - complex(torch.nan, torch.nan)   # (nan+nanj)
complex(torch.nan, torch.nan) - complex(torch.nan, -torch.nan)  # (nan+nanj)
complex(torch.nan, torch.nan) - complex(-torch.nan, torch.nan)  # (nan+nanj)
complex(torch.nan, torch.nan) - complex(-torch.nan, -torch.nan) # (nan+nanj)
complex(torch.nan, torch.nan) - complex(torch.inf, torch.inf)   # (nan+nanj)
complex(torch.nan, torch.nan) - complex(torch.inf, -torch.inf)  # (nan+nanj)
complex(torch.nan, torch.nan) - complex(-torch.inf, torch.inf)  # (nan+nanj)
complex(torch.nan, torch.nan) - complex(-torch.inf, -torch.inf) # (nan+nanj)

complex(torch.nan, torch.nan) * (100.0+0.j)                     # (nan+nanj)
complex(torch.nan, torch.nan) * complex(torch.nan, torch.nan)   # (nan+nanj)
complex(torch.nan, torch.nan) * complex(torch.nan, -torch.nan)  # (nan+nanj)
complex(torch.nan, torch.nan) * complex(-torch.nan, torch.nan)  # (nan+nanj)
complex(torch.nan, torch.nan) * complex(-torch.nan, -torch.nan) # (nan+nanj)
complex(torch.nan, torch.nan) * complex(torch.inf, torch.inf)   # (nan+nanj)
complex(torch.nan, torch.nan) * complex(torch.inf, -torch.inf)  # (nan+nanj)
complex(torch.nan, torch.nan) * complex(-torch.inf, torch.inf)  # (nan+nanj)
complex(torch.nan, torch.nan) * complex(-torch.inf, -torch.inf) # (nan+nanj)

complex(torch.nan, torch.nan) / (100.0+0.j)                     # (nan+nanj)
complex(torch.nan, torch.nan) / complex(torch.nan, torch.nan)   # (nan+nanj)
complex(torch.nan, torch.nan) / complex(torch.nan, -torch.nan)  # (nan+nanj)
complex(torch.nan, torch.nan) / complex(-torch.nan, torch.nan)  # (nan+nanj)
complex(torch.nan, torch.nan) / complex(-torch.nan, -torch.nan) # (nan+nanj)
complex(torch.nan, torch.nan) / complex(torch.inf, torch.inf)   # (nan+nanj)
complex(torch.nan, torch.nan) / complex(torch.inf, -torch.inf)  # (nan+nanj)
complex(torch.nan, torch.nan) / complex(-torch.inf, torch.inf)  # (nan+nanj)
complex(torch.nan, torch.nan) / complex(-torch.inf, -torch.inf) # (nan+nanj)
Enter fullscreen mode Exit fullscreen mode

The arithmetic operations with torch.inf and 100.0 or torch.inf as shown below. *complex type cannot do mod(modulo) calculation with %:

import torch

""" `float` type """
torch.inf + 100.0      # inf
torch.inf + torch.inf  # inf
torch.inf + -torch.inf # nan

torch.inf - 100.0      # inf
torch.inf - torch.inf  # nan
torch.inf - -torch.inf # inf

torch.inf * 100.0      # inf
torch.inf * torch.inf  # inf
torch.inf * -torch.inf # -inf

torch.inf / 100.0      # inf
torch.inf / torch.inf  # nan
torch.inf / -torch.inf # nan

torch.inf % 100.0      # nan
torch.inf % torch.inf  # nan
torch.inf % -torch.inf # nan

""" `complex` type """
complex(torch.inf, torch.inf) + (100.0+0.j)                     # (inf+infj)
complex(torch.inf, torch.inf) + complex(torch.inf, torch.inf)   # (inf+infj)
complex(torch.inf, torch.inf) + complex(torch.inf, -torch.inf)  # (inf+nanj)
complex(torch.inf, torch.inf) + complex(-torch.inf, torch.inf)  # (nan+infj)
complex(torch.inf, torch.inf) + complex(-torch.inf, -torch.inf) # (nan+nanj)

complex(torch.inf, torch.inf) - (100.0+0.j)                     # (inf+infj)
complex(torch.inf, torch.inf) - complex(torch.inf, torch.inf)   # (nan+nanj)
complex(torch.inf, torch.inf) - complex(torch.inf, -torch.inf)  # (nan+infj)
complex(torch.inf, torch.inf) - complex(-torch.inf, torch.inf)  # (inf+nanj)
complex(torch.inf, torch.inf) - complex(-torch.inf, -torch.inf) # (inf+infj)

complex(torch.inf, torch.inf) * (100.0+0.j)                     # (nan+nanj)
complex(torch.inf, torch.inf) * complex(torch.inf, torch.inf)   # (nan+infj)
complex(torch.inf, torch.inf) * complex(torch.inf, -torch.inf)  # (inf+nanj)
complex(torch.inf, torch.inf) * complex(-torch.inf, torch.inf)  # (-inf+nanj)
complex(torch.inf, torch.inf) * complex(-torch.inf, -torch.inf) # (nan-infj)

complex(torch.inf, torch.inf) / (100.0+0.j)                     # (nan+nanj)
complex(torch.inf, torch.inf) / complex(torch.inf, torch.inf)   # (nan+nanj)
complex(torch.inf, torch.inf) / complex(torch.inf, -torch.inf)  # (nan+nanj)
complex(torch.inf, torch.inf) / complex(-torch.inf, torch.inf)  # (nan+nanj)
complex(torch.inf, torch.inf) / complex(-torch.inf, -torch.inf) # (nan+nanj)
Enter fullscreen mode Exit fullscreen mode

Additionally, the arithmetic operations with nan and 100.0, nan or inf with float() and complex() which are Python's built-in functions as shown below:

*Memos:

  • The arithmetic operations with nan are always nan:
  • complex type cannot do mod(modulo) calculation with %.
import torch

""" `float` type """
float('nan') + 100.0         # nan
float('nan') + float('nan')  # nan
float('nan') + float('-nan') # nan
float('nan') + float('inf')  # nan
float('nan') + float('-inf') # nan

float('nan') - 100.0         # nan
float('nan') - float('nan')  # nan
float('nan') - float('-nan') # nan
float('nan') - float('inf')  # nan
float('nan') - float('-inf') # nan

float('nan') * 100.0         # nan
float('nan') * float('nan')  # nan
float('nan') * float('-nan') # nan
float('nan') * float('inf')  # nan
float('nan') * float('-inf') # nan

float('nan') / 100.0         # nan
float('nan') / float('nan')  # nan
float('nan') / float('-nan') # nan
float('nan') / float('inf')  # nan
float('nan') / float('-inf') # nan

float('nan') % 100.0         # nan
float('nan') % float('nan')  # nan
float('nan') % float('-nan') # nan
float('nan') % float('inf')  # nan
float('nan') % float('-inf') # nan

""" `complex` type """
complex('nan+nanj') + (100.0+0.j)          # (nan+nanj)
complex('nan+nanj') + complex('nan+nanj')  # (nan+nanj)
complex('nan+nanj') + complex('nan-nanj')  # (nan+nanj)
complex('nan+nanj') + complex('-nan+nanj') # (nan+nanj)
complex('nan+nanj') + complex('-nan-nanj') # (nan+nanj)
complex('nan+nanj') + complex('inf+infj')  # (nan+nanj)
complex('nan+nanj') + complex('inf-infj')  # (nan+nanj)
complex('nan+nanj') + complex('-inf+infj') # (nan+nanj)
complex('nan+nanj') + complex('-inf-infj') # (nan+nanj)

complex('nan+nanj') - (100.0+0.j)          # (nan+nanj)
complex('nan+nanj') - complex('nan+nanj')  # (nan+nanj)
complex('nan+nanj') - complex('nan-nanj')  # (nan+nanj)
complex('nan+nanj') - complex('-nan+nanj') # (nan+nanj)
complex('nan+nanj') - complex('-nan-nanj') # (nan+nanj)
complex('nan+nanj') - complex('inf+infj')  # (nan+nanj)
complex('nan+nanj') - complex('inf-infj')  # (nan+nanj)
complex('nan+nanj') - complex('-inf+infj') # (nan+nanj)
complex('nan+nanj') - complex('-inf-infj') # (nan+nanj)

complex('nan+nanj') * (100.0+0.j)          # (nan+nanj)
complex('nan+nanj') * complex('nan+nanj')  # (nan+nanj)
complex('nan+nanj') * complex('nan-nanj')  # (nan+nanj)
complex('nan+nanj') * complex('-nan+nanj') # (nan+nanj)
complex('nan+nanj') * complex('-nan-nanj') # (nan+nanj)
complex('nan+nanj') * complex('inf+infj')  # (nan+nanj)
complex('nan+nanj') * complex('inf-infj')  # (nan+nanj)
complex('nan+nanj') * complex('-inf+infj') # (nan+nanj)
complex('nan+nanj') * complex('-inf-infj') # (nan+nanj)

complex('nan+nanj') / (100.0+0.j)          # (nan+nanj)
complex('nan+nanj') / complex('nan+nanj')  # (nan+nanj)
complex('nan+nanj') / complex('nan-nanj')  # (nan+nanj)
complex('nan+nanj') / complex('-nan+nanj') # (nan+nanj)
complex('nan+nanj') / complex('-nan-nanj') # (nan+nanj)
complex('nan+nanj') / complex('inf+infj')  # (nan+nanj)
complex('nan+nanj') / complex('inf-infj')  # (nan+nanj)
complex('nan+nanj') / complex('-inf+infj') # (nan+nanj)
complex('nan+nanj') / complex('-inf-infj') # (nan+nanj)
Enter fullscreen mode Exit fullscreen mode

Additionally, the arithmetic operations with inf and 100.0 or inf with float() and complex() which are Python's built-in functions as shown below. *complex type cannot do mod(modulo) calculation with %:

import torch

""" `float` type """
float('inf') + 100.0         # inf
float('inf') + float('inf')  # inf
float('inf') + float('-inf') # nan

float('inf') - 100.0         # inf
float('inf') - float('inf')  # nan
float('inf') - float('-inf') # inf

float('inf') * 100.0         # inf
float('inf') * float('inf')  # inf
float('inf') * float('-inf') # -inf

float('inf') / 100.0         # inf
float('inf') / float('inf')  # nan
float('inf') / float('-inf') # nan

float('inf') % 100.0         # nan
float('inf') % float('inf')  # nan
float('inf') % float('-inf') # nan

""" `complex` type """
complex('inf+infj') + (100.0+0.j)          # (inf+infj)
complex('inf+infj') + complex('inf+infj')  # (inf+infj)
complex('inf+infj') + complex('inf-infj')  # (inf+nanj)
complex('inf+infj') + complex('-inf+infj') # (nan+infj)
complex('inf+infj') + complex('-inf-infj') # (nan+nanj)

complex('inf+infj') - (100.0+0.j)          # (inf+infj)
complex('inf+infj') - complex('inf+infj')  # (nan+nanj)
complex('inf+infj') - complex('inf-infj')  # (nan+infj)
complex('inf+infj') - complex('-inf+infj') # (inf+nanj)
complex('inf+infj') - complex('-inf-infj') # (inf+infj)

complex('inf+infj') * (100.0+0.j)          # (nan+nanj)
complex('inf+infj') * complex('inf+infj')  # (nan+infj)
complex('inf+infj') * complex('inf-infj')  # (inf+nanj)
complex('inf+infj') * complex('-inf+infj') # (-inf+nanj)
complex('inf+infj') * complex('-inf-infj') # (nan-infj)

complex('inf+infj') / (100.0+0.j)          # (nan+nanj)
complex('inf+infj') / complex('inf+infj')  # (nan+nanj)
complex('inf+infj') / complex('inf-infj')  # (nan+nanj)
complex('inf+infj') / complex('-inf+infj') # (nan+nanj)
complex('inf+infj') / complex('-inf-infj') # (nan+nanj)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)