ridgeworks / clpBNR

CLP(BNR) module for SWI-Prolog
MIT License
39 stars 6 forks source link

{2**X==1}. gives false #14

Closed kwon-young closed 2 years ago

kwon-young commented 2 years ago

Hello,

Thank you for this wonderful library. I think I found a bug with 0 exponents.

?- use_module(library(clpBNR)).
% *** clpBNR v0.9.12alpha ***.
true.

?- {2**N==1}.
false.

?- {2**0==D}.
D = 1.

Let me know if you need more information.

And if I do:

$ swipl
Welcome to SWI-Prolog (threaded, 64 bits, version 8.4.2)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.

For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).

?- debug(clpBNR).
Warning: clpBNR: no matching debug topic (yet)
true.

?- use_module(library(clpBNR)).
% *** clpBNR v0.9.12alpha ***.
true.

?- N::integer, {2**N==1}.
% Add {2**_10266{integer(-72057594037927936,72057594037927935)}==1}
[Thread 1]: failed to recover from global-overflow
C-stack trace labeled "out-of-stack":
  [0] Sopen_string() at ??:? [0x7f93ffe94dd7]
  [1] PL_get_long() at ??:? [0x7f93ffe63f59]
  [2] PL_next_solution() at ??:? [0x7f93ffdb06da]
  [3] PL_atom_generator_w() at ??:? [0x7f93ffd9e9d3]
  [4] PL_check_data() at ??:? [0x7f93ffdeb221]
  [5] PL_check_data() at ??:? [0x7f93ffdeb5cc]
  [6] PL_toplevel() at ??:? [0x7f93ffe6e381]
  [7] swipl(+0x10cd) [0x55a6fa4b60cd]
  [8] __libc_start_call_main() at ??:? [0x7f93ffb64590]
  [9] __libc_start_main_alias_2() at :? [0x7f93ffb64649]
  [10] swipl(+0x1115) [0x55a6fa4b6115]
C-stack trace labeled "crash":
  [0] Sopen_string() at ??:? [0x7f93ffe94dd7]
  [1] PL_get_long() at ??:? [0x7f93ffe64767]
  [2] PL_atom_generator_w() at ??:? [0x7f93ffd9e9d3]
  [3] PL_check_data() at ??:? [0x7f93ffdeb221]
  [4] PL_check_data() at ??:? [0x7f93ffdeb5cc]
  [5] PL_toplevel() at ??:? [0x7f93ffe6e381]
  [6] swipl(+0x10cd) [0x55a6fa4b60cd]
  [7] __libc_start_call_main() at ??:? [0x7f93ffb64590]
  [8] __libc_start_main_alias_2() at :? [0x7f93ffb64649]
  [9] swipl(+0x1115) [0x55a6fa4b6115]
[FATAL ERROR: at Sat May 21 10:16:59 2022
        Sorry, cannot continue]
[1]    458879 IOT instruction (core dumped)  swipl
ridgeworks commented 2 years ago

Sorry for the delay in responding. We've been without power for the last 5 days as the result of a bad storm.

Thanks for reporting this bug. The special cases with precise values are not currently handled well and need to be fixed. Imprecise values fair a little better:

?- {2**N==1.0}.
N::real(-1.6017132519074596e-16, 3.2034265038149186e-16).

?- N::integer(-10,10), {2**N==1.0}.
N = 0.

The crash occurs because N::integer produces, by default, an interval with very large endpoint values:

?- N::integer.
N::integer(-72057594037927936, 72057594037927935).

When the GMP library tries to generate the result of 2**N using these endpoints, the result is an integer so large it exceeds available memory and SWI-Prolog crashes. Currently this is expected behaviour of the GMP library supporting big integers and rationals. Note that if N is initially defined with a small range (as shown above), things work as expected.

I'll post a followup message here when I push the special case fix. Let me know if this is something you urgently need.

kwon-young commented 2 years ago

I'll post a followup message here when I push the special case fix. Let me know if this is something you urgently need.

Absolutely not :)

Thank you for your response, I now understand better what is happening and I'll make sure to specify the intervals on my variables.

I'll test the fix as soon as you push it

ridgeworks commented 2 years ago

I decided to push a small update fixing this issue rather than bundling it with current development a work. See Version 0.9.13 alpha.

Thanks again for reporting.

kwon-young commented 2 years ago

I've tested the change and now it is working as expected.

Thank you for your help !