Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raising rational number to integer exponent yields floating-point result #2753

Open
dcnorris opened this issue Jan 6, 2025 · 7 comments
Open

Comments

@dcnorris
Copy link

dcnorris commented Jan 6, 2025

?- R is 1 rdiv 5, R2 is R^2.
   R = 1 rdiv 5, R2 = 0.04000000000000001.

Cf. #2752 (comment)

@UWN
Copy link

UWN commented Jan 6, 2025

9.3.1.2 Template and modes

'**'(int-exp, int-exp) = float
'**'(float-exp, int-exp) = float
'**'(int-exp, float-exp) = float
'**'(float-exp, float-exp) = float

?- X is 2**1.
   X = 2.0.

So (**)/2 produces always floats.

However, the situation is different for (^)/2.

@UWN
Copy link

UWN commented Jan 6, 2025

What do you expect in stead of:

?- X is 2 ^ (1 rdiv 2).
   X = 1.4142135623730951. % not sure about this

So far, (^)/2 is about integer exponentiation as long as the arguments are all integers. And in cases where integers would produce a non-integer value we get:

?- X is 2^ -1.
   error(type_error(float,2),(^)/2).
?- X is 2.0^ -1.
   X = 0.5.

It is not clear what your use case is. Maybe you need an entirely new function/evaluable functor.

@dcnorris
Copy link
Author

dcnorris commented Jan 6, 2025

I've corrected the original now, to omit the irrelevant (**)/2.
The point is that (^)/2 does not yield a rational when expected.

@UWN
Copy link

UWN commented Jan 6, 2025

Again, what about 2 ^ (1 rdiv 2)?

@dcnorris
Copy link
Author

dcnorris commented Jan 6, 2025

Only non-negative integer exponents are needed for my use-case, which involves computing the joint probability $\prod_{i=1}^k p_i^{n_i}$ of one permutation of $n_1, n_2, ..., n_k$ occurrences each of events with probabilities $p_1, p_2, ..., p_k$. I'm looking for confirmation that a probability sums to exactly 1, as an indication that a DCG has indeed accounted for every possible outcome, as in:
https://github.com/Precisfice/DEDUCTION/blob/ca1dba28100ceaaae673b782cf76cb87a42b5a6d/catform.pl#L665-L675

FWIW, doing this check did alert me to some missing sequences the DCG neglected to describe.

@UWN
Copy link

UWN commented Jan 6, 2025

Probabilities and exact sums? Sounds unusual to me. It seems you could live with a type error as shown above for integers, this time only for rationals that result in non-rational value.

@dcnorris
Copy link
Author

dcnorris commented Jan 6, 2025

With an arbitrary vector of rational toxicity-probabilities, one can rather easily assign an exact probability of occurrence to each possible trial realization described by declarative code (here, a DCG). The sum of these probabilities then gives a convenient check that all possibilities considered by the [relatively easy] probability calculation are accounted-for by the [more difficult] code. (I will even venture that the Girsanov theorem lends some justification to such arbitrary probability choices.)

At any rate, here's the result; it is at least much tidier now; floating-point is so gauche! ;)
https://github.com/Precisfice/DEDUCTION/blob/78434ed53bebc7b2420bed74a208bb9c513845d9/catform.pl#L634-L714

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants