my_float = 1.23333333333333333333333333333333333333 , why this value is precising to 1.23333333333 on running


python has excellent documentation on this:

very short version: computers count in base 2, where as humans count in base 10. Which causes problems


I think the thing to take with you is that floats are approximations, you must not treat them as exact values. (So it would for example be a very bad thing to use == on two floats (unless you know that float is able to represent those values and all intermediary values, but that’s usually just saying that you shouldn’t be using floats in the first place))

You could use some arbitrary (any amount you want) precision data type, but even then you’d have to say something like “fine, one million digits is enough” because computers don’t have infinite memory or processing power. The decimal.Decimal datatype supports arbitrary precision.

floats are supported directly by the hardware, processing units have circuitry for common operations like addition and subtraction for this number representation. (Extremely common operations are typically implemented in hardware and the rest get done with code by combining existing operations)


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.