Just for verification: So Python can't store the value 1/3 as .3 repeating like a calculator can? So if I set a variable equal to 1/3 it won't actually be equal to .3 repeating? Seems quite unreasonable to me. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Just for verification: So Python can't store the value 1/3 as .3 repeating like a calculator can? So if I set a variable equal to 1/3 it won't actually be equal to .3 repeating? Seems quite unreasonable to me.

26th Jul 2016, 6:43 AM
Jason Li
Jason Li - avatar
4 Answers
+ 4
yes, storing money values is definitely a problem. while you probably will not try to store 1/3$ somewhere, you could try to store values like 0.1$ which have infinitely many digits in their binary representation and thus have certain error while storing them. that's why, in the databases, you have both floating point and decimal type. floating point sacrifises a bit of precision to be achieve more accuracy with smaller numbers while being less accurate with very big numbers which are not used as often, while decimal type is made to be precise for storing decimal numbers with certain number of digits. you could also store a number of pennies (10p instead of 0.1$) and use integers if your calculations are up to penny precision.
27th Jul 2016, 6:46 AM
RedAnt
RedAnt - avatar
+ 3
not sure if i should answer with yes or no, but i'll try to explain how it works so you can decide yourself. for storing decimal numbers most programming languages use floating point specification to store them in 32b or 64b locations. lets say that that python uses 64b (not realy sure if this is true, but it does not matter for the sake of explanation). that means that any decimal number must be represented with 64 values where each must be 1 or 0. this gives a finite amount of combinations ( very big finite, but still finite), so we come to the problem of representing some numbers, which gives some value error for them. if you execute print(0.2+0.1) you will get 0.30000000000000004 when you expect 0.3 . now this may seem like a terrible thing, since your program is not giving you correct answers, but note that the error is on the 17th digit, which means the error is very terribly small. so basically, while 1/3 cannot be acurately represented (since you dont have infinite amount of memory) , it can be represented acurately enough for most purposes. note that python may make some optimizations to get more accurate values in some situations which i am not aware of, but this is the basic idea.
26th Jul 2016, 7:40 AM
RedAnt
RedAnt - avatar
0
Thanks for the help RedAnt. I understand a bit better now. I feel like this might be a problem with money transactions but I not sure because money can't be represented by irrational numbers. Pretty new to this stuff.
26th Jul 2016, 8:59 PM
Jason Li
Jason Li - avatar
0
Thank you red ant, you are very helpful
4th Aug 2016, 10:09 PM
Narek Tonoyan
Narek Tonoyan - avatar