Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

double numbers

Status
Not open for further replies.

fenris

Programmer
May 20, 1999
824
CA
IS there a problem with java when it comes to double numbers and accuracy?<br><br>When I do something like this:<br><br>double x = 5;<br>double y = 20;<br><br>system.out.print(y/x);<br><br>I get<br>&nbsp;4.000000000012<br><br>or something to that effect.<br><br>Is this normal or could my machine be malfunctioning. If this is normal (the same calc works properly in VB) is there any way to produce correct output?<br><br> <p> fenris<br><a href=mailto:fenris@hotmail.com>fenris@hotmail.com</a><br><a href= > </a><br>
 
Thanks for the info, I just tried it out on my work computer and it works fine, even 2/3 and 1/3 give the answer that I expect. I will have to check my home machine out very carefully when I get there as that is where I ran the tests.<br><br>Thanks <p> fenris<br><a href=mailto:fenris@hotmail.com>fenris@hotmail.com</a><br><a href= > </a><br>
 
I've had all kinds of problems with this type of error. One java assignment I had in a class I took last year was to implement different kinds of number classes all inheriting from an abstract GeneralNumber class. It's children included GeneralLong, GeneralDouble, GeneralComplex and GeneralRational. One of the requirements was converting between the different types of objects using polymorphism. Often, in converting GeneralDouble to GeneralRational, I'd get odd fractions. For example, .5 might end up getting converted to 10000000/20000032 or something (that's just an example, I don't remember how close that kind of answer is to what I was getting last year). The thing you need to remember is this- when dealing with rationals and doubles, Java is not 100% accurate. And this does depend on the machine. I'm not sure exactly how machine-dependent Java really is- it's not 100% platform independent as it would like to be. My classmates and I often got slightly different answers when we compiled and ran on different machines. I'm not sure what math processor Java uses.<br><br>In any case... I ended up getting mad at Java but handing it in anyway knowing that it was not a bug in my code- it was a problem with the processor.<br><br>Best of luck :eek:) <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence.
 
My question to you Liam would be is there any way to compensate for this problem? I realize that it may not occur on all machines, but I did see a set of financial classes that were for sale, they claimed that they had a class that would take care of this problem, ensuring accurate results. Have you heard about or seen any classes that would do this? The reason that I am concerned with this is that I do a lot of work with the need for double precision accuracy and I am dissapointed that the problem may crop up.<br><br><br>Thanks <p> fenris<br><a href=mailto:fenris@hotmail.com>fenris@hotmail.com</a><br><a href= > </a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top