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

Num.mod_num behaves differently depending on its internal representation #6753

Closed
vicuna opened this issue Jan 19, 2015 · 5 comments
Closed

Comments

@vicuna
Copy link

vicuna commented Jan 19, 2015

Original bug ID: 6753
Reporter: furuse
Status: closed (set by @xavierleroy on 2016-12-07T10:49:10Z)
Resolution: fixed
Priority: normal
Severity: major
Version: 4.02.1
Target version: 4.02.2+dev / +rc1
Fixed in version: 4.02.2+dev / +rc1
Category: otherlibs
Monitored by: @gasche @hcarty

Bug description

Big_int.mod_big_int returns always 0 or positive, as stated in its document, i.e. (-5) mod 3 = 1 in Big_int.

However, in Num, the results are unstable:

mod_num (num_of_big_int (big_int_of_int (-5)) (num_of_big_int (big_int_of_int 3)) returns -2, probably because -5 and 3 are represented as Int (-5) and Int 3.

mod_num (Big_int (big_int_of_int (-5)) (Big_int (big_int_of_int 3)) returns 1.

Well, mod_num saids nothing about its results in details so it is not strictly a bug but this should be filed at least.

File attachments

@vicuna
Copy link
Author

vicuna commented Jan 19, 2015

Comment author: furuse

(* The following demonstrates the problem *)

open Num
open Big_int

let () = print_endline (string_of_int (-5 mod 3))

let mf = num_of_int (-5)
let t = num_of_int 3
let () = print_endline (string_of_num (mod_num mf t))

let mf = num_of_big_int (big_int_of_int (-5))
let t = num_of_big_int (big_int_of_int 3)
let () = print_endline (string_of_num (mod_num mf t))

let mf = Big_int (big_int_of_int (-5))
let t = Big_int (big_int_of_int 3)
let () = print_endline (string_of_num (mod_num mf t))

@vicuna
Copy link
Author

vicuna commented Feb 19, 2015

Comment author: @damiendoligez

Well, mod_num saids nothing about its results in details so it is not strictly a bug but this should be filed at least.

That's not really true: mod_num says it's the remainder of euclidean division, which is a complete formal specification. This bug is definitely a bug.

@vicuna
Copy link
Author

vicuna commented Feb 19, 2015

Comment author: @damiendoligez

It's worse than you think: there are 9 cases to test (for a and b each being one of Int, Big_int, Ratio).
See the attached test file:

When a and b are both positive, all is well.
When a is negative the Int/Int case is wrong.
When b is negative, the Ratio/* and */Ratio cases are wrong.

@vicuna
Copy link
Author

vicuna commented Mar 1, 2015

@vicuna
Copy link
Author

vicuna commented May 6, 2015

Comment author: @xavierleroy

Tentative fix in commit 16086 on version/4.02. Damien's nifty test passes. A code review by someone else would be welcome.

@vicuna vicuna closed this as completed Dec 7, 2016
@vicuna vicuna added this to the 4.02.2 milestone Mar 14, 2019
@vicuna vicuna added the bug label Mar 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant