0

Been learning C++ in a class, but for one of the exercises I've been getting a [Warning] Integer Overflow in [-Woverflow], what's causing the problem and how do I fix it? (The program is meant to calculate the output of complete matter->energy transformation in Joules, and here it is)

#include <iostream>
using namespace std;

int main()
{
    long antimass = 0;
    //long amosus = 0;
    cout<< "Breakdown, M\n 3-23\n Exersize 5\n";
    cout<< "Enter The Mass of your antimatter (in grams)!\n";
    cin >> antimass;
    //amosus = antimass%1000;
    cout<< "output in joules = "<<   (antimass%1000*(299792458*299792458))<<endl;

}
Marek R
  • 32,568
  • 6
  • 55
  • 140
  • Use `unsigned long long` or `ull` – Jason Sep 26 '22 at 15:56
  • Are you asking what "integer overflow" is? – Drew Dormann Sep 26 '22 at 15:56
  • 6
    It means `299792458*299792458` has a value larger than can be stored in the type of the expression. Given the context you probably should be using floating point here, i.e. use `double` instead of `long` and add `.0` or just `.` after the constants to mark them as floating point literals instead of integer literals. – user17732522 Sep 26 '22 at 15:57
  • `299792458*299792458` is an operation using `int` values with an `int` result. `int` is typically a 32-bit integer, with a range up to about 2 billion. – Some programmer dude Sep 26 '22 at 15:57
  • 2
    What is `antimass%1000` supposed to mean physically btw? – user17732522 Sep 26 '22 at 15:58
  • did that so you could more easily enter in grams instead of Kilograms, user17732522. I'm new to programming and have no clue what I'm doing – Mental-Breakdown Sep 26 '22 at 15:59
  • @Mental-Breakdown What do you expect `%` to do? It is not the division operator. Division is `/`. – user17732522 Sep 26 '22 at 16:00
  • 2
    It would be best to learn the basics of the programming language by going through a proper course on C++ first, e.g. one of the [recommended books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) would be a good choice. (Assuming your current course isn't already a course focused on C++. In my experience physics programming courses are usually not focused on teaching the language, but rather on the numerical aspects of physics.) – user17732522 Sep 26 '22 at 16:01
  • Recalculating units usually involves floating points not integers. Also why there is modulo operator? In context of converting units it doesn't make seance. – Marek R Sep 26 '22 at 16:04
  • 1
    I'm a bit critical about floating point – how much precision do you rely on? Floating point can cover very large numbers, but not (all) precisely (53 bit precision with IEEE754 double precision, which is what C++ `double` usually maps to). If you need large numbers precisely then some big integer library might be better choice. – Aconcagua Sep 26 '22 at 16:06

1 Answers1

0

Thanks to user17732522 for figuring out what was wrong! I replaced the pesky Long (line 6) with Double and replaced the % (line 12) with / and that fixed it!

user17732522's original post: "It means 299792458*299792458 has a value larger than can be stored in the type of the expression. Given the context you probably should be using floating point here, i.e. use double instead of long and add .0 or just . after the constants to mark them as floating point literals instead of integer literals."