views:

133

answers:

7

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

A: 

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.

Cheeso
A: 

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.

Mark Ransom
A: 

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).

mjv
Not always. A 64-bit double has more precision than a 32-bit int.
Mark Ransom
@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
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
@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
@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
FP arithmetic is totally deterministic. If you do the same thing twice, you get the same results. You get error bits at the end of the number which I called "unpredictable" in my answer, yet I got called out too. Whatever.
Potatoswatter
@Potatoswatter: If you avoid overflow and division that doesn't produce an integral result, any mathematical identity will be an identity in integer arithmetic. The unsigned integral types in C form a perfect mathematical ring. Floating-point addition isn't even associative.
David Thornley
+1  A: 

Hi

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 !)

Regards

Mark

High Performance Mark
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
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
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
A: 

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.

Potatoswatter
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
Certain aspects of real life are predictable too, and become more predictable with more analysis. It's an analogy.
Potatoswatter
A: 

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.

starblue
A: 

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.

andora