Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Maths Output

 
Frankie Chee
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Output:
1.299
5.92999

I seriously doubt that. Maybe your JVM was written by Microsoft?
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
output from my system.

1.299
5.9799995
 
Frankie Chee
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chi Lin, yours have more decimal places than mine!
 
Frankie Chee
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I check if my JVM is written by microsoft?

I downloaded it from http://java.sun.com.
 
Marilyn de Queiroz
Sheriff
Posts: 9064
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not a problem with the JVM. It has to do with how floating point is stored. It is never precise.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic