Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

In a word, yes. You are using floating point accuracy, which means that 0.4 is not actually 0.4, but rather the closest the computer can get to it with a certain accuracy. In Sage, that default is 53 bits of precision:

sage: w.parent()
Real Field with 53 bits of precision
sage: w.str(truncate=False)
'0.40000000000000002'

For what you are looking for, you need arbitrary precision - which rationals would give you.

sage: w = 4/10
sage: while w > 1/10:
....:     w = w - 1/10
....:     print w
....: 
3/10
1/5
1/10

which also shows that your loop 'should have' ended earlier, but didn't because the number that printed 0.10000 actually was slightly larger than 0.1 in the computer's internal memory, so the loop iterated one more time.

I'm not an expert on numerical analysis or machine representation of numbers, but the Internet should have some good explanations of these things.