N
N
Nahs2016-02-12 09:07:27
Python
Nahs, 2016-02-12 09:07:27

Rounding numbers. What am I doing wrong?

Python 3.4
I have this code:

f2 = round(f, 3)
buffer = 0.001
if f2 < buffer: 
      r = {'f=':f,'round(f,3)=':f2,'buffer=':buffer}

I have this result from it:
{'f=': 3.564236791125941e-09, 'buffer=': 0.001, 'round(f,3)=': 0.0}
{'f=': 1.3266317874845862e-06, 'buffer=': 0.001, 'round(f,3)=': 0.0}
{'f=': 1.9855396039131223e-08, 'buffer=': 0.001, 'round(f,3)=': 0.0}
{'f=': 6.35513772582379e-06, 'buffer=': 0.001, 'round(f,3)=': 0.0}
{'f=': 1.7587640969915497e-06, 'buffer=': 0.001, 'round(f,3)=': 0.0}
{'f=': 2.832111881297117e-06, 'buffer=': 0.001, 'round(f,3)=': 0.0}
{'f=': 2.5486645882828902e-05, 'buffer=': 0.001, 'round(f,3)=': 0.0}
{'f=': 8.51481378687756e-06, 'buffer=': 0.001, 'round(f,3)=': 0.0}
{'f=': 3.240056853934595e-07, 'buffer=': 0.001, 'round(f,3)=': 0.0}
{'f=': 2.5071613169100337e-06, 'buffer=': 0.001, 'round(f,3)=': 0.0}
{'f=': 4.280439648979284e-07, 'buffer=': 0.001, 'round(f,3)=': 0.0}
{'f=': 4.778466944528603e-07, 'buffer=': 0.001, 'round(f,3)=': 0.0}
{'f=': 2.841350227730452e-06, 'buffer=': 0.001, 'round(f,3)=': 0.0}
{'f=': 4.24937996249021e-05, 'buffer=': 0.001, 'round(f,3)=': 0.0}

This is exactly what it outputs, despite the condition if
If we assign
else: 
   r = 0

then, outputs zeros
Please explain how rounding to the third decimal place translates fto f2?
What am I doing wrong and how can I do it right?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem Klimenko, 2016-02-12
@Nahs

>>> Decimal(0.0005)
Out[23]: Decimal('0.0005000000000000000104083408558608425664715468883514404296875')
>>> Decimal('0.0005')
Out[24]: Decimal('0.0005')
>>> round(Decimal('0.0005'), 3)
Out[25]: Decimal('0.000')
>>> round(Decimal(0.0005), 3)
Out[26]: Decimal('0.001')
>>> round(Decimal(0.0006), 3)
Out[27]: Decimal('0.001')
>>> round(Decimal(0.0004), 3)
Out[28]: Decimal('0.000')

If the number <= 0.0005, it will be rounded towards 0
, otherwise, towards 0.001 (of course, we are talking about numbers less than 0.001, you have questions about them)
Immediately note that Decimal(0.0005) != Decimal('0.0005')
PS asvetlov.blogspot.ru/2012/08/numerics.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question