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 1 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?
TAMILMARAN said:
1 decade ago
It's simple program:
Check out value of a = 25.4.
Variables acceptance = 16, 387.064 result functioning.
Check out value of a = 25.4.
Variables acceptance = 16, 387.064 result functioning.
Tamilmaran c said:
1 decade ago
Dear sir,
Please find the statement this answer would be possible to update such this.
printf ("%f\n", a+a+a);
a+a+a it seems meaning less.
Right answer false (B).
Please find the statement this answer would be possible to update such this.
printf ("%f\n", a+a+a);
a+a+a it seems meaning less.
Right answer false (B).
Rupesh sahu said:
1 decade ago
It's wrong you can put and checkout the value of a = 25.4.
Then 25.4+25.4+25.4 = 76.2.
And 25.4*25.4*25.4 = 16, 387.064.
Then 25.4+25.4+25.4 = 76.2.
And 25.4*25.4*25.4 = 16, 387.064.
Vishnuprabhu said:
1 decade ago
But will it be applicable for all the float values?
Sonerao said:
1 decade ago
But as per my info Float calculation is not same because both Addition and Multiplication Fractions Values are Different so How is it correct.
Rob said:
1 decade ago
The wording on this question is not clear. The way it is written it is asking if the printf function will print the same values regardless of what the user entered for input. This is not the case here.
ie. If the user enters:
3.0
then the program will output:
9.00000
9.00000
but if the user enters:
4.0
then the output will not be 9.00000 it will be 12.00000.
Thus the answer would be No based on the fact that a change in the user input changed the output.
ie. If the user enters:
3.0
then the program will output:
9.00000
9.00000
but if the user enters:
4.0
then the output will not be 9.00000 it will be 12.00000.
Thus the answer would be No based on the fact that a change in the user input changed the output.
(1)
Ranjana said:
1 decade ago
As we know 3+3+3=9 same trick is used in this Eg: a=4.0 then first printf will print 12.0 and 2nd 3*4.0 i.e 12 you can do it with any value.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers