• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

int to float 16777217

 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been asked to explain this statement
"A float cannot hold the integer 16777217, because the significand is 24 bits."
Step 1. Collect some data.

Here is the floating point 32-bit representation (leading 0�s dropped)
16777210, 1001011011111111111111111111010
16777211, 1001011011111111111111111111011
16777212, 1001011011111111111111111111100
16777213, 1001011011111111111111111111101
16777214, 1001011011111111111111111111110
16777215, 1001011011111111111111111111111
16777216, 1001011100000000000000000000000
16777217, 1001011100000000000000000000000
[ September 09, 2003: Message edited by: Marlene Miller ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Step 2. Let�s analyze the floating point representation of 16777215.
16777215 1001011011111111111111111111111
Add leading 0�s removed by System.out.println()
Separate bits into 1-bit sign, 8-bit exponent and 23-bit signficand.
0 10010110 111 1111 1111 1111 1111 1111
Compute exponent. (127 means 2^0, 128 means 2^1, 126 means 2^-1)
exponent 10010110, 128 + 16 + 6 = 127 + 23, 2^23
Insert implicit leading 1. before signficand.
1.111 1111 1111 1111 1111 1111 * 2^23 ==
1111 1111 1111 1111 1111 1111

16777215, 111111111111111111111111
[ September 09, 2003: Message edited by: Marlene Miller ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let�s analyze the floating point representation of 16777216.
16777216 1001011100000000000000000000000
0 10010111 000 0000 0000 0000 0000 0000
exponent 10010111, 128 + 16 + 7 = 127 + 24, 2^24
1.000 0000 0000 0000 0000 0000 * 2^24 ==
1 0000 0000 0000 0000 0000 0000

16777216, 1000000000000000000000000
[ September 09, 2003: Message edited by: Marlene Miller ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Step 3. Let�s convert the integer 16777217 to a float.

1000000000000000000000001 (leading 0�s dropped)
16777217 == binary 1000000000000000000000001
1 0000 0000 0000 0000 0000 0001 ==
1.0000 0000 0000 0000 0000 0001 * 2^24
The significand of this number is 24 bits. But the signficand of a float is only 23 bits. This number will not fit into a float.
[ September 09, 2003: Message edited by: Marlene Miller ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I learned about floating point representation from Computer Organization & Design, The Hardware/Software Interface by David A. Patterson and John L. Hennessy, section 4.8
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Marlene.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is an article in the JavaRanch newsletter dealing with floats. See this: http://www.javaranch.com/newsletter/July2003/newsletterjuly2003.jsp#a4
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is one useful piece of information that I forgot to mention. The number 16777217 is special. It is the first (smallest) integer that will not fit into a float.
So if your integers stay under 16,777,217 you can be sure float f = i; is perfectly accurate.
reply
    Bookmark Topic Watch Topic
  • New Topic