C Programming - Floating Point Issues - Discussion
Discussion Forum : Floating Point Issues - Yes / No Questions (Q.No. 1)
1.
Will the printf() statement print the same values for any values of a?
#include<stdio.h>
int main()
{
float a;
scanf("%f", &a);
printf("%f\n", a+a+a);
printf("%f\n", 3*a);
return 0;
}
Discussion:
13 comments Page 2 of 2.
Kelvin desai said:
9 years ago
@Rupesh Sahu.
It is 25.4 * 3 not 25.4 * 25.4 * 25.4.
It is 25.4 * 3 not 25.4 * 25.4 * 25.4.
Kelvin desai said:
9 years ago
@Tamilmaran, it is showing correct only, check in terminal.
float f = 25.4;
printf("%f %f\n",f+f+f,f*3);
Output: 76.199999 76.199999
float f = 25.4;
printf("%f %f\n",f+f+f,f*3);
Output: 76.199999 76.199999
Ganesh Raj said:
9 years ago
It is not a^3 here. It's just 3*a. Then, how comes 387.064?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers