'OverflowError in Python (numerical result out of range)

so I'm supposed to calculate masses (and some other values) of binary stars for my astronomical course via dynamical parallax. The problem is the numbers I work with are too big for the program to handle, hence I'm getting an OverflowError. Is there any way to work around this? I'm quite new at this so any help would be appreciated :D

import random
import math
import numpy as np

T = 95 #years
G = 6.67*10**-11
Ms = 1.98*10**30 #mass of the sun
Ls = 3.83*10**26 #sun luminosity
rel_m1 = 3.9
rel_m2 = 5.3
Ms2 = random.randrange(3.98*10**30, 4*10**30) #total mass guess

a = np.cbrt((T**2*G*(Ms2)/(4*math.pi**2))) #in au
a_arcsec = a*3*10**16 #in arcseconds

d = 1/a_arcsec

abs_m1 = rel_m1 + 5*(1-math.log10(d))
abs_m2 = rel_m2 + 5*(1-math.log10(d))

lum1 = Ls*pow(10,((Ms-abs_m1)/2.5))
lum2 = Ls*pow(10,((Ms-abs_m2)/2.5))

mass1 = ((lum1/Ls)**(1/3.5))*Ms
mass2 = ((lum2/Ls)**(1/3.5))*Ms

Mt = mass1 + mass2

print(d, "pc")
print(abs_m1)
print(abs_m2)
print(mass1)
print(mass2)
print(Mt)

This is the error I'm getting

Traceback (most recent call last):
  File "/home/v3ctr0n/Dokumenty/astro/iterations.py", line 28, in <module>
    lum1 = Ls*pow(10,((Ms-abs_m1)/2.5))
OverflowError: (34, 'Numerical result out of range')


Solution 1:[1]

I'm not an expert either, but have't you thought about saving your large numbers in string format? sure, your program will occupy more memory, but surely you will have precise calculations without worrying about any OverflowErrors.

Once I stepped in this video on YouTube that I found interesting and maybe can solve your problem

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 donnynja