Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

About primitive promotion.

 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all, plz see the following code and give me some explanation:

The o/p is:

The question is, why long value results in equal but int value results in not equal.
Thanks in advance.
Guoqiao
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The int to float loses some bits of precision.
You are comparing two double values arrived at by different conversion paths.
int -> -> double
int -> float -> double
Since float uses 32 bits, it uses some for the exponent and has fewer bits for precision.
Bill
 
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hav a q about William's explanation
applyiong the same logic ,
we shld get "Not equal " for first call to compare
becoz conversion paths r
Long -> Double
Long -> float -> Double

long is 64 bit ? wont more bits be lost while converting to float's base-mantisaa format ??
------------------
Gagan (/^_^\)
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has to do with rounding that occurs under IEEE float rules.
Copy f to a double (g) and print both:
f = 2.14748365E9
g = 2.147483648E9
------------------
Tom - SCJP --- Co-Moderator of the Programmer Certification Forums
 
Guoqiao Sun
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am still not very clear.
Guoqiao
 
My favorite is a chocolate cupcake with white frosting and tiny ad sprinkles.
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!