Issue with power function? [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

Turning an unsigned integer into a double from 0 to 1. Dividing by integer limit not working [duplicate]

This question already has an answer here:
Integer division always zero [duplicate]
(1 answer)
Closed 10 months ago.
So I'm wanting to turn an unsigned integer (Fairly large one, often above half of the unsigned integer limit) into a double that shows how far it is between 0 and the unsigned integer limit. Problem is, dividing it by the unsigned integer limit is always returning 0. Example:
#include <iostream>
;
int main()
{
uint64_t a = 11446744073709551615
double b = a / 18446744073709551615;
std::cout << b;
};
This always returns 0. Is there an alternative method or a way to fix this one?
If it means anything, I'm using GCC with the -O3 optimisation flag.
You have to convert the expression on the right to double, for example, like this:
double b = static_cast<double>(a) / 18446744073709551615;
or
double b = a / 18446744073709551615.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

Division in C++ [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;