File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Floating point literal Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Floating point literal" Watch "Floating point literal" New topic
Author

Floating point literal

sanjana narayanan
Ranch Hand

Joined: Nov 25, 2003
Posts: 142
Hi,
Can anyone give some examples for the follo. 2 pts.
1.A compile-time error occurs if a nonzero floating-point literal is too small, so that, on rounded conversion to its internal representation, it becomes a zero.
2.A compile-time error does not occur if a nonzero floating-point literal has a small value that, on rounded conversion to its internal representation, becomes a nonzero denormalized number?
Eg float f = 2.0f)- can this be an eg for the 2nd point.(not sure..)
-Sanjana
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
float f1 = 0.7e-45f; // too small
float f2 = 0.8e-45f; // okay

A normalized number looks like this, 1.xxxxxxxx * 2^yyy.
The smallest float normalized number == 1.0 * 2^-126.
The internal representation is 0 00000001 00000000000000000000000
Rather than having a gap between 0 and the smallest normalized number, the IEEE standard allows denormalized numbers.
The smallest float denormalized number ==
0.00000000000000000000001 * 2^-126
The internal representation is 0 00000000 00000000000000000000001
You might try to visualize a number line

[ December 18, 2003: Message edited by: Marlene Miller ]
sanjana narayanan
Ranch Hand

Joined: Nov 25, 2003
Posts: 142
Hi Merlene,
I am not very much clear with the example u had given. How do u know that (0.72-45f) is the smallest no..and also let me know how do u arrive at the representation. Is there a formula for it..
Your example seems to be good and it doesn't get into my mind:-)

-Sanjana
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
The smallest normalized float is
1.000 0000 0000 0000 0000 0000 * 2^-126
The internal 32-bit form is
0 00000001 000 0000 0000 0000 0000 0000
The 1-bit sign field is 0 (positive).
The 8-bit exponent field is 00000001. A value of 1 means 2^-126. (A value of 127 means 2^0, 126 means 2^-1 etc.)
The 23-bit signficand field is 00000000000000000000000. This means 1.0 (an implied 1. before the fraction)
-----
The smallest denormalized float is
0.000 0000 0000 0000 0000 0001 * 2^-126
The internal 32-bit form is
0 00000000 000 0000 0000 0000 0000 0001
The 1-bit sign field is 0 (positive)
The 8-bit exponent field uses the reserved value of 00000000 to denote a denormalized number.
The 23-bit signficand field is 00000000000000000001 This means 0.00000000000000000000001 (an implied 0. before the fraction)
-----

The smallest normalized float
0.00000000000000000000000000000000000001175494350822287507968736
5372222456778186655567720875215087517062784172594547271728515625
The smallest denormalized float
0.00000000000000000000000000000000000000000000140129846432481707
0923729583289916131280261941876515771757068283889791082685860601
48663818836212158203125
(Notice the JLS 3.10.2 says the smallest float literal is 1.40239846e-45.)
----
I don�t know the IEEE rules for rounding. So I don�t know when numbers smaller than 1.401e-45 are rounded down to 0 and when they are rounded up to 1.401e-145.
So I just experimented. This way I discovered 0.7e-45 rounds down to 0 (compiler error) and 0.8e-45 rounds up. All I knew was the numbers you were looking for had to be smaller than 1.401e-45. It is reassuring to see that the rounding split (up or down) occurs about half way between 0 and 1.4
I had this picture in my head.

[ December 20, 2003: Message edited by: Marlene Miller ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
let me know how do u arrive at the representation

Sanjana, I learned about floating point representation from the book Computer Organization and Design, the Hardware/Software Interface by David Paterson and John Hennessy, chapter 4.
The formal description is not difficult to understand. The 3 fields are 1-bit sign, 8-bit exponent, 23-bit significand/fraction. The values of the exponent field 1 to 254 mean 2^-126 to 2^127, 0 is reserved for 0.0 and 255 is reserved for NaN. There is an implied 1. in front of the fraction.
Use Float.intBitsToFloat(i) and Float.floatToIntBits(f) to study the internal representation of some arbitrary float values. But do not use System.out.println to print the base 10 value of floats. Use new BigDecimal(f).
[ December 20, 2003: Message edited by: Marlene Miller ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
How do u know that (0.72-45f) is the smallest no

The way I figured out the smallest float,
For a normalized number,
The biggest exponent is 254 or 11111110
the smallest exponent is 1 or 00000001
the exponents 0 and 255 are reserved.
The biggest significand (fraction) is
.111 1111 1111 1111 1111 1111
The smallest signficand (fraction) is
.000 0000 0000 0000 0000 0000
There is an implied 1. before the fraction.
----
For a denormalized number,
The exponent is always 00000000 meaning 2^-126.
The biggest signficand is
.111 1111 1111 1111 1111 1111
The smallest signficand is
.000 0000 0000 0000 0000 0001
There is an implied 0. before the fraction.
----
There will be quiz tomorrow.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
For purposes of the exam, no one will ever ask if you know how normalized vs. denormalized numbers work. And the smallest positive float value is given by Float.MIN_VALUE, which is 2^-149.


"I'm not back." - Bill Harding, Twister
sanjana narayanan
Ranch Hand

Joined: Nov 25, 2003
Posts: 142
Hi Merlene,
Thank u very much for all your replies.
-Sanjana
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Sanjana, I have a much better answer to your original question. Erase all that other stuff from your mind.
2^-126 is the smallest normalized float.
2^-149 is the smallest denormalized float.
Or as Jim said, the smallest float is Float.MIN_VALUE (2^-149).
Half way between 0.0 and Float.MIN_VALUE is the split. Numbers to the left round to 0.0, causing a compiler error. Numbers to the right round up to Float.MIN_VALUE.
One half of Float.MIN_VALUE is 2^-150.

[ December 21, 2003: Message edited by: Marlene Miller ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Here is an even better answer.
Float literals between 0.0 and about 0.7006e-45 round down to 0.0 and cause a compiler error.

about 0.700649e-45
[ December 21, 2003: Message edited by: Marlene Miller ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Floating point literal
 
Similar Threads
Constructor Arguments for Wrapper Class Float
Primitive Casting
How to pass a long in ...Long ll = Long.valueOf(3L);//gives
strange conversion??
my notes on JLS for any1 who needs them !!