Why pi value is not displayed accurately? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why pi value is not displayed accurately?

https://code.sololearn.com/cvkXqi2Xqdn4/?ref=app Why my output is just 3.000000 when it should be 3.14...

5th Jul 2021, 4:14 PM
Srinath
8 Answers
+ 3
Verstappen Because operation happens from right to left. So first it will be divide then assign to double variable. You can also do this double a=(double)22/7;
5th Jul 2021, 4:24 PM
A͢J
A͢J - avatar
+ 2
Because you are dividing two integers (22 and 7) So, the result will be an integer. try 22.0/7 or 22/7.0 or 22.0/7.0
5th Jul 2021, 4:18 PM
zak00aria
zak00aria - avatar
+ 2
in the C language if you divide numbers the type of the result will be the type with max memory, so: int/int = int double/int =double .... so in your case, double a=22/7; you just convert an int into a double.
5th Jul 2021, 4:34 PM
zak00aria
zak00aria - avatar
+ 1
zak aria thanks! But why does this happen? I declared the variable as double , so isn't 22/7 supposed to be taken as a double value ?
5th Jul 2021, 4:22 PM
Srinath
+ 1
I ᴀᴍ "Tɪᴍᴇ" Thanks now I get it
5th Jul 2021, 4:40 PM
Srinath
+ 1
zak aria ohh, but what it i divide float/int, as they both have same size, will the answer be assigned as int or float?
5th Jul 2021, 4:41 PM
Srinath
+ 1
it will be double, why...? I don't know hhhh
5th Jul 2021, 4:46 PM
zak00aria
zak00aria - avatar
+ 1
zak aria, Verstappen implicit conversion isn't only about size in memory, you can see the documentation here: https://en.cppreference.com/w/c/language/conversion
7th Jul 2021, 1:43 AM
Angelo
Angelo - avatar