-1

I am confused as to why there can be rounding errors in floating point numbers. Can someone show me an example or two? What are their min and max values? Does that mean I get every (well not every..if not how much?) number and decimal in between the min/max?

So if I wanted to hold a population of a country. i.e. USA, should I choose ints over floats for this purpose or would they have no effect. Memory, efficiency, taken into account. And what would be the answer if are not taken into account? The population count will be whole numbers.

These are questions are quite rudimentary, and on the web in lengthy detail, but I am just having trouble piecing them together as they get really complicated.

Thank you for your time.

PS: Please let me know via comment if this questions is badly worded. I will edit so it can get clearer.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
Mathew Kurian
  • 5,949
  • 5
  • 46
  • 73
  • 1
    Regarding the population of a country, I've never seen a country with a population containing fractions, it would look weird if a country had ten million _and a half_ citizen. – Some programmer dude Dec 08 '12 at 03:46
  • @JoachimPileborg: Basically, can it simply hold a whole number population. One doesn't have to increment by a decimal rite? – Mathew Kurian Dec 08 '12 at 03:48
  • @JoachimPileborg: What I am trying to ask is that although floats are used for decimal purposes, they can still work for population which are whole numbers? – Mathew Kurian Dec 08 '12 at 03:50
  • 2
    No, because then you will get trouble when doing arithmetics on the values. If you don't need a fraction (e.g. for populations) use a fraction-less type such as integers. I also recommend you to read [What Every Computer Scientist Should Know About Floating-Point Arithmetic](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html). – Some programmer dude Dec 08 '12 at 03:53
  • 1
    For a little more about floating point numbers, see this question (and answer): http://stackoverflow.com/q/10049533/937822 which talks about the "rounding errors". – lnafziger Dec 08 '12 at 04:23
  • 1
    At the moment, you can fit the population of any single country into a 32-bit signed integer (China is at about 1.3 billion), but the population of the world no longer fits into a 32-bit unsigned integer. Remember that a `float` uses 32-bits to store an approximation to a floating point number; it can't store more values than a 32-bit `int`. – Jonathan Leffler Dec 08 '12 at 05:48

1 Answers1

1

No you cannot should not use float. Both float & int are 32 bit storage types & you gain nothing range wise.

For example, if the max value of signed int 2147483647 is tried as a float gives you 2.14748e+09

So if it was a population count you lost the 3647.

The best you can use is a 64 bit type int (type long long in C/C++) in cases for a longer range.

Edit:

Regarding Efficiency, the most efficient type is always the int with word size of a machine.

Floating point numbers require the machine to use a math processing unit additionally.

So if it's a 32 bit machine, int 32 bit is the fastest & most efficient.

loxxy
  • 12,990
  • 2
  • 25
  • 56
  • 2
    This should say that you **should** not use floats. Of course he **can**. :-) – lnafziger Dec 08 '12 at 04:19
  • @lnafziger: If i have a float like 3,423,343.0 + 1. Does that guarantee 3,423,344.0? – Mathew Kurian Dec 08 '12 at 04:25
  • @bluejamesbond: Of course not, however lots and lots of programs are written that use floating point numbers, and very few decimal number are guaranteed to be an exact number. This doesn't mean that we can't use them. It is just more work. :) – lnafziger Dec 08 '12 at 04:27
  • @lnafziger: Thnx for the good explanation. I really suck at floating point I am having a hard time preparing for an upcoming exam. Nowhere close to the professionals here on stackoverflow. – Mathew Kurian Dec 08 '12 at 04:29
  • 1
    @bluejamesbond: I posted a link above to a question (which I answered some time ago) talking about floating point numbers and rounding issues. Perhaps that will help you understand! But, for what you are describing above, by far and away the best storage type would be one of the integer types.. – lnafziger Dec 08 '12 at 04:30
  • 1
    @lnafziger Right.. Edited that. – loxxy Dec 08 '12 at 05:46