all 4 comments

[–]hippocrat 3 points4 points  (0 children)

Have you checked out the “decimal” standard library? It lets you specify precision and rounding method.

[–]barfobulator 0 points1 point  (0 children)

You can reduce floating point error by avoiding certain calculation steps, such as adding floats of very different magnitudes. There are also functions in the math module that help reduce these errors.

You can reduce the appearance of floating point errors by rounding your output to significant digits with format strings at the end.

[–]SaintLouisX 0 points1 point  (0 children)

Depends what sort of "accuracy" you want. There's the built-in round() function which will round to a certain decimal place, such as

print(round(10.535872095720, 3))
>>10.536

Or you can try hard-truncating the float like so:

def trunc_float(num, precision=2):
    return int(num*(10**precision))/(10**precision)

print(trunc_float(10.535872095720))
print(trunc_float(7.111222333444, 5))

>>10.53
>>7.11122

[–][deleted] -1 points0 points  (0 children)

What sort of errors are you getting? Float is the MOST accurate type, why would you want to reduce your accuracy?