Floating point exception?

Hey all, brand new with C++ and just got back from my second day at class. :)

I'm able to successfully compile and run this program, but I am having a runtime error when it gets to a certain point.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
using namespace std;

int main()
        {
                int pone;
                int ptwo;
                int ptotal;
                int vtotal;

                cout << "\n\nHello. This program will help you decide"
                        " how much of each kind of medicine you need\n for your"
                        " patients.\n\n";
                cout << "What percent concentration are you looking for? ";
                cin >> ptotal;
                cout << "What percent concentration is solution A? ";
                cin >> pone;
                cout << "What percent concentration is solution B? ";
                cin >> ptwo;
                cout << "What total volume do you need for the final solution, in mL? ";
                cin >> vtotal;

                long double vone;
                long double  vtwo;
                vone = (vtotal*((ptotal/100)-(ptwo/100)))/((pone/100)-(ptwo/100));
                vtwo = (vtotal-vone);

                cout << "\n\nYou require ";
                cout << vone;
                cout << " ml of Solution A, and\n ";
                cout << vtwo;
                cout << " ml of Solution B.";


        return 0;
        }


The program is designed to tell the user how much of two varying concentrations of solution are needed to produce a certain volume of a third solution. vone, vtwo, and vtotal are all representing volume, and pone, ptwo, and ptotal represent the percent concentration of each solution. I'm sure my math is right, but I keep getting this error. I've searched on google a few times to find the answer to this and most people seem to say that I'm either declaring the variables incorrectly (I've tried both int and long double) or that I'm dividing by zero or something like that. Unfortunately I wasn't able to derive a solution from others' problems; can anyone help me with what I'm doing wrong? Much appreciated :)
This:

1
2
int pone = 9;
double answer = pone/100;


does integer division. answer will be zero in this case. Actually, answer will be zero if pone is in the range [0,99]. For integer division, imagine you're doing long division but you don't continue to solve for the decimal values. You will get an answer and a remainder and integer division returns the answer. For example, 6/3 would give 2 with remainder 0, 5/3 would give 1 with remainder 2, and 1/3 would give 0 with remainder 1. The modulus operator, %, would give you the remainder of the integer division between the operands.

Thus, if pone and ptwo are less than 100, you are indeed dividing by zero. You need to cast the operands to double (or float or whatever you want) to tell C++ to treat the division as a non-integer division.

e.g.

vone = (vtotal*(((double)ptotal/(double)100)-((double)ptwo/(double)100)))/(((double)pone/(double)100)-((double)ptwo/(double)100));
This:

1
2
int pone = 9;
double answer = pone/100;


does integer division. answer will be zero in this case. Actually, answer will be zero if pone is in the range [0,99]. For integer division, imagine you're doing long division but you don't continue to solve for the decimal values. You will get an answer and a remainder and integer division returns the answer. For example, 6/3 would give 2 with remainder 0, 5/3 would give 1 with remainder 2, and 1/3 would give 0 with remainder 1. The modulus operator, %, would give you the remainder of the integer division between the operands.

Thus, if pone and ptwo are less than 100, you are indeed dividing by zero. You need to cast the operands to double (or float or whatever you want) to tell C++ to treat the division as a non-integer division.

e.g.

vone = (vtotal*(((double)ptotal/(double)100)-((double)ptwo/(double)100)))/(((double)pone/(double)100)-((double)ptwo/(double)100));


While I don't mean to be rude, I can't help but say....

WOOOOOSH

lol. I added in "double" where you said, and I got the program to work! I just don't understand what I did. At all. :P

I've only been in class for two days, if it's not too much trouble, would you mind dumbing down what you said a bit more? Again, sorry for the trouble! I really appreciate it though. :)
When you use the divide operator, "/", in C++, it assumes the numerator and denominator are both of type int, unless it has more information to go by.

e.g.

1
2
int x = 2;
double answer = x/5;


C++ sees "x/5", and goes, "'x', let's see...Oh yeah, 'x' is an int. OK, '5'...Um I don't know what type you are so I'm just going to assume you're an int."

e.g.

1
2
float x = 2;
double answer = x/(float)5;


C++ sees "x/(float)5", and goes, "'x', let's see...Oh yeah, 'x' is a float. OK, '(float)5'...OK then, you want me to make '5' a float, huh. Gotcha".

When C++ thinks (or knows) that both the numerator and denominator are ints, then this is bad news if you want decimal values! Basically, it gets rid of everything after the decimal point.

e.g. 1/3 is 0.333333....But without telling C++ to make at least one of the numerator or denominator a float or double, it'll give you 0. 5/4 is 1.25 but without telling C++ to make at least one of the numerator or denominator a float or double, it'll give you 1.
Topic archived. No new replies allowed.