The Google Calculator Glitch, could float vs. double be a possible reason?

Question :

The Google Calculator Glitch, could float vs. double be a possible reason?,

Answer :

I did this Just for kicks (so, not exactly a question, i can see the downmodding happening already) but, in lieu of Google’s newfound to do  (check it! according to google 500,000,000,000,002 – 500,000,000,000,001 = 0), i figured i’d try the following in C to run a little theory.

int main()  {     char* a = "399999999999999";     char* b = "399999999999998";       float da = atof(a);     float db = atof(b);       printf("%s - %s = %fn", a, b, da-db);       a = "500000000000002";     b = "500000000000001";     da = atof(a);     db = atof(b);     printf("%s - %s = %fn", a, b, da-db);  }  

When you run this program, you get the following

   399999999999999 - 399999999999998 = 0.000000     500000000000002 - 500000000000001 = 0.000000  

It would seem like Google is using simple 32 bit floating precision (the error here), if you switch float for double in the above code, you fix the issue! Could this be it?

Read More  What’s the best way to implement field validation using ASP.NET MVC?

/mp

,

For more of this kind of silliness see this nice article pertaining to Windows calculator.

 

The innards of Calc – the arithmetic
engine – was completely thrown away
and rewritten from scratch. The
standard IEEE floating point library
was replaced with an
arbitrary-precision arithmetic
library. This was done after people
kept writing ha-ha articles about how
Calc couldn’t do decimal arithmetic
correctly, that for example computing
10.21 – 10.2 resulted in 0.0100000000000016.

That’s the answer The Google Calculator Glitch, could float vs. double be a possible reason?, Hope this helps those looking for an answer. Then we suggest to do a search for the next question and find the answer only on our site.

Read More  Draining Standard Error in Java

Disclaimer :

The answers provided above are only to be used to guide the learning process. The questions above are open-ended questions, meaning that many answers are not fixed as above. I hope this article can be useful, Thank you