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;
}
Yes
No
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
13 comments Page 1 of 2.

Kalai said:   1 decade ago
According to the ans...

Consider a=0.1.. scanf func reads value of a
then a+a+a=0.1+0.1+0.1=0.3
next 3*0.1=0.3

So it prints the same value.

Seifdean said:   1 decade ago
In that case a is not initialized so it will take an arbitrary value not 0, other thing &a is the address of a how can have the same value of 3*a is this coincidence ?

Ranjana said:   1 decade ago
It will not print any value for the first printf statement and for 2nd and third it assumes the value0.0000 because no declaration or input is value is given of a so it print 0.0000 both time.

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.

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.
(1)

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.

Vishnuprabhu said:   1 decade ago
But will it be applicable for all the float values?

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.

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).

TAMILMARAN said:   1 decade ago
It's simple program:

Check out value of a = 25.4.

Variables acceptance = 16, 387.064 result functioning.


Post your comments here:

Your comments will be displayed after verification.