+ 6
Can someone explain this concept.
14 Respuestas
+ 1
change printf("%f\n%f\n",x,y) to printf("%.15f\n%.15f\n",x,y) you will see the output of float is different from the out put of double because the size of float is
smaller than size of double(32 bit and 64 bit)
+ 6
Harsh
First of all, I have copied your program and saved it to my code as private if verifications are required futher.
The problem here is you have declared x as double and y as float either declare both as double or declare both float.
Reason :- You need to declare datatype of both variable either as double or float because if you declare x as double and y as float then ,
double has a size of 8 byte
float has a size of 4 byte
In C all floats are auto casted to double in assignment side effects since double has highest size of byte.And since float and double are same which deals with floating points, they both only differ by precision limits and double has highest precision against float.
So, your program doesnot have the assignment operator(=) between x and y so it won't be auto casted.And hence you need to declare both as same due to difference in size of bytes in their types.
So, correction
float x=3.123456789;
float y=3.123456789;
This was your real mistake in output rather than printf
+ 6
Harsh See in my answer I have given above and your reason why char and int and why not double and float.PLEASE READ THE ANSWER BEFORE POSTING AS YOUR DOUBT.SINCE ITS ALREADY GIVEN IN MY ABOVE ANSWER TO YOUR DOUBT ON DOUBLE AND FLOAT USAGE IN YOUR CODE.
This is the correction you need to make in your program and its reason is in my answer above, see your previous question doubts in my code list,I have written it there in my profile.
Make correction:-
float x=3.123456789;
float y=3.123456789;
or you can declare it as double due to its higher precision:-
double x=3.123456789;
double y=3.123456789;
And you will get "yes" thats what I meant in my above answer in meaning to correction.There is no shortcut to learning ,so you need to read the whole answer!
The code given below is error free and its your code only,I just modified it and fixed it! Have a look at it.
Just open it, press the RUNto work to see "yes"
https://code.sololearn.com/cPAM182IleyR/?ref=app
+ 3
I "think" that the reason is that the size of double in bytes is greater than the sizes of floating point numbers
+ 2
the variables are of different type, and therefore cannot equal each other
+ 2
Harsh your new code(x and y = 3.1), chan printf("%f\n%f\n",x,y) to
printf("%.15f\n%.15f\n",x,y) like your old code, you will surprise at the output. I can not explain that but i will try✌👍👍
+ 2
I want to say that, why the compiler changes my float to 3.099~99 and double to 3.1000~001
0
Chilly_Vanilly I can't understand this. \_°¡°_/
0
well Chilly_Vanilly I had not read c++ yet
0
well D-Key here both double and float are equal but it prints "no" whereas when char and int have different size they print "yes"
https://code.sololearn.com/c5JhPP0R3GO0/?ref=app
https://code.sololearn.com/cHVTty1m8Z8C/?ref=app
0
Harsh yes