Arithmetic operations with `nan` and `inf` in PyTorch

Buy Me a Coffee☕

*My post explains how to create nan and inf in PyTorch.

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

*Memos:

Arithmetic operations with nan are always nan:

complex type cannot do mod(modu…


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)

Buy Me a Coffee

*My post explains how to create nan and inf in PyTorch.

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

*Memos:

  • 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)

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)

Additionally, 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:

  • 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)

Additionally, 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)


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)


Print Share Comment Cite Upload Translate Updates
APA

Super Kai (Kazuya Ito) | Sciencx (2024-07-22T01:03:17+00:00) Arithmetic operations with `nan` and `inf` in PyTorch. Retrieved from https://www.scien.cx/2024/07/22/arithmetic-operations-with-nan-and-inf-in-pytorch/

MLA
" » Arithmetic operations with `nan` and `inf` in PyTorch." Super Kai (Kazuya Ito) | Sciencx - Monday July 22, 2024, https://www.scien.cx/2024/07/22/arithmetic-operations-with-nan-and-inf-in-pytorch/
HARVARD
Super Kai (Kazuya Ito) | Sciencx Monday July 22, 2024 » Arithmetic operations with `nan` and `inf` in PyTorch., viewed ,<https://www.scien.cx/2024/07/22/arithmetic-operations-with-nan-and-inf-in-pytorch/>
VANCOUVER
Super Kai (Kazuya Ito) | Sciencx - » Arithmetic operations with `nan` and `inf` in PyTorch. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/22/arithmetic-operations-with-nan-and-inf-in-pytorch/
CHICAGO
" » Arithmetic operations with `nan` and `inf` in PyTorch." Super Kai (Kazuya Ito) | Sciencx - Accessed . https://www.scien.cx/2024/07/22/arithmetic-operations-with-nan-and-inf-in-pytorch/
IEEE
" » Arithmetic operations with `nan` and `inf` in PyTorch." Super Kai (Kazuya Ito) | Sciencx [Online]. Available: https://www.scien.cx/2024/07/22/arithmetic-operations-with-nan-and-inf-in-pytorch/. [Accessed: ]
rf:citation
» Arithmetic operations with `nan` and `inf` in PyTorch | Super Kai (Kazuya Ito) | Sciencx | https://www.scien.cx/2024/07/22/arithmetic-operations-with-nan-and-inf-in-pytorch/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.