• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

related to arithmetic operations on datatype.

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi friends go through following code:

class test{
public static void main(String agr[]){

float f=Float.parseFloat(agr[0]);
int i=(int)f;

System.out.println("whole no is "+i);
System.out.println("subtraction is "+(f-i));

}
}

generates output when i give no 25.48(or any other float) as
whole no is 25
subtraction is 0.47999954

where the o/ should be:
whole no is 25
subtraction is 0.48

why so?
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at point #21 in the JavaRanch Beginner's FAQ. It discusses floating point accuracy issues -- and have links if more information is needed.

Henry
[ September 18, 2006: Message edited by: Henry Wong ]
 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why do you think the output should be 0.48 ?
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shubhada Nandarshi:

int i=(int)f;

System.out.println("whole no is "+i);
System.out.println("subtraction is "+(f-i));



f is truncated when cast to int.
with the subtraction, i is promoted to float and the result is a float.
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can float and double value represent decimal number exactly?
 
Shubhada Nandarshi
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for reply.
I have one more doubt..how we can convert float or double updo some decided significant digits? e.g. if we want that, answere should be printed upto 4 significant digits only ,then how to do this?
 
Shubhada Nandarshi
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please can anybody will explain me?
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use the DecimalFormat class in the java.text package.

If you need accuracy in decimal numbers, you can use the BigDecimal class in the java.math package.
 
Does this tiny ad smell okay to you?
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic