0

I want to encode solar mass as a std::ratio (in order to convert it to kg). How can I do such a thing if solar mass is 1.988478e+30 kg?

std::ratio<(unsigned long long)1'988'478e+30, 1> kg_to_solar_mass // overflow

1 Answers1

0

A 64 bit unsigned integer (what the typical machine uses for a unsigned long long) has a max value of 18446744073709551615 (1.8446744073709551615e+19). That is orders of magnitude smaller then a 1'988'478e+39 (or even 1.989e+30) and is going to overflow the integer.

You're either going to have to switch to a different type that uses doubles or some "Big Int" library, or not specify a ratio for a single kilogram to a solar mass.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402