2

by performing the following calculation

p=math.ceil(57050*1.12)

I am receiving 63897. However other tools (such as Excel) deliver 63896.

I need to use ceil (round up), because the procedure is part of a larger model, where I cannot make an exeption this specific value (57050). The problem is that I need to import a current model from Excel into Python and it is very important that all calculations deliver the same values.

I was wondering if there is a way to adjust the precision of "ceil" by e.g. 10 decimals. How can I adjust it, or what else can I do, that is not so severe, so that it is not going to create problems for other values?

Expected results: 63896

Actual results: 63897

ZetDen
  • 155
  • 8
  • 3
    Are you familiar with [the limitations of floating point](https://stackoverflow.com/questions/588004/is-floating-point-math-broken)? Have you considered using a different way to handle the numbers, such as the `decimal` standard library module? – Karl Knechtel Jul 19 '21 at 08:26

1 Answers1

0

Essentially, if you want to make only the first 10 decimal places relevant, you could do something like the following:

def ceil(n):
    return math.ceil(int(n*10**10)/10**10)
  • The answer solved my problem. Thanks for that. @ThierryLathuille I not sure what you mean, but I am interested. I have 40 values that pass the function and they were all rounded up in the same way Excel also did it. So I am basically fine. Do you think this approach will cause other problems in future? – ZetDen Jul 19 '21 at 08:53
  • @Thierry Lathuille I just answered the following question: `I was wondering if there is a way to adjust the precision of "ceil" by e.g. 10 decimals. How can I adjust it, or what else can I do, that is not so severe, so that it is not going to create problems for other values?` And the function I provided above does exactly that. – Freshman's Dream Jul 19 '21 at 08:53