It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes float  result Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "float  result" Watch "float  result" New topic

float result

Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

o/p ==> -46

Please explain the o/p

Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Ravi Shankar R

Joined: Jan 05, 2005
Posts: 7
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

Joined: Mar 01, 2005
Posts: 89
How did you come up with f = 1.234567936E9 ?

SCJP 1.5<br />SCWCD 1.4
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
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

Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.
Pedro Henrique Pereira de Andrade

Joined: Jul 05, 2005
Posts: 3

First, sorry for my serious bad inglish, but i'm tring.

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

Thanks god for all you've done.
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
Floating point values are not accurate. Do a search on this forum for floating point accuracy, it has been discussed many times.
I agree. Here's the link:
subject: float result
It's not a secret anymore!