{"action":"created","comment":{"url":"https://api.github.com/repos/drt24/gnuprologjava/pulls/comments/59493341","id":59493341,"diff_hunk":"@@ -0,0 +1,248 @@\n+/* GNU Prolog for Java\n+ * Copyright (C) 1997-1999  Constantine Plotnikov\n+ * Copyright (C) 2010       Daniel Thomas\n+ * Copyright (C) 2016       Matt Lilley\n+ * This library is free software; you can redistribute it and/or\n+ * modify it under the terms of the GNU Library General Public\n+ * License as published by the Free Software Foundation; either\n+ * version 3 of the License, or (at your option) any later version.\n+ * This library is distributed in the hope that it will be useful,\n+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\n+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n+ * Library General Public License for more details.\n+ *\n+ * You should have received a copy of the GNU Library General Public\n+ * License along with this library; if not, write to the\n+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,\n+ * Boston, MA  02111-1307, USA. The text of license can be also found\n+ * at http://www.gnu.org/copyleft/lgpl.html\n+ */\n+\n+package gnu.prolog.term;\n+import java.math.BigInteger;\n+import java.math.BigDecimal;\n+import java.math.MathContext;\n+\n+public class Rational implements Comparable\n+{\n+\tprivate MathContext mc = MathContext.DECIMAL64;\n+\tprivate BigInteger numerator;\n+\tprivate BigInteger denominator;\n+\tpublic static final BigInteger INTEGER_MIN = BigInteger.valueOf(Integer.MIN_VALUE);\n+\tpublic static final BigInteger INTEGER_MAX = BigInteger.valueOf(Integer.MAX_VALUE);\n+\n+\tpublic Rational(BigInteger numerator, BigInteger denominator)\n+\t{\n+\t\tif (denominator.equals(BigInteger.ZERO))\n+\t\t{\n+\t\t\tthrow new ArithmeticException(\"Division by zero\");\n+\t\t}\n+\t\tBigInteger gcd = numerator.gcd(denominator);\n+\t\tif (denominator.signum() == -1)\n+\t\t{\n+\t\t\t// multiply by -1/-1\n+\t\t\tnumerator = numerator.negate();\n+\t\t\tdenominator = denominator.negate();\n+\t\t}\n+\n+\t\tif (gcd.equals(BigInteger.ONE))\n+\t\t{\n+\t\t\t// Save 2 division operations here\n+\t\t\tthis.numerator = numerator;\n+\t\t\tthis.denominator = denominator;\n+\t\t}\n+\t\telse\n+\t\t{\n+\t\t\tthis.numerator = numerator.divide(gcd);\n+\t\t\tthis.denominator = denominator.divide(gcd);\n+\t\t}\n+\t}\n+\n+\t@Override\n+\tpublic boolean equals(Object o)\n+\t{\n+\t\treturn (o instanceof Rational &&\n+\t\t\t((Rational)o).numerator.equals(numerator) &&\n+\t\t\t((Rational)o).denominator.equals(denominator));\n+\t}\n+\n+\t@Override\n+\tpublic int compareTo(Object o)\n+\t{\n+\t\tif (o instanceof Rational)\n+\t\t{\n+\t\t\tRational subtrahend = (Rational)o;\n+\t\t\treturn numerator.multiply(subtrahend.denominator).subtract(subtrahend.numerator.multiply(denominator)).signum();\n+\t\t}\n+\t\treturn hashCode() - o.hashCode();\n+\t}\n+\n+\t@Override\n+\tpublic String toString()\n+\t{\n+\t\t// Note that this just prints out the value to 20dp\n+\t\t// We do have more precision than that, but we have to draw the line somewhere when asked to print\n+\t\treturn numerator.toString() + \" rdiv \" + denominator.toString();\n+\t\t//return new BigDecimal(numerator).divide(new BigDecimal(denominator), mc).toString();\n+\t}\n+\n+\tpublic double doubleValue()\n+\t{\n+\t\treturn new BigDecimal(numerator).divide(new BigDecimal(denominator), mc).doubleValue();\n+\t}\n+\n+\tpublic static Rational get(int val)\n+\t{\n+\t\treturn new Rational(BigInteger.valueOf(val), BigInteger.valueOf(1));\n+\t}\n+\n+\tpublic static Rational get(BigInteger val)\n+\t{\n+\t\treturn new Rational(val, BigInteger.valueOf(1));\n+\t}\n+\n+        private static final double DBL_EPSILON = 0.00000000000000022204;\n+        public static Rational getApproximate(double val)\n+        {\n+                // This is borrowed directly from SWI Prolog","path":"src/gnu/prolog/term/Rational.java","position":107,"original_position":107,"commit_id":"2ded73f92e3689d182b2736b9086f306343b3e94","original_commit_id":"d0ebb9db664a2f90b1a044542b2819973a4186ac","user":{"login":"thetrime","id":4109871,"avatar_url":"https://avatars.githubusercontent.com/u/4109871?v=3","gravatar_id":"","url":"https://api.github.com/users/thetrime","html_url":"https://github.com/thetrime","followers_url":"https://api.github.com/users/thetrime/followers","following_url":"https://api.github.com/users/thetrime/following{/other_user}","gists_url":"https://api.github.com/users/thetrime/gists{/gist_id}","starred_url":"https://api.github.com/users/thetrime/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/thetrime/subscriptions","organizations_url":"https://api.github.com/users/thetrime/orgs","repos_url":"https://api.github.com/users/thetrime/repos","events_url":"https://api.github.com/users/thetrime/events{/privacy}","received_events_url":"https://api.github.com/users/thetrime/received_events","type":"User","site_admin":false},"body":"I'm not sure. The code is inspired very directly by SWI Prolog, but the same algorithm is also implemented in YAP, and apparently in Common LISP. I've found another copy of the paper, at https://www.cs.duke.edu/brd/papers/src-papers/rotations-scg92.pdf and the algorithm is presented in a very similar format in figure 3.","created_at":"2016-04-13T05:09:31Z","updated_at":"2016-04-13T05:09:31Z","html_url":"https://github.com/drt24/gnuprologjava/pull/11#discussion_r59493341","pull_request_url":"https://api.github.com/repos/drt24/gnuprologjava/pulls/11","_links":{"self":{"href":"https://api.github.com/repos/drt24/gnuprologjava/pulls/comments/59493341"},"html":{"href":"https://github.com/drt24/gnuprologjava/pull/11#discussion_r59493341"},"pull_request":{"href":"https://api.github.com/repos/drt24/gnuprologjava/pulls/11"}}},"pull_request":{"url":"https://api.github.com/repos/drt24/gnuprologjava/pulls/11","id":65261136,"html_url":"https://github.com/drt24/gnuprologjava/pull/11","diff_url":"https://github.com/drt24/gnuprologjava/pull/11.diff","patch_url":"https://github.com/drt24/gnuprologjava/pull/11.patch","issue_url":"https://api.github.com/repos/drt24/gnuprologjava/issues/11","number":11,"state":"open","locked":false,"title":"Multiprecision Arithmetic","user":{"login":"thetrime","id":4109871,"avatar_url":"https://avatars.githubusercontent.com/u/4109871?v=3","gravatar_id":"","url":"https://api.github.com/users/thetrime","html_url":"https://github.com/thetrime","followers_url":"https://api.github.com/users/thetrime/followers","following_url":"https://api.github.com/users/thetrime/following{/other_user}","gists_url":"https://api.github.com/users/thetrime/gists{/gist_id}","starred_url":"https://api.github.com/users/thetrime/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/thetrime/subscriptions","organizations_url":"https://api.github.com/users/thetrime/orgs","repos_url":"https://api.github.com/users/thetrime/repos","events_url":"https://api.github.com/users/thetrime/events{/privacy}","received_events_url":"https://api.github.com/users/thetrime/received_events","type":"User","site_admin":false},"body":"This is quite a big change. It implements arbitrary[1] precision arithmetic as follows:\r\n   * Integers are now represented by IntegerTerm, if possible, or BigIntegerTerm, if not\r\n      * This means that instead of IntegerTerm.get(String image) the parser must now call NumericTerm.get(String image), which returns an appropriate numeric term\r\n   * Rational numbers of arbitrary[1] precision are represented by RationalTerm\r\n   * You can create a rational using arithmetic functions rdiv/2, rationalize/1 or rational/1\r\n   * Availability of this is all determined by vm.Evaluate.isUnbounded. If false, then very large integers parsed from the source turn into floats instead, and the rational functions are not supported (you'll get an evaluation error if you try to use them). The value of this is available via current_prolog_flag(bounded, X)\r\n\r\nI've also added 39 tests (of which 18 are skipped if unbounded arithmetic is disabled). These turned up a few existing problems, like (5 >> 64) being given as 5 due to the way Java handles >>. I've fixed those here too.\r\n\r\nFinally, I've added another flag vm.Evaluate.strictISO. If this is turned off, then we allow things that ISO doesn't, but make a lot of sense logically speaking. For example, float_fractional_part(3) gives 0, and float_integer_part(3) returns 3, even though ISO says we should raise a type error. Similarly, rounding functions on integers also just echo their input. It also recognises the atom nan as a FloatTerm with value Float.NaN, implements div/2 as integer division that rounds toward -infinity (which is useful since it is guaranteed to be consistent with mod/2), and implements gcd/2 as the GCD of 2 [big]integers\r\n\r\nThis should probably be documented somewhere, but I'm not entirely sure where. A new section of the manual? Under 'extending'? Is there a sensible way to configure the isUnbounded and/or strictISO flags?  Maybe there's some ant option where we can generate a .java file and include that in the build? \r\n\r\n(Given that you definitely do NOT want to change them during execution, and it seems like it'd be a real effort to provide access to the Environment or Interpreter while /parsing/, I think this is a compile-time setting, not a runtime one)\r\n\r\nYou might want to bump the version number for this as well (to 0.3.1 or 0.4.0).\r\n\r\n1: (subject to available memory, of course)","created_at":"2016-04-05T07:18:26Z","updated_at":"2016-04-13T05:09:31Z","closed_at":null,"merged_at":null,"merge_commit_sha":"34f18dfe53ae9d9752247c9441dc05ebd156ba26","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/drt24/gnuprologjava/pulls/11/commits","review_comments_url":"https://api.github.com/repos/drt24/gnuprologjava/pulls/11/comments","review_comment_url":"https://api.github.com/repos/drt24/gnuprologjava/pulls/comments{/number}","comments_url":"https://api.github.com/repos/drt24/gnuprologjava/issues/11/comments","statuses_url":"https://api.github.com/repos/drt24/gnuprologjava/statuses/2ded73f92e3689d182b2736b9086f306343b3e94","head":{"label":"thetrime:mp","ref":"mp","sha":"2ded73f92e3689d182b2736b9086f306343b3e94","user":{"login":"thetrime","id":4109871,"avatar_url":"https://avatars.githubusercontent.com/u/4109871?v=3","gravatar_id":"","url":"https://api.github.com/users/thetrime","html_url":"https://github.com/thetrime","followers_url":"https://api.github.com/users/thetrime/followers","following_url":"https://api.github.com/users/thetrime/following{/other_user}","gists_url":"https://api.github.com/users/thetrime/gists{/gist_id}","starred_url":"https://api.github.com/users/thetrime/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/thetrime/subscriptions","organizations_url":"https://api.github.com/users/thetrime/orgs","repos_url":"https://api.github.com/users/thetrime/repos","events_url":"https://api.github.com/users/thetrime/events{/privacy}","received_events_url":"https://api.github.com/users/thetrime/received_events","type":"User","site_admin":false},"repo":{"id":52710084,"name":"gnuprologjava","full_name":"thetrime/gnuprologjava","owner":{"login":"thetrime","id":4109871,"avatar_url":"https://avatars.githubusercontent.com/u/4109871?v=3","gravatar_id":"","url":"https://api.github.com/users/thetrime","html_url":"https://github.com/thetrime","followers_url":"https://api.github.com/users/thetrime/followers","following_url":"https://api.github.com/users/thetrime/following{/other_user}","gists_url":"https://api.github.com/users/thetrime/gists{/gist_id}","starred_url":"https://api.github.com/users/thetrime/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/thetrime/subscriptions","organizations_url":"https://api.github.com/users/thetrime/orgs","repos_url":"https://api.github.com/users/thetrime/repos","events_url":"https://api.github.com/users/thetrime/events{/privacy}","received_events_url":"https://api.github.com/users/thetrime/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/thetrime/gnuprologjava","description":"GNU Prolog for Java","fork":true,"url":"https://api.github.com/repos/thetrime/gnuprologjava","forks_url":"https://api.github.com/repos/thetrime/gnuprologjava/forks","keys_url":"https://api.github.com/repos/thetrime/gnuprologjava/keys{/key_id}","collaborators_url":"https://api.github.com/repos/thetrime/gnuprologjava/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/thetrime/gnuprologjava/teams","hooks_url":"https://api.github.com/repos/thetrime/gnuprologjava/hooks","issue_events_url":"https://api.github.com/repos/thetrime/gnuprologjava/issues/events{/number}","events_url":"https://api.github.com/repos/thetrime/gnuprologjava/events","assignees_url":"https://api.github.com/repos/thetrime/gnuprologjava/assignees{/user}","branches_url":"https://api.github.com/repos/thetrime/gnuprologjava/branches{/branch}","tags_url":"https://api.github.com/repos/thetrime/gnuprologjava/tags","blobs_url":"https://api.github.com/repos/thetrime/gnuprologjava/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/thetrime/gnuprologjava/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/thetrime/gnuprologjava/git/refs{/sha}","trees_url":"https://api.github.com/repos/thetrime/gnuprologjava/git/trees{/sha}","statuses_url":"https://api.github.com/repos/thetrime/gnuprologjava/statuses/{sha}","languages_url":"https://api.github.com/repos/thetrime/gnuprologjava/languages","stargazers_url":"https://api.github.com/repos/thetrime/gnuprologjava/stargazers","contributors_url":"https://api.github.com/repos/thetrime/gnuprologjava/contributors","subscribers_url":"https://api.github.com/repos/thetrime/gnuprologjava/subscribers","subscription_url":"https://api.github.com/repos/thetrime/gnuprologjava/subscription","commits_url":"https://api.github.com/repos/thetrime/gnuprologjava/commits{/sha}","git_commits_url":"https://api.github.com/repos/thetrime/gnuprologjava/git/commits{/sha}","comments_url":"https://api.github.com/repos/thetrime/gnuprologjava/comments{/number}","issue_comment_url":"https://api.github.com/repos/thetrime/gnuprologjava/issues/comments{/number}","contents_url":"https://api.github.com/repos/thetrime/gnuprologjava/contents/{+path}","compare_url":"https://api.github.com/repos/thetrime/gnuprologjava/compare/{base}...{head}","merges_url":"https://api.github.com/repos/thetrime/gnuprologjava/merges","archive_url":"https://api.github.com/repos/thetrime/gnuprologjava/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/thetrime/gnuprologjava/downloads","issues_url":"https://api.github.com/repos/thetrime/gnuprologjava/issues{/number}","pulls_url":"https://api.github.com/repos/thetrime/gnuprologjava/pulls{/number}","milestones_url":"https://api.github.com/repos/thetrime/gnuprologjava/milestones{/number}","notifications_url":"https://api.github.com/repos/thetrime/gnuprologjava/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/thetrime/gnuprologjava/labels{/name}","releases_url":"https://api.github.com/repos/thetrime/gnuprologjava/releases{/id}","deployments_url":"https://api.github.com/repos/thetrime/gnuprologjava/deployments","created_at":"2016-02-28T07:29:53Z","updated_at":"2016-02-28T07:29:54Z","pushed_at":"2016-04-12T16:46:28Z","git_url":"git://github.com/thetrime/gnuprologjava.git","ssh_url":"git@github.com:thetrime/gnuprologjava.git","clone_url":"https://github.com/thetrime/gnuprologjava.git","svn_url":"https://github.com/thetrime/gnuprologjava","homepage":null,"size":2629,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"drt24:master","ref":"master","sha":"7fe633c0dd07dfda0e5e2afb9c4eb0088b6c2d08","user":{"login":"drt24","id":314854,"avatar_url":"https://avatars.githubusercontent.com/u/314854?v=3","gravatar_id":"","url":"https://api.github.com/users/drt24","html_url":"https://github.com/drt24","followers_url":"https://api.github.com/users/drt24/followers","following_url":"https://api.github.com/users/drt24/following{/other_user}","gists_url":"https://api.github.com/users/drt24/gists{/gist_id}","starred_url":"https://api.github.com/users/drt24/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/drt24/subscriptions","organizations_url":"https://api.github.com/users/drt24/orgs","repos_url":"https://api.github.com/users/drt24/repos","events_url":"https://api.github.com/users/drt24/events{/privacy}","received_events_url":"https://api.github.com/users/drt24/received_events","type":"User","site_admin":false},"repo":{"id":10065353,"name":"gnuprologjava","full_name":"drt24/gnuprologjava","owner":{"login":"drt24","id":314854,"avatar_url":"https://avatars.githubusercontent.com/u/314854?v=3","gravatar_id":"","url":"https://api.github.com/users/drt24","html_url":"https://github.com/drt24","followers_url":"https://api.github.com/users/drt24/followers","following_url":"https://api.github.com/users/drt24/following{/other_user}","gists_url":"https://api.github.com/users/drt24/gists{/gist_id}","starred_url":"https://api.github.com/users/drt24/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/drt24/subscriptions","organizations_url":"https://api.github.com/users/drt24/orgs","repos_url":"https://api.github.com/users/drt24/repos","events_url":"https://api.github.com/users/drt24/events{/privacy}","received_events_url":"https://api.github.com/users/drt24/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/drt24/gnuprologjava","description":"GNU Prolog for Java","fork":false,"url":"https://api.github.com/repos/drt24/gnuprologjava","forks_url":"https://api.github.com/repos/drt24/gnuprologjava/forks","keys_url":"https://api.github.com/repos/drt24/gnuprologjava/keys{/key_id}","collaborators_url":"https://api.github.com/repos/drt24/gnuprologjava/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/drt24/gnuprologjava/teams","hooks_url":"https://api.github.com/repos/drt24/gnuprologjava/hooks","issue_events_url":"https://api.github.com/repos/drt24/gnuprologjava/issues/events{/number}","events_url":"https://api.github.com/repos/drt24/gnuprologjava/events","assignees_url":"https://api.github.com/repos/drt24/gnuprologjava/assignees{/user}","branches_url":"https://api.github.com/repos/drt24/gnuprologjava/branches{/branch}","tags_url":"https://api.github.com/repos/drt24/gnuprologjava/tags","blobs_url":"https://api.github.com/repos/drt24/gnuprologjava/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/drt24/gnuprologjava/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/drt24/gnuprologjava/git/refs{/sha}","trees_url":"https://api.github.com/repos/drt24/gnuprologjava/git/trees{/sha}","statuses_url":"https://api.github.com/repos/drt24/gnuprologjava/statuses/{sha}","languages_url":"https://api.github.com/repos/drt24/gnuprologjava/languages","stargazers_url":"https://api.github.com/repos/drt24/gnuprologjava/stargazers","contributors_url":"https://api.github.com/repos/drt24/gnuprologjava/contributors","subscribers_url":"https://api.github.com/repos/drt24/gnuprologjava/subscribers","subscription_url":"https://api.github.com/repos/drt24/gnuprologjava/subscription","commits_url":"https://api.github.com/repos/drt24/gnuprologjava/commits{/sha}","git_commits_url":"https://api.github.com/repos/drt24/gnuprologjava/git/commits{/sha}","comments_url":"https://api.github.com/repos/drt24/gnuprologjava/comments{/number}","issue_comment_url":"https://api.github.com/repos/drt24/gnuprologjava/issues/comments{/number}","contents_url":"https://api.github.com/repos/drt24/gnuprologjava/contents/{+path}","compare_url":"https://api.github.com/repos/drt24/gnuprologjava/compare/{base}...{head}","merges_url":"https://api.github.com/repos/drt24/gnuprologjava/merges","archive_url":"https://api.github.com/repos/drt24/gnuprologjava/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/drt24/gnuprologjava/downloads","issues_url":"https://api.github.com/repos/drt24/gnuprologjava/issues{/number}","pulls_url":"https://api.github.com/repos/drt24/gnuprologjava/pulls{/number}","milestones_url":"https://api.github.com/repos/drt24/gnuprologjava/milestones{/number}","notifications_url":"https://api.github.com/repos/drt24/gnuprologjava/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/drt24/gnuprologjava/labels{/name}","releases_url":"https://api.github.com/repos/drt24/gnuprologjava/releases{/id}","deployments_url":"https://api.github.com/repos/drt24/gnuprologjava/deployments","created_at":"2013-05-14T21:31:02Z","updated_at":"2016-03-24T17:26:15Z","pushed_at":"2016-04-12T16:46:11Z","git_url":"git://github.com/drt24/gnuprologjava.git","ssh_url":"git@github.com:drt24/gnuprologjava.git","clone_url":"https://github.com/drt24/gnuprologjava.git","svn_url":"https://github.com/drt24/gnuprologjava","homepage":null,"size":2636,"stargazers_count":2,"watchers_count":2,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":4,"mirror_url":null,"open_issues_count":7,"forks":4,"open_issues":7,"watchers":2,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/drt24/gnuprologjava/pulls/11"},"html":{"href":"https://github.com/drt24/gnuprologjava/pull/11"},"issue":{"href":"https://api.github.com/repos/drt24/gnuprologjava/issues/11"},"comments":{"href":"https://api.github.com/repos/drt24/gnuprologjava/issues/11/comments"},"review_comments":{"href":"https://api.github.com/repos/drt24/gnuprologjava/pulls/11/comments"},"review_comment":{"href":"https://api.github.com/repos/drt24/gnuprologjava/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/drt24/gnuprologjava/pulls/11/commits"},"statuses":{"href":"https://api.github.com/repos/drt24/gnuprologjava/statuses/2ded73f92e3689d182b2736b9086f306343b3e94"}}}}