Skip to content
Advertisement

How to disable python rounding?

I’m looking for a way to disable this:

print(0+1e-20) returns 1e-20, but print(1+1e-20) returns 1.0

I want it to return something like 1+1e-20.

I need it because of this problem:

JavaScript

returns

JavaScript

f1 is the original function, f2 is f1 shifted by 1 to the left and f3 is f2 moved back by 1 to the right. By this logic, f1 and f3 should be equal to eachother, but this is not the case.

I know about decimal. Decimal and it doesn’t work, because decimal doesn’t support some functions including sin. If you could somehow make Decimal work for all functions, I’d like to know how.

Advertisement

Answer

As others have stated, in general this can’t be done (due to how computers commonly represent numbers).

It’s common to work with the precision you’ve got, ensuring that algorithms are numerically stable can be awkward.

In this case I’d redefine f1 to work on the logarithms of numbers, e.g.:

JavaScript

You might need to alter other parts of the code to work in log space as well depending on what you need to do. Note that most stats algorithms work with log-probabilities because it’s much more compatible with how computers represent numbers.

f3 is a bit more work due to the subtraction.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement