• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Maths Output

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Output:
1.299
5.92999

I seriously doubt that. Maybe your JVM was written by Microsoft?
 
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do I check if my JVM is written by microsoft?

I downloaded it from http://java.sun.com.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic