0

double in cpp

https://code.sololearn.com/coaM9cpWltrq/?ref=app Why ìn cpp: double b=9/7; //its always 1; cout<<b; But double b=9; //its accurate 1.28571428 cout<<b/7; Both are DOUBLE With Respect

7th Dec 2021, 4:25 PM
‎میم شین‎
7 Answers
+ 3
‎میم شین‎ 9 and 7 are int not double. You need to cast one of them with double to get in decimal: double b = (double) 9 / 7; In second case b is double so 9 will be treated as double.
7th Dec 2021, 4:28 PM
A͢J
A͢J - avatar
+ 3
You could also use double b = 9.0/7.0;
7th Dec 2021, 4:38 PM
cadbrooke
cadbrooke - avatar
7th Dec 2021, 4:29 PM
‎میم شین‎
+ 2
‎میم شین‎ In Java too you will get same result but 1.0 instead of 1
7th Dec 2021, 4:38 PM
A͢J
A͢J - avatar
+ 2
You could also use double b = 9.0/7; or double b = 9/7.0;
7th Dec 2021, 5:24 PM
SoloProg
SoloProg - avatar
+ 2
Because division of two integers is always an integer even if you declare it as double or float.
8th Dec 2021, 9:44 AM
Abhishek Pandey
Abhishek Pandey - avatar
+ 2
Use this instead, double = 9.0 / 7.0
8th Dec 2021, 9:45 AM
Abhishek Pandey
Abhishek Pandey - avatar