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.
Related
This question already has answers here:
Closed 10 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.
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;
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;
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.
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.