I understand any fraction to be a quotient of integers which isn't 0, but after coming across the term "float" in various programming languages (such as JavaScript) I misunderstand why it is even needed and we don't say a fraction instead.

What is the difference between a fraction and a float?

  • $\begingroup$ en.wikipedia.org/wiki/Floating-point_arithmetic $\endgroup$ – Yuval Filmus Sep 15 '20 at 12:49
  • $\begingroup$ One important difference is that the properties of the arithmetic operations with fractions satisfy (as long as over/underflow doesn't occur) the properties that you are used to work with: commutativity, associativity, distributivity, etc. Floats don't satisfy the last two, for example. $\endgroup$ – plop Sep 15 '20 at 12:52
  • $\begingroup$ One reason for using floats instead of fractions is that to represent numbers that are very close together you might need large denominators. Floats (part of it) encode an exponential, instead of a binary number. So, you can get larger denominators represented. This comes at the cost of not being able to represent some numbers and losing the arithmetic properties mentioned above. $\endgroup$ – plop Sep 15 '20 at 12:56

Computers usually deal with floating-point numbers rather than with fractions. The main difference is that floating-point numbers have limited accuracy, but are much faster to perform arithmetic with (and are the only type of non-integer numbers supported natively in hardware).

Floating-point numbers are stored in "scientific notation" with a fixed accuracy, which depends on the datatype. Roughly speaking, they are stored in the form $\alpha \cdot 2^\beta$, where $1 \leq \alpha < 2$, $\beta$ is an integer, and both are stored in a fixed number of bits. This limits the accuracy of $\alpha$ and the range of $\beta$: if $\alpha$ is stored using $a$ bits (as $1.x_1\ldots x_a$) then it always expresses a fraction whose denominator is $2^a$, and if $\beta$ is stored using $b$ bits then it is always in the range $-2^{b-1},\ldots,2^{b-1}-1$.

Due to the limited accuracy of floating-point numbers, arithmetic on these numbers is only approximate, leading to numerical inaccuracies. When developing algorithms, you have to keep that in mind. There is actually an entire area in computer science, numerical analysis, devoted to such issues.

  • $\begingroup$ @guest Here what is meant by fixed accuracy is that the $\alpha$ is not any number between $1$ and $2$ (denoted as $[1,2)$), but it is restricted to be numbers between $1$ and $2$ with a number of non-zero digits bounded by some constant (the precision) depending on the data type. For example, this common type of floats only allows $\alpha$ to have at most $52$ non-zero (binary) digits ($53$ if we count the most significant digit). $\endgroup$ – plop Sep 15 '20 at 14:16
  • $\begingroup$ @plop thanks, what is the most significant digit of 52+1 in that case? That's where I lost you... $\endgroup$ – guest Sep 15 '20 at 14:18
  • $\begingroup$ @guest What I meant is that $\alpha$ can be $\mathbf{1}.\underbrace{11\ldots1}_{52}$ (this is a number in binary), but in that floating point type no more digits are stored. The first $1$ is the most significant digit. $\endgroup$ – plop Sep 15 '20 at 14:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.