• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

why ceil & floor accepts int

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
don't really understand why ceil & floor accepts integer(or long, float) as parameter. While the following code with no luck to compile:
(creating a funciton a and hope it can accept float / double)
public class MathTest2 {
public static void main (String args[]) {
System.out.println(a(1f));
System.out.println(a(1d));
System.out.println(a(1L));
System.out.println(a(1));
}

static double a(int in) {
return in;
}
}
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java will cast an int into a wider type such as long, float, double. However Java will not cast a primitive to a smaller type such as double, float, long to int.
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java will automatically perform an arithmetic promotion for higher order data types for numbers. For example, byte to int, int to long, long to double. It is because no problems will be encountered by just such upcasting.

However, Java will not perform an downcast for int to byte, because changing a value from 32 bit to 16 bit might lead to information lost. In case you need to perform a downcast, the compiler will make sure that you did your own choice, i.e. to cast explicitly.

Nick
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
float f = 123L;

then why is this legal without casting ???
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because float is a 'wider' type than long. Float is capable of holding a wider range of numbers than long. Contrary to popular belief the casting rules for primitives do not really take into account accuracy so much as they take into account range.
 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thought float was 4 bytes and long was 8 bytes.
How come

work in that case ?
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Steven said, Java cares more about range of values than about precision. Even though a float has only 32 bits, it can hold a much bigger number than a long because it uses a form of scientific notation.

In your example, 123445678912345L becomes 1.23445682E14F
The size of the number is preserved but the full precision is lost.

On the other hand, you can't convert from a float to a long without an explicit cast because the float can be much bigger than any long.

For example, 1.0E55F cannot fit in a long.
 
Catch Ernie! Catch the egg! And catch this tiny ad too:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic