Today I have started to use python and I am wondering... If I put a print(10/3)
the answer is limited to
3.3333333333333335
because obviously he couldn't write an infinite number... But is there a way to change the number of figures he writes? For example, instead of writing 16 decimal figures, write 20, or 3, or 45 (that I can regulate the number of figures, in conclusion)
The problem is not so much how many decimal places you want to display the number (which you can control with format strings,
"{:.10f}".format(10/3)
for example, as how much precision it's actually stored with.By default, float numbers are stored in a format called IEEE-754, which uses 32 or 64 bits (depending on single or double precision) to store any real number. Python uses double precision. Naturally, in 64 bits, which is the highest possible precision, a maximum of 2^64 different numbers can be represented, which is very far from the infinity of order Aleph1 that real numbers are. That is, there are infinite numbers not representable in this format.
When working with very small numbers (of the type 0.0000000...01) there is a very high precision, since numbers whose integer part is zero are represented in a special way in that format, being able to reach the order of 2^(− 1074 ), that is, correct up to decimal 324, but as the numbers get larger, the precision drops, since the number of bits available to represent the figures is constant, regardless of where the comma is.
Thus, for those of the type 1.00000...01 (which no longer has a zero integer part) it is only stored correctly up to approximately decimal 16 and as the integer part increases in size, the number of exact decimals in the part fraction is decreasing.
The result of 10/3 is stored correctly only up to decimal 15 and after that it is already wrong, as you can see if you try to display it with 20 decimal places:
In many scientific fields the precision provided by the
float
Python type (equivalent to thatdouble
of C) is sufficient, since it is also common in science that a lot of precision is wanted when working with small numbers, and a larger error can be tolerated when working with small numbers. large, so that the relative error is constant.However, if you wanted to have complete control over precision, you should stop using the type
float
and switch to using theDecimal
. This type has arbitrary precision, set by the programmer. Unfortunately it is more cumbersome to operate. See, for example, how to calculate 10/3 with precision of 30 correct decimals:Although with
format
you can reduce the number of decimal places displayed, if you try to increase it above 30 in this case the error is discovered:Finally, comment that you also have the fraction type, which does not try to perform the division but instead stores the numerator and denominator separately, which avoids rounding errors while working with fractions (but they will appear again when trying to convert the result to
float
)You could do the following:
You declare a variable and inside it you put the division:
Then you call it and format it
In this part you are telling it to take up to 5 decimal places {0:.5f}
The result is:
3.33333
If you change to 2 the result is as follows:
If what you want is to print the data with a specific number of decimal places, you can do the following:
Result
Note that the value has not been modified,
division_con_decimales
it still has the decimal places it had before. If what you want is to round to specific decimals, you can do:Result
Finally you can check that they are not equal by:
Result
An elegant and simple way to do this is with
math.trunc
And when executing it, it returns a value of the form.
What does the trick is that in
posiciones*numero
the number you entered, the number of digits is moved to the right, itmath.trunc
eliminates the remaining digits on the right and then, when dividing byposiciones
, the same digits that were previously moved to the right are traveled, but now toward the left.Namely.
If you entered 5.55555 and want three digits, first multiply 5.55555*10^3, so you get 5555.55.
math.trunc
you get only the 5555 and then that is divided by 10^3, so now 5555/10^3 = 5,555I saw that everyone uses the
.format()
but I found it curious that none useround()
Example
formula
That code would show you the result of the division with two decimal places.