by mehrdada · 11y ago · 0 comments
Technically, [digital] computers only natively "store" high and low voltages. The interpretation of a sequence of those signals as "integers" is entirely up to you.
In all seriousness, representing integers is not a given either (e.g. two's complement vs one's complement, BCD, bignum, sparse integers, etc.). If you are going down in abstraction levels to describe floating point representation, you should not just call integers "native" blindly.
Furthermore, floating point is not the only way to represent computation involving real values. It is indeed possible to represent computation on real values symbolically in many cases and lazily compute arbitrary precision results for output-printing purposes.
Here you can see python has a fractions.Fraction type which can be used for rational number arithmetic.
>>> from fractions import Fraction
>>> Fraction("0.1") + Fraction("0.2")
Fraction(3, 10)
>>> repr(float(Fraction("0.1") + Fraction("0.2")))
'0.3'
Here we see the floating point behaviour. >>> 0.1 + 0.2
0.30000000000000004
Here we see that python has a nice behaviour for when printing floats. >>> print(0.1 + 0.2)
0.3
Here is a common error where people feed the input of Fraction (and Decimal) with a float. Even though you use the Fraction here it still carries the floating point error through. >>> Fraction(0.1) + Fraction(0.2)
Fraction(10808639105689191, 36028797018963968)
>>> float(Fraction(0.1) + Fraction(0.2))
0.30000000000000004
Here is the Decimal type which does not have this float problem. Some people have argued (including myself) that Decimal should be used by python instead of float. Since this is much friendlier to people doing things like adding these numbers together. >>> from decimal import Decimal
>>> Decimal("0.1") + Decimal("0.2")
Decimal('0.3')
>>> float(Decimal("0.1") + Decimal("0.2"))
0.3Are you sure?
>>> Decimal(0.1)
Decimal('0.1000000000000000055511151231257827021181583404541015625')If I'm not mistaken, 0.3333... would be 0.1 and 0.6666... would be 0.2. It probably gets nasty for a lot of simple base 10 numbers, so it isn't a good solution.
And then there's MLC NAND flash which stores bit sequences as different voltage levels within the same cell.
also we don't have 5 year olds.