-3

Why body of if() in 1st code Executes While the body of if() in 2nd code Doesn't Executes

Float are not precise

Working

double num1 = 0.2;
double num2 = 0.2;
double num3 = num1 + num2;
if (num3 == 0.4)
{
    MessageBox.Show("1st");
}

Not Working

double num1 = 0.1;
double num2 = 0.2;
double num3 = num1 + num2;
if (num3 == 0.3)
{
    MessageBox.Show("2nd");
}
Community
  • 1
  • 1
TeamKhurram
  • 107
  • 9

1 Answers1

2

Reason

Machine uses binary language When it convert number into binary , convert back into decimal The value get changed ,due to some number can't be converted Completely in binary

When you convert 0.1 into base 2 (binary) you get a repeating pattern after the decimal point,

Like 1/3 in base 10 ; 1/3=0.333333333333333333333333333333333.......... & never get Exact value

Therefor you can't Get every number's exact Value using normal floating point methods.

Example (Conversion Pattern)

0.1 Pattern In Binary...

Binary Pattern of 0.1

Source: Why 0.1 Does Not Exist In Floating-Point

0.1 Conversion 0.1 is one-tenth, or 1/10. To show it in binary You can See 100 is repeating after intervals As Giving output as 1001 as Shown In Diagram

Conversion Of 0.1 to binary

Source: Why 0.1 Does Not Exist In Floating-Point

Second Code Snippets {Answer of 0.1+0.2 Is not Same as actual value of 0.3}

--------------------------------------------------------------
Actual {Answer of 0.1+0.2 Is not Same as actual value of 0.3}
--------------------------------------------------------------
0.1=0.1000000000000000055511151231257827021181583404541015625
0.2=0.200000000000000011102230246251565404236316680908203125
0.3=0.299999999999999988897769753748434595763683319091796875

==============================================================
 Calculation 0.1 + 0.2
______________________________________________________________ 
 0.1=0.1000000000000000055511151231257827021181583404541015625
+0.2=0.200000000000000011102230246251565404236316680908203125
--------------------------------------------------------------
 0.3=0.3000000000000000444089209850062616169452667236328125

==============================================================
Answer
--------------------------------------------------------------
  0.3000000000000000444089209850062616169452667236328125
!=0.299999999999999988897769753748434595763683319091796875
---------------------------------------------------------------

First Code Snippets {Answer of 0.2+0.2 Is Same as actual value of 0.4}

---------------------------------------------------------------
Actual {Answer of 0.2+0.2 Is Same as actual value of 0.4}
---------------------------------------------------------------
 0.2=0.200000000000000011102230246251565404236316680908203125
 0.2=0.200000000000000011102230246251565404236316680908203125
 0.4=0.40000000000000002220446049250313080847263336181640625  
==============================================================
 Calculation 0.2 + 0.2
---------------------------------------------------------------
 0.2=0.200000000000000011102230246251565404236316680908203125
+0.2=0.200000000000000011102230246251565404236316680908203125
---------------------------------------------------------------
 0.4=0.40000000000000002220446049250313080847263336181640625

==============================================================
Answer
--------------------------------------------------------------
  0.4=0.40000000000000002220446049250313080847263336181640625
==0.4=0.40000000000000002220446049250313080847263336181640625
---------------------------------------------------------------
  1. Source:Is floating point math broken?

  2. Source:Why 0.1 Does Not Exist In Floating-Point

  3. Further Guide About Float Point

  4. Guide By Oricle

  5. Examine Number

  6. IEEE 754 double-precision binary floating-point format

Community
  • 1
  • 1
Khurram Sharif
  • 504
  • 1
  • 4
  • 20
  • 2
    I didn't downvote this (yet), but I love these images from [Rick Regan's article](http://www.exploringbinary.com/why-0-point-1-does-not-exist-in-floating-point/). Oh, and link 1 is broken. – Blas Soriano Jun 05 '15 at 06:36
  • 4
    Answers shouldn't be plagiarized, you should give credit where it is due – Sayse Jun 05 '15 at 06:38
  • 1
    What does this long post even say? Where in all this do you mention that floating point numbers are inaccurate? Also note that plagiarism (posting other people's work as your own answer without attribution) [isn't acceptable and your answer can get flagged](http://meta.stackexchange.com/questions/160071/what-to-do-when-plagiarism-is-discovered). Put the links to your sources – Panagiotis Kanavos Jun 05 '15 at 06:38
  • These edits IMHO saved your post (upvoted). It would be nice if you add something (including references) to [UNUM](http://sites.ieee.org/scv-cs/files/2013/03/Right-SizingPrecision1.pdf) as a possible future solution. – Blas Soriano Jun 05 '15 at 07:06
  • A simple link to Rick Regan's blog would do, you are not improving the Internet by copying it here as screenshots. The question you are answering has been answered already and your answer does not make this site better. Downvoted. – Pascal Cuoq Jun 05 '15 at 08:51
  • @PascalCuoq It means I must not give "Refined answer" of any question while Compleate Answer is not given in a single post – Khurram Sharif Jun 05 '15 at 09:17