4 ms·
Technically, much storage does not need voltage being high or low either. Pits burned with lasers, and magnetic changes are also common ways to store stuff that
by illumen 11y ago
Technically, much storage does not need voltage being high or low either. Pits burned with lasers, and magnetic changes are also common ways to store stuff that don't involve voltage in the stored state.
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.3
- jessaustin 11y agoHere is the Decimal type which does not have this float problem. Are you sure? >>> Decimal(0.1) Decimal('0.1000000000000000055511151231257827021181583404541015625')
- anon4 11y agoThis is a good case for macros in Python, actually. Fraction('0.1') is usually what you want, so Fraction(0.1) should mean just that and if you do want the current behaviour, Fraction(float(0.1)) would do it. Same for Fraction(17/3) for example.