Numerics question

K

kj

I define

ninv = 1.0/n

....where n is some integer, and I want to write some function f such
that f(m * ninv) returns the smallest integer that is >= m * ninv,
where m is some other integer. And, in particular, if m is p*n
for some integer p, then f((p*n) * ninv) should return the integer
p.

The first solution that comes to mind is something like

def f(x):
return int(math.ceil(x))

At first this seems to work:
7

....but there are values of n for which it fails:
8

The problem here is that, due to numerical error, the expression
((7*75) * (1.0/75)) evaluates to a number *just* above 7. The
surrounding math.ceil then turns this into 8.0, etc.

Is there a way to define f so that it behaves as expected?

TIA!

~K
 
K

kj

Please disregard my ineptly posed question.

~K





ninv = 1.0/n
...where n is some integer, and I want to write some function f such
that f(m * ninv) returns the smallest integer that is >= m * ninv,
where m is some other integer. And, in particular, if m is p*n
for some integer p, then f((p*n) * ninv) should return the integer
p.
The first solution that comes to mind is something like
def f(x):
return int(math.ceil(x))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top