Please explain the following code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Please explain the following code?

will float 1.1 and double 1.1 will give same result? (ans. no). will float 1.5 and double 1.5 will give same result?(ans. yes)

4th Sep 2016, 6:23 AM
ASHISH RAKESH
ASHISH RAKESH - avatar
3 Answers
+ 3
Some fractions of real numbers can not be represented or saved in binary format. So never compare doubles with the (default) equality operator. Instead use some approximation method where you can define the precision to compare two doubles with. 0.1 in decimal is 0.0001100110011.. period in binary, where you'd run out of memory before ever being able to store that number. So it gets cut off after several digits. When reverting from binary to decimal, you won't get the exact same number. Keep that in mind :)
4th Sep 2016, 11:53 PM
René Schindhelm
René Schindhelm - avatar
+ 1
Yes they will
11th Sep 2016, 4:33 PM
Pau Suan Dal
Pau Suan Dal - avatar
0
The precision in which fractional numbers (set R) can be represented is different. Hence the difference.
4th Sep 2016, 10:38 AM
Stefan
Stefan - avatar