Division in C++ [duplicate] - c++

This question already has an answer here:
Integer division always zero [duplicate]
(1 answer)
Closed 6 years ago.
I am new to C++ and I tried this simple code:
#include<iostream>
#include<math.h>
using namespace std;
int main(){
double a;
a=1/6;
cout<<a;
}
But the result is 0. As I understood, double should work with real numbers, so shouldn't the result be 1/6 or 0.1666666? Thank you!

In the expression 1 / 6, both numbers are integers. This means that this division will perform integer division, which results in 0. To do a double division, one number has to be a double: 1.0 / 6 for example.

Integer literals 1 and 6 have type int. Thus in the expression
1/6
there is used the integer arithmetic and the result is equal to 0.
Use at least one of the operands as a floating literal. For example
a = 1.0/6;
or
a = 1/6.0;
or
a = 1.0/6.0;

Related

Issue with power function? [duplicate]

This question already has an answer here:
Integer division always zero [duplicate]
(1 answer)
Closed 6 years ago.
I am new to C++ and I tried this simple code:
#include<iostream>
#include<math.h>
using namespace std;
int main(){
double a;
a=1/6;
cout<<a;
}
But the result is 0. As I understood, double should work with real numbers, so shouldn't the result be 1/6 or 0.1666666? Thank you!
In the expression 1 / 6, both numbers are integers. This means that this division will perform integer division, which results in 0. To do a double division, one number has to be a double: 1.0 / 6 for example.
Integer literals 1 and 6 have type int. Thus in the expression
1/6
there is used the integer arithmetic and the result is equal to 0.
Use at least one of the operands as a floating literal. For example
a = 1.0/6;
or
a = 1/6.0;
or
a = 1.0/6.0;

I'm trying to do an equation in C++ but it keeps outputting 1 [duplicate]

This question already has an answer here:
Integer division always zero [duplicate]
(1 answer)
Closed 3 years ago.
As the title says, this code is meant to calculate the probability of 2 people having the same birthday in a group of 5 but it just outputs 1, I'm fairly new to C++ so any help would be appreciated.
#include <iostream>
using namespace std;
int main(){
float p;
p=1-(364/365)*(363/365)*(362/365)*(361/365);
cout<<p;
}
Put a .0 on each number, that way is treated as a double instead of an integer. Integer division (364/365) equals 0
p=1.0-(364.0/365.0)*(363.0/365.0)*(362.0/365.0)*(361.0/365.0);
This is because after calculation 364/365 the calculates answer is an integer which is 0.
To make it work change it like this.
p=1-(364/365.0)*(363/365.0)*(362/365.0)*(361/365.0);
You need to cast the integers to floats as / rounds to the largest integer below the result when both types are int:
p=1-(float(364)/float(365))*(float(363)/float(365))*(float(362)/float(365))*(float(361)/float(365));

Converting double to int is giving unexpected results [duplicate]

This question already has answers here:
Conversion from double to integer [duplicate]
(2 answers)
Closed 4 years ago.
I was trying to find cube root of a perfect cube using pow function but the answers were unexpected (3375 = 15^3)
#include <bits/stdc++.h>
using namespace std;
int main()
{
double cb = pow(3375, 1.0/3.0);
printf("cb(in double) = %lf\n", cb);
printf("cb(in int) = %d\n",(int)cb);
return 0;
}
the output it shows is :
cb(in double) = 15.000000
cb(in int) = 14
After discussing it with people it turned out that the code ,
printf("%0.0lf", 14.0/3);
gives output
5
I am not getting why it's happening, and if it's due to precision in storing double and rounding off, then it should round it down to a smaller value rather than rounding to a greater value.
TL;DR: (int)double always rounds towards zero. printf("%0.0lf", double) actually rounds to closest integer.
When you convert the double to int, then the result is rounded towards zero. E.g. (int)14.9999 is 14, not 15. Therefore your first example with pow() must have given you the result slightly below 15, therefore direct cast to int gives 14.
Now, when you use printf(), then other rules are used. Printf, when asked to print a double using smallest number of digits possible (%0.0lf) rounds it to the closest integer, therefore printf("%0.0lf", 14.666) prints 15.

Why double a=(double)('z'/'A') gives only integer part in C++? [duplicate]

This question already has answers here:
Why does division result in zero instead of a decimal?
(5 answers)
Closed 5 years ago.
I'm still a beginner in C++ so I seek some help with the basics.
Here, in the following code, I'm using type-casting to find value of 122/65 but I'm getting only the integer part even with double data type.
#include <iostream>
using namespace std;
int main()
{
double a=(double)('z'/'A');
cout<<a;
return 0;
}
Can someone provide me a good reason for this??
Thank you.
You make an integer division and then you typecast the result to double. Basically you have:
(double) (122/65) = (double) (1) = 1.0
^ truncated -> integer division
If you want a floating point division you can do it this way:
double a = (double)'z' / (double)'A';
// a = 122.0 / 65.0

Newbie trying to work out C++ [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
C programming division
probably my question is very simple and stupid.
I would like to store the value of a division, in particular 1 / x where x is a
integer value.
int x = 17;
double result = 1/x;
I try to do it but I always get 0.000000 ... I try to enter a value fixed in x, for example 1/17 but always get the same value .. what's Wrong?
You are doing integer division.
Try the following and it will work as expected:
int x = 17;
double result = 1.0 / x;
The type of the 1 in the expression you have above is int, and the type of x is int. When you do int / int, you get an int back. You need at least one of the types involved to be floating point (float or double) in order for floating point division to occur.
Unlike in Mathematics, division in C++ can either refer to truncated integer division (what you did) or floating point division (what I did in my example). Be careful of this!
In my example, explicitly what we have is double / int -> double.