You are on page 1of 3

5/11/13

About primitive promotion. (SCJP forum at JavaRanch)

A friendly place for programming greenhorns!

Big Moose Saloon


Search

Java FAQ

Recent Topics

Register / Login

JavaRanch Java Forums Certification Programmer Certification (SCJP/OCPJP)

Author

About primitive promotion.


posted 8/13/2001 3:51 PM

Guoqiao Sun Ranch Hand Joined: Jul 18, 2001 Posts: 317

Dear all, plz see the following code and give me some explanation:
view plain c opy to c lipboard print ?

N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 .

p u b l i cc l a s sT e s t 1 0 1 { s t a t i cd o u b l ed ; s t a t i cf l o a tf ; p u b l i cs t a t i cv o i dm a i n ( S t r i n ga r g s [ ] ) { c o m p a r e ( L o n g . M A X _ V A L U E ,L o n g . M A X _ V A L U E ) ; u a l c o m p a r e ( I n t e g e r . M A X _ V A L U E ,I n t e g e r . M A X _ V A L U E ) ; te q u a l u a l / / n o

/ / e q

1 0 . 1 1 . u a l 1 2 . u a l 1 3 . 1 4 . 1 5 . 1 6 . 1 7 . 1 8 . 1 9 . 2 0 . 2 1 . }

c o m p a r e ( C h a r a c t e r . M A X _ V A L U E ,C h a r a c t e r . M A X _ V A L U E ) ;/ / e q c o m p a r e ( S h o r t . M A X _ V A L U E ,S h o r t . M A X _ V A L U E ) ; c o m p a r e ( B y t e . M A X _ V A L U E ,B y t e . M A X _ V A L U E ) ; / / e q / / e q

s t a t i cv o i dc o m p a r e ( d o u b l ed ,f l o a tf ) { S y s t e m . o u t . p r i n t ( " d="+d+"f="+f ) ; i f( f= =d )S y s t e m . o u t . p r i n t l n ( "e q u a l " ) ; e l s e S y s t e m . o u t . p r i n t l n ( "n o te q u a l" ) ; } }


1/3

www.coderanch.com/t/205198/java-programmer-SCJP/certification/primitive-promotion

5/11/13

About primitive promotion. (SCJP forum at JavaRanch)

The o/p is:


view plain c opy to c lipboard print ?

N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

0 1 . 0 2 . 0 3 . 0 4 . 0 5 .

d=9 . 2 2 3 3 7 2 0 3 6 8 5 4 7 7 6 E 1 8f=9 . 2 2 3 3 7 2 E 1 8e q u a l d=2 . 1 4 7 4 8 3 6 4 7 E 9f=2 . 1 4 7 4 8 3 6 5 E 9n o te q u a l d=6 5 5 3 5 . 0f=6 5 5 3 5 . 0e q u a l d=3 2 7 6 7 . 0f=3 2 7 6 7 . 0e q u a l d=1 2 7 . 0f=1 2 7 . 0e q u a l

The question is, why long value results in equal but int value results in not equal. Thanks in advance. Guoqiao

Guoqiao Sun<br />SC JP2 SC WC D2<br />C reator of <a href="http://www.jiris.com/" target="_blank" rel="nofollow">www.jiris.com</a>, Java resource, mock exam, forum William Brogden Author and allaround good cowpoke Rancher Joined: Mar 22, 2000 Posts: 12248

posted 8/13/2001 7:04 PM

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

Java Resources at www.wbrogden.com

Gagan Indus Ranch Hand Joined: Feb 28, 2001 Posts: 346

posted 8/13/2001 7:15 PM

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 (/^_^\)

Gagan (/^_^\) SC JP2 SC WC D IBM486 <br />Die-hard JavaMonk -- little Java a day, keeps you going.<br /><a href="http://www.objectfirst.com/blog" target="_blank" rel="nofollow">My Blog</a>

Thomas Paul mister krabs

posted 8/13/2001 9:42 PM


2/3

www.coderanch.com/t/205198/java-programmer-SCJP/certification/primitive-promotion

5/11/13

mister krabs Ranch Hand Joined: May 05, 2000 Posts: 13974

About primitive promotion. (SCJP forum at JavaRanch)

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

Associate Instructor - Hofstra University Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog

Guoqiao Sun Ranch Hand Joined: Jul 18, 2001 Posts: 317

posted 8/14/2001 6:47 AM

I am still not very clear. Guoqiao

Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp

subject: About primitive promotion.

Similar Threads comparisons Question about max_values... comparing MAX Values of Wrapper class with float and double Confusing outputs Wrapper-Q
All times above are in your local time zone & format.T he current ranch time (not your local time) is May 11, 2013 06:43:03 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

www.coderanch.com/t/205198/java-programmer-SCJP/certification/primitive-promotion

3/3

You might also like