• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

*****primitive type casting issue******

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

What is the result type of float and long. The answer is supposed to be float.
However, from what i understand, the result type always go to the bigger type if not int. float is 32 bits and long is 64 bits. Is it not supposed to be long?? What are exactly the rules surround these casting issue. BTW I havent have the scjp book yet.

Thanks.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For floating point number like float ,double etc result type is double
For integer type numbers like int , long etc the result type is int

If we have a float and a long the result should be double because one number is floating type.

Regards,
Amit Mahajan
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Hi Amit,
Can i know why can't the result be stored in float as float is larger than long.

Regards,
Seema
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

...as float is larger than long


No it isn't, float is 32 bits, long is 64 bits.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to the JLS 2nd edition Section 4.2.2 Integer Operations, it says


If an integer operator other than a shift operator has at least one operand of type long, then the operation is carried out using 64-bit precision, and the result of the numerical operator is of type long. If the other operand is not long, it is first widened (�5.1.4) to type long by numeric promotion (�5.6). Otherwise, the operation is carried out using 32-bit precision, and the result of the numerical operator is of type int. If either operand is not an int, it is first widened to type int by numeric promotion.



The Section 4.2.4 on Floating Point operations, it says


If at least one of the operands to a binary operator is of floating-point type, then the operation is a floating-point operation, even if the other is integral.

If at least one of the operands to a numerical operator is of type double, then the operation is carried out using 64-bit floating-point arithmetic, and the result of the numerical operator is a value of type double. (If the other operand is not a double, it is first widened to type double by numeric promotion (�5.6).) Otherwise, the operation is carried out using 32-bit floating-point arithmetic, and the result of the numerical operator is a value of type float. If the other operand is not a float, it is first widened to type float by numeric promotion.



Hope this helps!
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy cowboys,

it does not matter how many bits they have.
It only matters here, how big the numbers can be.

And any long number is low enough to fit into a float, so there is no double needed. But I admit, the double would be more precise.

prints
long: 9223372036854775807
same: 9.223372E18
same2: 9.223372036854776E18
float: 3.4028235E38

The value of the highest possible long can easily be represented in a float. And even if you stored the long into a double you would lose some precission, because the double cannot store all the digits of the long (lost in bold above), it needs some bits for the exponent.

But you could argue, that for the sake(*) of precission, it would have been better to put the floats into a double automatically. I guess this hasn't been done to save system resources. But this is only my guess, I don't know really.



Yours,
Bu.

------------------

(*) = japanese rice wine
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic