Ask Your Question
0

Changing the meaning of /

asked 2022-10-12 01:04:05 +0200

John L gravatar image

I have a program that makes some calculations in a ring R, where R may be either approximate real numbers (for which I am using RealField(20), or approximate p-adic numbers, for which I use Zp(p,prec=numbits).

I need to do division. If R is RealField, I can just use /, but // is undefined. If R is Zp, / complains if the denominator is a non-unit. Fine, this makes sense, but I want it to makes its best guess, which is implemented by //.

Anyway, the formulas I have involve dividing. I'd rather use the same functions for some calculations, no matter what R is. How can I overload / in Zp to do what // does, even in the case of non-units? Or // in RealField() to be /?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-10-12 01:31:51 +0200

Max Alekseyev gravatar image

You can just can try / and if there is an exception, do // instead - like:

try:
    result = x / y
except ZeroDivisionError:
    result = x // y
edit flag offensive delete link more

Comments

Sure, this will work, but then I have to do it in every single line that involves division. There are a lot of them, it's not just one single line in the function. I'm hoping for a more efficient method that making two copies of the entire algorithm.

John L gravatar imageJohn L ( 2022-10-12 02:00:03 +0200 )edit
3

Define a division function and call it in place of /

Max Alekseyev gravatar imageMax Alekseyev ( 2022-10-12 06:15:51 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2022-10-12 01:04:05 +0200

Seen: 316 times

Last updated: Oct 12 '22