Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

float result

 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


o/p ==> -46

Please explain the o/p
 
Ravi Shankar R
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when the value of int i is assigned to float f the value of f is 1.234567936E9 since the change has happened from an integer literal to double literal so i = 1234567890 and f = 1.234567936E9

thus, i - f = 1234567890 - 1.234567936E9 = -46


 
Rupak Khurana
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How did you come up with f = 1.234567936E9 ?
 
Jay Pawar
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vishnu prakash:


o/p ==> -46

Please explain the o/p


System.out.println(i - (int)f);
when you cast the float value f to int the resultant value is 1234567936
hence the value printed out is ( 1234567890 - 1234567936 ) which is -46
 
Pedro Henrique Pereira de Andrade
Greenhorn
Posts: 3
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, sorry for my serious bad inglish, but i'm tring.


The question is: why 1234567890 when converting to float is 1.234567936E9 ???

 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Floating point values are not accurate. Do a search on this forum for floating point accuracy, it has been discussed many times.
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!