aspose file tools*
The moose likes Beginning Java and the fly likes Maths Output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Maths Output" Watch "Maths Output" New topic
Author

Maths Output

Frankie Chee
Ranch Hand

Joined: Oct 26, 2003
Posts: 43
Hi all,
I am a student and am new to programming, I would like to ask a simple question about the output of the following:
class calculation
{
public static void main(String args[])
{
int x=10;
int y=20;
float z=25.98f;
System.out.println(z/y);
System.out.println(z-y);
}
}
Output:
1.299
5.92999
My question is for z-y why is the output with so many decimal places when my calculator shows an output of 5.98?
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937

Output:
1.299
5.92999

I seriously doubt that. Maybe your JVM was written by Microsoft?
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
output from my system.

1.299
5.9799995
Frankie Chee
Ranch Hand

Joined: Oct 26, 2003
Posts: 43
Chi Lin, yours have more decimal places than mine!
Frankie Chee
Ranch Hand

Joined: Oct 26, 2003
Posts: 43
How do I check if my JVM is written by microsoft?

I downloaded it from http://java.sun.com.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10
It's not a problem with the JVM. It has to do with how floating point is stored. It is never precise.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
The mechanism for representing floating point numbers with a limited number of binary digits that Marilyn referred to is the IEEE 754 Floating Point Standard. Take a look at What Every Computer Scientist Should Know About Floating Point Arithmetic.
[ November 18, 2003: Message edited by: Dirk Schreckmann ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
Consider Paul's rocket mass heater.
 
subject: Maths Output