Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 weird....
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
person




PostPosted: Sun Nov 06, 2005 5:35 pm   Post subject: weird....

code:

public class hi
{
    public static void main (String[] args)
    {
        System.out.println (0.1 + 0.2);
    }
}


k, so this seems like it should give me 0.3
but apparently, it outputs 0.30000000000000004
y?
Sponsor
Sponsor
Sponsor
sponsor
[Gandalf]




PostPosted: Sun Nov 06, 2005 6:00 pm   Post subject: (No subject)

This topic was talked about in detail here:

http://www.compsci.ca/v2/viewtopic.php?t=9833

This may not be what you are talking about
person




PostPosted: Mon Nov 07, 2005 7:24 pm   Post subject: (No subject)

and how would i solve this problem and make java add like it should be?
wtd




PostPosted: Mon Nov 07, 2005 7:40 pm   Post subject: (No subject)

Floating point numbers cannot be natively represented inside a computer. Therefore we have a Pretty Good(tm) way of estimating them to a fair degree of accuracy.
person




PostPosted: Mon Nov 07, 2005 7:53 pm   Post subject: (No subject)

Quote:
Floating point numbers cannot be natively represented inside a computer. Therefore we have a Pretty Good(tm) way of estimating them to a fair degree of accuracy.


so basically, there's no way to actually do addition with decimals in java?
wtd




PostPosted: Mon Nov 07, 2005 8:18 pm   Post subject: (No subject)

Not to complete accuracy. The issue is not restricted to Java.
person




PostPosted: Mon Nov 07, 2005 8:23 pm   Post subject: (No subject)

Quote:
Not to complete accuracy. The issue is not restricted to Java.


k, i know languages like turing has the same problem but y does turing not output 0.30000000000004 for 0.1+0.2? does turing hava another way of calculating hte numbers?
wtd




PostPosted: Mon Nov 07, 2005 8:43 pm   Post subject: (No subject)

person wrote:
Quote:
Not to complete accuracy. The issue is not restricted to Java.


k, i know languages like turing has the same problem but y does turing not output 0.30000000000004 for 0.1+0.2? does turing hava another way of calculating hte numbers?


It's just a matter of how it outputs the number. It like rounds to a certain place by default. Many environments will round to something like 8-10 places.

Then, rather than output:

code:
0.3000000000


It simply outputs:

code:
0.3
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: