The Artima Developer Community
Sponsored Link

Java Buzz Forum
1.03 - .42 = ?

4 replies on 1 page. Most recent reply: Jul 15, 2004 2:00 AM by Andy

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Weiqi Gao

Posts: 1808
Nickname: weiqigao
Registered: Jun, 2003

Weiqi Gao is a Java programmer.
1.03 - .42 = ? Posted: Jul 7, 2004 10:56 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Weiqi Gao.
Original Post: 1.03 - .42 = ?
Feed Title: Weiqi Gao's Weblog
Feed URL: http://www.weiqigao.com/blog/rss.xml
Feed Description: Sharing My Experience...
Latest Java Buzz Posts
Latest Java Buzz Posts by Weiqi Gao
Latest Posts From Weiqi Gao's Weblog

Advertisement

Item 31 of Joshua Bloch's Effective Java reads:

Avoid float and double if exact answers are required

And as an example, he used the following example:

System.out.println(1.03 - .42);

which prints

0.6100000000000001

I understand the reasoning behind the advice. No problem there.

Out of curiosity, I tried similar expressions in several different languages that's available on my Fedora Core 2 box. And the result is fascinating:

LanguageExpressionResult
C# Console.WriteLine(1.03 - .42); 0.61
C++ std::cout << 1.03 - 0.42 << std::endl; 0.61
Perl print(1.03 - .42); 0.61
Python print(1.03 - .42) 0.61
Ruby print(1.03 - 0.42) 0.61
Guile (display (- 1.03 .42)) 0.61

What is going on here?

Read: 1.03 - .42 = ?


nes

Posts: 137
Nickname: nn
Registered: Jul, 2004

Re: 1.03 - .42 = ? Posted: Jul 11, 2004 11:24 AM
Reply to this message Reply
Python 2.3.3 (#51, Dec 18 2003, 20:22:39) [MSC v.1200 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print (1.03-.42)
0.61
>>> print repr(1.03-.42)
0.6100000000000001
>>>

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: 1.03 - .42 = ? Posted: Jul 12, 2004 1:20 PM
Reply to this message Reply
I suspect that many or all of these "print" routines have default behavior designed to display float and double values nicely.

Here's a way to see the precision loss in Python without resorting to repr():


>>> print 1.03 - .42 - .61
1.11022302463e-016


It probably has similar results in the other languages listed.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: 1.03 - .42 = ? Posted: Jul 12, 2004 1:26 PM
Reply to this message Reply
> It probably has similar results in the other languages listed.

... C# gives a few more digits of precision (not to be confused with accuracy!): 1.11022302462516E-16

Andy

Posts: 28
Nickname: adn
Registered: Jul, 2004

Re: 1.03 - .42 = ? Posted: Jul 15, 2004 2:00 AM
Reply to this message Reply
> It probably has similar results in the other languages
> listed.

From the python documentation (first paragraph);

http://www.python.org/doc/current/lib/typesnumeric.html

"Floating point numbers are implemented using double in C"

I bet that most, if not all the languages listed are in some way using standard C types. So they give the same error each time.

Flat View: This topic has 4 replies on 1 page
Topic: Linux Saves the Day Previous Topic   Next Topic Topic: Erik's JavaOne-esque Schedule

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use