Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

ints, floats and doubles

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would appreciate a brief overview of these primitive data types. When coding I sometimes notice a loss of precision when moving from one to the other (especially int to float).
Thanks!
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Surely I can answer this without even referring to Java? A floating point number is wider and more precise than an integer. I.e., 1.0005 is not the same as 1. A "double" (64 bit floating point) is wider than a "float" (32 bit floating point) which is wider than an "int" (32 bit integer). Perhaps I'm wrong in assuming this is just a simple application of maths ...
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may notice a loss of precision going from int-to-float when you're dealing with large numbers. With small numbers (certainly upto 1-million - probably MUCH beyond that as well, but you get my drift) the numbers should match well.
Why is this? Well, if you're really techie, read on - but beware! I'm writing this turned midnight with no reference books to hand
Consider this: Both int and float are 32 bits. Now, 32 bits can take on 2**32 distinct "values" (that is, 2-raised-to-32nd power - ie. 2 times 2 times 2 ..... 32 times). Therefore, both int and float can only have this number of distinct values as well.
The int type holds these as precise integers upto (plus-or-minus) 2**16.
The float type holds a fantastically wider range of numbers: offhand, let's say from 10**-99 through to 10**99 (positive and negative).
BUT.....
You can't get something for nothing. The float type has a wider range of values, but remember that it can still only hold 2**32 distinct values. That means, if there is a "good" match with integers around zero, there has to be a "worse" match with integers far from zero - ie. BIG numbers.
 
To avoid criticism do nothing, say nothing, be nothing. -Elbert Hubbard. Please critique this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic