Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 432
Flafla2 Avatar asked Apr 11 '11 23:04

Flafla2


People also ask

How is a float defined?

A float is a floating-point number, which means it is a number that has a decimal place. Floats are used when more precision is needed.

What is float and example?

A floating point number, is a positive or negative whole number with a decimal point. For example, 5.5, 0.25, and -103.342 are all floating point numbers, while 91, and 0 are not. Floating point numbers get their name from the way the decimal point can "float" to any position necessary.

What is a good float for a stock?

Float Percentage This is the percentage of the total shares of stock available for trading. Each trader has their preferences, but most look for a percentage between 10% and 25%.

What is float and why is it important?

Float: The float indicates how many shares are available for the general investing public to buy and sell. It does not include, among other things, restricted stock held by insiders. However, if insiders eventually sell their stock in the market, these shares become part of the float.


2 Answers

See

  • http://steve.hollasch.net/cgindex/coding/ieeefloat.html
  • http://en.wikipedia.org/wiki/IEEE_754-2008

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.

like image 62
Nicholas Carey Avatar answered Sep 24 '22 14:09

Nicholas Carey


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.

like image 38
Anton Avatar answered Sep 21 '22 14:09

Anton