Flafla2
Flafla2

Reputation: 701

What exactly is a float?

Yeah, the noob question of the century... But in all seriousness, I actually have no idea what a float value actually is... And more importantly, the difference between a float and an integer.

Thanks in advance!

Upvotes: 9

Views: 1384

Answers (7)

Anton
Anton

Reputation: 3220

I want to expand on something that Nicholas Carey mentioned in his answer:

Floating point values, in software, trade magnitude for absolute precision (you've only got so many bits to spread around).

This is the reason why we call them "floating point numbers" - we allow the decimal point to "float" depending on how big the number that we want to write is.

Let's give an example in decimal notation. Suppose that you are given 5 cells to write down a number: _ _ _ _ _ . If you don't use decimal points, then you can represent numbers from 0 to 99999. However, the smallest difference that you can represent is 1.

Now you decide that you need to write down dollar amounts so you add a decimal point two digits from the left: _ _ _ . _ _ This is called fixed point arithmetic. Now you can only write numbers from 0 to 999.99, but you can now represent a difference of one cent or 0.01

What if you wanted to use this program for both small day-to-day expenses and for your income tax? You can allow the decimal point to 'float' and represent it's position using one of the digits: [_] _ _ _ _

For example, your bank interest may be [3] 4 7 6 5 representing 4.765, your grocery bill may be [2] 5 9 8 2 (59.82), your rent payment 1 8 7 5 9 ( 875.9 ) and your income tax return [0] 2 3 8 9 (2389). You may even allow the decimal point to venture outside the digits like this: [-1] 4 5 9 8 represents 4598x10 = 45,980.

Note that you can now represent both very small and very big numbers, but you cannot represent all numbers exactly. For example, in writing [0]2389 we loose the cents.

It is more conventional to always think of floating point numbers in scientific notation like 4.598x10^4 where '4.598' is called the significand, '4' is the exponent, and '10' is the radix. The links mentioned by others have more detail on the actual storage format.

Upvotes: 3

Nicholas Carey
Nicholas Carey

Reputation: 74257

See

Basically, the floating point data type is a way of storing a numeric value in scientific notation. You might write 2.3789 x 10^12, but since computers work in binary, it's a binary scientific notation.

Floating point values, in software, trade magnitude for absolute precision (you've only got so many bits to spread around).

Integers are...well..integers. The rightmost (low-order bit) represents 2^0 (e.g., 1), the next bit represents 2^1 (2s), the next 2^2 (4) and so forth. The leftmost (high-order) bit represents the sign (0 is positive, 1 is negative). Which brings us to negative values: they are represented in what's called two's-complement notation. To get the two's complement of a positive number:

  • flip all the bits: zeros become ones and ones become zeros.
  • add 1 to the result, doing all the usual carries.

So an eight-bit positive 1 (in binary, 00000001) get converted to its negative representation (11111111) by

  1. 00000001 The original value.
  2. 11111110 Flip its bits
  3. 11111111 Add 1

The advantage of two's complement notation is that addition and subtraction use the same circuitry: subtraction is implemented via addition: Using are +1/-1 example above, yields zero: add the rightmost bits giving you a binary 10 (decimal 2). Carry that to the next column, repeat the addition. The carry propagates out through the sign bit giving you the final value of 00000000.

There used to be systems that used other representations, but most (if not all) modern computers use two's-complement notation.

Then, of course, there is bit-order (big-endian v. little-endian) but that's another story.

Upvotes: 12

Scherbius.com
Scherbius.com

Reputation: 3414

float is a datatype.

http://en.wikipedia.org/wiki/Floating_point

Upvotes: 0

entropo
entropo

Reputation: 2481

In this case, Wikipedia is your best friend:

Though you should probably start just on the data types overview

Also, implementations of data types vary by language and processor architectures (e.g. integers are different numbers of bits on different architectures), but if using C as canon (C is the de-facto standard for system programming and is considered "just-above" assembly language in terms of abstraction) then these resources should be further enlightening:

Upvotes: 2

Trevor Arjeski
Trevor Arjeski

Reputation: 2168

Let me google this for you!

http://en.wikipedia.org/wiki/Primitive_data_type#Floating-point_numbers

Pretty interesting read for pros and beginners! Enjoy.

Upvotes: 0

Calum
Calum

Reputation: 5316

In basic terms:

Floating point value is a real number which is allowed to have decimal points.

Integer value has no decimal points (a whole number if you like)

Upvotes: 0

Related Questions