How to calculate a/b in C++? [duplicate] - c++

This question already has answers here:
Why does dividing two int not yield the right value when assigned to double?
(10 answers)
Closed 6 years ago.
I am trying to get value for a/b, but I always get '0' for a/b.
Why do I get a/b=0 ?
#include <iostream>
using namespace std;
int main()
{
int a,b;
cout << "Give a and b" << endl;
cin >> a >> b;
double q=a/b;
cout << "a/b=" << q;
return 0;
}

You are using integer arithmetic, so the division result will be an integer. A floating point value that is between 0 and 1 will get truncated to 0 when interpretted as an integer, before it is assigned to your q variable. So either:
change your a and b variables to double instead of int
typecast a and/or b to double during the division.
Either way, you will then be performing floating-point division instead of integer division, so the result will be a floating-point value.

Can do double q=double(a)/b; instead.

Related

Arithmetic Operation in C++

#include"iostream"
using namespace std;
int main(){
float arithmetic_operation = (4+5)+9*2-4+2/5+1-13;
cout<< arithmetic_operation << " <--The Result." << endl;
return 0;
}
I am getting 11 <--The Result. But actually the result is 11.4, Can someone please help me to understand the point please.
You are doing integer arithmetic. All operands are integers, which meand all operations will be done using integer operations. And for integer division 2/5 is equal to zero.
Use floating point value all over instead:
double arithmetic_operation = (4.+5.)+9.*2.-4.+2./5.+1.-13.;

Arithmetic expression is 0 instead of 1 [duplicate]

This question already has an answer here:
Integer division always zero [duplicate]
(1 answer)
Closed 7 years ago.
#include <iostream>
using namespace std;
int main() {
int a,b,c;
cin >> a >> b >> c;
cout << a/b*c;
return 0;
}
For a=5 b=10 c=2 the output gives 0 which is obviously wrong.
As far as I understood << / * are binary operators / and * "bond" stronger than << and for / and * it is calculated from the left to right so first 5 is devided by 10, then the result (0.5) is multiplicated with 2 which is 1 and that is delivered by << to cout.
So can anyone give me an explanation for that result (0)?
This is the integer division issue: if a==5 and b==10, then a/b==0.
See the standard, Multiplicative operators [expr.mul]
For
integral operands the / operator yields the algebraic quotient with any fractional part discarded
This is the result of integer rounding. When integer division is performed any fractional remainder is simply cut off. So 2 / 3 == 0.
To keep the results as accurate convert use doubles or convert the variables to doubles in the expression. So something like this
static_cast<int>(static_cast<double>(a) / static_cast<double>(b) * static_cast<double>(c))
will result in the correct value.
No its not wrong
Since all variables are integers.
5/10 is 0 in the integer world and that multiplied with 2 is still 0
9/10 is also 0 but 11/10 is 1
if you want something useful from this declare the variables as double instead or IOW:
int main() {
double a,b,c;
cin >> a >> b >> c;
cout << a/b*c;
return 0;
}

How to display a floating point result of a division of two integers? [duplicate]

This question already has answers here:
Dividing 1/n always returns 0.0 [duplicate]
(3 answers)
Closed 8 years ago.
I have a problem with displaying and keeping any floating number or double or long or floating type this is the code :
int sd, n;
float d;
d=n/sd.capacity();
cout << d.toFloat() << endl;
cout << n << " / " << sd.capacity() << " = " << d << endl;
In the output I have d equal to 0 everytime, sd.capacity() and n are never 0, so all the time n is lower than sd.capacity value, d = 0 but it should never 0. The variable d does not contain anything but 0.
int/int results into int. So this does a problem.
You need to have atleast one of the operands as float.
Since, you are telling that n always < than sd.capacity(), so n/sd.capacity() in int division always gives 0.
I suppose you should do,
d= (float)n / (float)sd.capacity();
Does this solve it?

Can't get math c++ program to work [duplicate]

This question already has answers here:
Why does division result in zero instead of a decimal?
(5 answers)
Closed 8 years ago.
So i am trying to make one of my first very simple math programs in c++. the problem is that
i can't seem to get the function potodds to do what i want. There is no problem in getting it to multiple the two variables (x,y) that works perfectly fine. the problem occurs when i try to replace return x*y*100 with return (x/y)*100. in this case it always return the value 0?
Hopes that someone out there can help me pinpoint my mistake.
The code looks like this:
#include "stdafx.h"
#include <iostream>
int x;
int y;
int potodds(int x, int y) {
return x * y * 100; //(x/y)*100;
}
int main() {
using namespace std;
cout << "what's the size of the pot?" << endl;
cin >> y;
cout << "what's the size of the bet?" << endl;
cin >> x;
cout << "your potodds are:" << endl;
cout << potodds(x, y) << endl;
return 0;
}
Thanks to Ebyrob i got the solution.
the problem that I was having was that I was trying to divide an integer, that was assigned a decimal value and by definition an integer can only contain whole numbers. The result was that the integer was rounded down to zero.
x and y are defined as integral values. The integral division returns only the quotient. So if x is less than y then x /y will be equal to 0.
So it would be better to substitute expression ( x /y ) * 100 for ( 100 * x ) / y
Otherwise use float numbers
For example
( double )x / y * 100
Changing int to double should solve the problem but also "using namespace std;" is always outside of main when I program. I don't know if this affects anything but you might consider putting it above your main function because it might be causing a problem now or it might cause a problem in another program you make. I think it is a matter of it being global or private but I would have it outside of main so that any other functions you use can use it ( assuming I'm right ).
If you divide two integers, the result will be an integer (the quotient: the remainder is discarded).
So, 1 / 2 -> 0 instead of 0.5.
Note also that (1 / 2) * 100 is therefore 0 * 100 -> 0, while, as Vlad says,
100*1/2 -> 100/2 -> 50, which is what you want.

Dividing two integers to produce a float result [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why can't I return a double from two ints being divided
My C++ program is truncating the output of my integer devision even when I try and place the output into a float. How can I prevent this whilst keeping those to variables (a & b) as integers?
user#box:~/c/precision$ cat precision.cpp
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int a = 10, b = 3;
float ans = (a/b);
cout<<fixed<<setprecision(3);
cout << (a/b) << endl;
cout << ans << endl;
return 0;
}
user#box:~/c/precision$ g++ -o precision precision.cpp
user#box:~/c/precision$ ./precision
3
3.000
Cast the operands to floats:
float ans = (float)a / (float)b;