File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

float and double observation

 
Leandro Melo
Ranch Hand
Posts: 401
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
I know all floating points are by default type double (unless you type f or F).



However, i was thinking... If i instantiate a very large number, it won't help me to insert an f at it's end, because it wouldn't work as a cast. In this case i'm forced to explicitly cast the number (what's the default behaviour when your "shrinking" the bits representation).



As i said in the post subject, this is just an observation. But, here's one thought. The point of inserting the (float) cast before the number is to say the compiler something like "Hey, i know what i'm doing, don't bother". In other hand, when you insert the F or f at the end of the number, you're telling the compiler "Hey, i want this as a float, not a double, ok".
So, in a more abstract sense, this two things means the actual same thing.
Then i ask myself if this whole thing is actually necessary...? Why float isn't just float, not mattering the f or F? Also with longs...?
Is this because some IEEE standard or something?

[ January 27, 2005: Message edited by: Leandro Melo ]
[ January 27, 2005: Message edited by: Leandro Melo ]
 
navneet shrivastava
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

nice question... we can call it a issue rather.
still hunting for explanation.

thanks,
 
Liang Anmian
Ranch Hand
Posts: 119
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my opinion, casting and appending an 'F' is not really the same. Casting means you are forcing the number to fit in 4 bytes. In other words, bits may be lost.

However, when you append an 'F', you are merely indicating to the JVM not to use double. It says nothing about bits truncation. The reason why the first line of code fails compilation is that appending an 'F' doesn't truncate extra bits, so you will get the "floating point too large" error.

You don't get a compilation error for the second line because the JVM will truncate the extra bits, so that only 4 bytes are left.

I do hope I'm making sense.
 
Leandro Melo
Ranch Hand
Posts: 401
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with you Liang.
Append f or F doesn't tell the compiler to truncate the value. Just like appending L or l to a number.
But one thing that i'd like to understand better is "why not avoiding this kinda stuff". Why couldn't we write

and the variable 'a' actually be a float (and not a double).
So, my point here is not the casting or non-casting itself, but why do me make things more "complicated" than they really need to be.
I'm not saying the appending an f or a l at the number is really "complicated", but is it really necessary?
Maybe it's some kind of ieee standart or it has some very arguments to be this way... i don't know... that's what i wanted to know.
 
Liang Anmian
Ranch Hand
Posts: 119
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you know, Java enforces the "default data type" rule. In other words, numbers hardcoded in the source code are integers by default etc. What I think is that Java has to enforce this rule in order to know how to handle certain arithmetic operations. In the end, it just happens that the developers have chosen to use double as default for floating point values, so they require programmers like us to provide hints to the compiler if we want any workarounds (e.g appending an 'F' to signify float).

Not sure if what I said here makes sense, but just a thought.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic