Take the 2-minute tour ×

why do we need integers and floating in processor?thank you

share|improve this question
Smells like homework. –  S.Lott Jan 14 '10 at 3:38
I actually think this is a good question. When you've been in the business long enough, you take certain things for granted. It's a good idea to question your assumptions once in a while. –  Mark Ransom Jan 14 '10 at 3:49
I upvoted this question. It may smell like homework but it sure generated a small storm of discussion and I don't think everyone involved is a student. I know I'm not, though I am working from home today. –  High Performance Mark Jan 14 '10 at 16:56
add comment

7 Answers

Integers are for counting, floating-point numbers are for calculating. We have them both in maths (where they are called integers and real numbers respectively) so we need them in algorithms and in programs too. End of story.

Sure, the range of most fp number implementations is larger than the range of most integer implementations but I could invent a language tomorrow in which I allow 512-bit integers but only 16-bit floating-point numbers (1 sign bit, 3 exponent bits, 12 significand bits). The integers are still not closed under division and the floating-point numbers are still no use for counting because while there is a successor function on fp numbers there isn't on real numbers and we like to pretend that fp numbers are a close implementation of real numbers.

No, integers are not easier on the processor, the processor does fundamental boolean logic operations on bits. And if processor X1 does integer arithmetic faster than fp arithmetic, a trawl through the memory banks will find a counter example.

We don't even need fp numbers for fractions, we could use pairs of integers to represent numerator and denominator.

The absolute precision of integers is why we use them for counting. For all practical purposes the precision of existing fp implementations is enough (now, there's a wild claim to attract disagreement !)



share|improve this answer
FP numbers are NOT real numbers. Please don't give OP or anyone that idea. FP numbers are not closed over division, as you mention. Infinity is a floating-point number but neither a real or an int. –  Potatoswatter Jan 14 '10 at 5:49
Floating point is absolutely closed under division. When you divide two floating point values, you get another floating-point value. Always. It may not be exactly equal to the result you would get if they were real numbers, but that's not what closed means. Division of floating point values is defined to include this rounding. –  Stephen Canon Jan 14 '10 at 14:38
Oh, I think we've got a subtle terminology issue here. Potatoswatter said "FP number", and Stephen said "floating point value". What about NaN? It's a value which a floating-point variable can hold, but by its own admission it's Not a Number. Floating-point numbers are not closed under division, but values of floating-point variables are closed under division, right? –  Ken Jun 17 '10 at 22:17
add comment

They are all just numbers, so you might think you don't need to distinguish. But, in many languages there's an optimization possible when doing integer math - addition, subtraction, multiplying and dividing are possible with CPU instructions. Likewise there are instructions that do simnilar operations on floating point numbers, but because the numbers are represented differently in the machine, and operations are different, then it makes sense to bubble up the distinction between integers and floats that is apparent in the processor, into the programming language itself.

C#, Java, C++, and other languages all have distinct types for handling integers versus floats. Javascript makes the opposite choice - there is no special integer type, and if I am not mistaken, all numbers are floats.

As for why you need ints and floats - floats allow a much wider range of values, although at extreme ends (eg, astronomical quantities) the precision falls off. You can't represent 1.37999933247474x10e24 precisely in floating point math. On the other hand, ints offer precision and speed for a fixed set of numbers.

share|improve this answer
add comment

Integers are easier on processor resources, and often faster. This was a big deal many years ago, when processors didn't even come with built-in floating point capabilities. Not so much now, but the differences can still be significant in tight code.

Integers are often all that you need.

share|improve this answer
add comment

Floating point values have a [much] greater range than integers. Also, they can represent fractional values. These features however are provided at the cost of a loss of precision.

Edit: (what I mean by loss of precision)
Integer arithmetic is always exact, so long that one doesn't provide operand which cause an overflow, or a division by zero.
This is not the case with floating point arithmetic, whereby some portions of values may be lost when using such values in simple operations. The reason for this is that the tremendous range offered by floating point values, is such that it is impossible to represent all contiguous values within the range, given the [relatively] small storage (typically 8 or 16 bytes).

share|improve this answer
Not always. A 64-bit double has more precision than a 32-bit int. –  Mark Ransom Jan 14 '10 at 3:47
@Mark Ransom. regardless of variable size, an integer is always, within its range, 100% precise. That is unlike a float value, which within its range, may include parts which will be lost when the value is used for various, even simple, calculations. –  mjv Jan 14 '10 at 4:04
It is trivially easy to generate a constant that represents the largest floating point value that guarantees equal precision to an integer. Floating point often provides even greater guarantees. What do you get if you multiply 2147483647 x 2147483647 as integers? As doubles? –  Mark Ransom Jan 14 '10 at 4:19
@mjv: Any deterministic number system is 100% precise within its specification. Are you saying that int a = 1/2; is 100% precise in terms of grade-school arithmetic? –  Potatoswatter Jan 14 '10 at 4:41
@Potatoswatter maybe deterministic number system is the proper keyword then. Floating point arithmetic is just not DETERMINISTIC. Anyway... High-Performance Mark's provided the best overall answer; "our work is done" ;-) –  mjv Jan 14 '10 at 4:54
show 2 more comments

Integers are the most common things to use in programming tasks. They can represent memory addresses. It's easy to count from one integer to the next: just add one.

Floating-point values are used to approximate real numbers. Real numbers are the most common kind of thing in continuous math. Continuous math is used to represent the real world. (Hence the terminology "real number.")

Floating-point values cannot usually be used as integers. You can't easily count from X to the next number greater than X. They round off, and there is no guarantee that X + 1 is even a different number than X. Generally speaking, two floating-point numbers might be different if they were produced by different sequences of operations, even if the expressions are supposed to be equal.

Floating-point numbers are unpredictable, like real life. Integers are ordered and efficient, like computers.

share|improve this answer
Floating point numbers are completely predictable. It's not like your computer is returning a random number when you ask it to ad 1e-32 to 1e+32. It's just not simple, and certainly not intuitive to the casual user. You have to study the relevant implementation standard and be willing to work out the details on paper if need be. –  Charles E. Grant Jan 14 '10 at 5:11
Certain aspects of real life are predictable too, and become more predictable with more analysis. It's an analogy. –  Potatoswatter Jan 14 '10 at 5:41
add comment

In most applications floating-point numbers can be replaced by integers, by carefully defining what range of values needs to be represented at what precision, and multiplying with appropriate scaling factors. However, this is additional development effort, which is only worthwhile on small embedded platforms (i.e. small microcontrollers) which can't do the calculations in floating-point arithmetic in the available time.

With floating-point numbers you can get away without thinking about the representation of values most of the time, as long as you stay within the available range and precision. Unfortunately this is rather dangerous, because that way you may not notice when you leave the safe region.

share|improve this answer
add comment

A slightly different perpective: Integers are useful for digital quantities, while floats are useful for analogue quantities. An example, while looking at boats in the harbour, use ints to count the boats, use floats to represent the water level.

share|improve this answer
add comment

Your Answer


