2013年12月15日星期日

Tell you a true secret : Java sucks ! Will be eliminated !


System.out.println ((2.0-1.1) == 0.9);

Why?
------ Solution ---------------------------------------- ----
  The reply was deleted administrator at 2013-12-15 09:12:15

------ Solution ------------------------------------ --------
menstrual posted again. With float it System.out.println ((2.0f-1.1f) == 0.9f);
------ Solution ----------------- ---------------------------
accuracy has nothing to do with language.
float / double: limited space ( such as 64 ) for unlimited data ( an interval is infinite decimal ) there must be accuracy problems.

Here's wrong , and note the numbers are strings. Do not pass float.
BigDecimal d1 = new BigDecimal ("2.0");
BigDecimal d2 = new BigDecimal ("1.1");
BigDecimal d3 = new BigDecimal ("0.9");
d1 = d1.subtract (d2);
System.out.println (d1.equals (d3));
------ Solution ---------------------- ----------------------


you have this problem , and if so
System.out.println ((2.0001f-1.1001f) == 0.9f);
BigDecimal d1 = new BigDecimal ("2.0001");
BigDecimal d2 = new BigDecimal ("1.1001");
BigDecimal d3 = new BigDecimal ("0.9");
d1 = d1.subtract (d2);
System.out.println (d1);
System.out.println (d1.compareTo (d3)); / / find equals too. There will be 0.90! = 0.9

没有评论:

发表评论