{"action":"created","comment":{"url":"https://api.github.com/repos/project-rig/rig/pulls/comments/63196242","id":63196242,"diff_hunk":"@@ -1,16 +1,123 @@\n \"\"\"Fixed point conversion utilities.\n \"\"\"\n import numpy as np\n+import warnings\n \n \n-def float_to_fix(signed, n_bits, n_frac):\n+def float_to_fp(signed, n_bits, n_frac):\n     \"\"\"Return a function to convert a floating point value to a fixed point\n     value.\n \n     For example, a function to convert a float to a signed fractional\n     representation with 8 bits overall and 4 fractional bits (S3.4) can be\n     constructed and used with::\n \n+        >>> s34 = float_to_fp(signed=True, n_bits=8, n_frac=4)\n+        >>> hex(int(s34(0.5)))\n+        '0x8'\n+\n+    The fixed point conversion is saturating::\n+\n+        >>> q34 = float_to_fp(False, 8, 4)  # Unsigned 4.4\n+        >>> hex(int(q34(-0.5)))\n+        '0x0'\n+\n+        >>> hex(int(q34(15.0)))\n+        '0xf0'\n+\n+        >>> hex(int(q34(16.0)))\n+        '0xff'\n+\n+    Parameters\n+    ----------\n+    signed : bool\n+        Whether the values that are to be converted should be signed, or\n+        clipped at zero.\n+\n+            >>> hex(int(float_to_fp(True, 8, 4)(-0.5)))  # Signed\n+            '-0x8'\n+            >>> hex(int(float_to_fp(False, 8, 4)(-0.5)))  # Unsigned\n+            '0x0'\n+\n+    n_bits : int\n+        Total number of bits in the fixed-point representation (including sign\n+        bit and fractional bits).\n+    n_frac : int\n+        Number of fractional bits in the fixed-point representation.\n+    \"\"\"\n+    # Calculate the maximum and minimum values\n+    if signed:\n+        max_v = (1 << (n_bits - 1)) - 1\n+        min_v = -max_v - 1\n+    else:\n+        min_v = 0\n+        max_v = (1 << n_bits) - 1\n+\n+    # Compute the scale\n+    scale = 2.0**n_frac\n+\n+    def bitsk(value):\n+        \"\"\"Convert a floating point value to a fixed point value.\n+\n+        Parameters\n+        ----------\n+        value : float\n+            The value to convert.\n+        \"\"\"\n+        int_val = int(scale * value)\n+        return np.clip(int_val, min_v, max_v)","path":"rig/type_casts.py","position":69,"original_position":69,"commit_id":"71b4c76dca40f49512eb5dc15f06c72973eee170","original_commit_id":"71b4c76dca40f49512eb5dc15f06c72973eee170","user":{"login":"neworderofjamie","id":6793242,"avatar_url":"https://avatars.githubusercontent.com/u/6793242?v=3","gravatar_id":"","url":"https://api.github.com/users/neworderofjamie","html_url":"https://github.com/neworderofjamie","followers_url":"https://api.github.com/users/neworderofjamie/followers","following_url":"https://api.github.com/users/neworderofjamie/following{/other_user}","gists_url":"https://api.github.com/users/neworderofjamie/gists{/gist_id}","starred_url":"https://api.github.com/users/neworderofjamie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/neworderofjamie/subscriptions","organizations_url":"https://api.github.com/users/neworderofjamie/orgs","repos_url":"https://api.github.com/users/neworderofjamie/repos","events_url":"https://api.github.com/users/neworderofjamie/events{/privacy}","received_events_url":"https://api.github.com/users/neworderofjamie/received_events","type":"User","site_admin":false},"body":"I had this precise fear, on my machine it seems to work correctly, but on numpy 0.8 who knows","created_at":"2016-05-13T14:51:29Z","updated_at":"2016-05-13T14:51:29Z","html_url":"https://github.com/project-rig/rig/pull/245#discussion_r63196242","pull_request_url":"https://api.github.com/repos/project-rig/rig/pulls/245","_links":{"self":{"href":"https://api.github.com/repos/project-rig/rig/pulls/comments/63196242"},"html":{"href":"https://github.com/project-rig/rig/pull/245#discussion_r63196242"},"pull_request":{"href":"https://api.github.com/repos/project-rig/rig/pulls/245"}}},"pull_request":{"url":"https://api.github.com/repos/project-rig/rig/pulls/245","id":69978366,"html_url":"https://github.com/project-rig/rig/pull/245","diff_url":"https://github.com/project-rig/rig/pull/245.diff","patch_url":"https://github.com/project-rig/rig/pull/245.patch","issue_url":"https://api.github.com/repos/project-rig/rig/issues/245","number":245,"state":"open","locked":false,"title":"Improve type cast utilities","user":{"login":"mundya","id":3439837,"avatar_url":"https://avatars.githubusercontent.com/u/3439837?v=3","gravatar_id":"","url":"https://api.github.com/users/mundya","html_url":"https://github.com/mundya","followers_url":"https://api.github.com/users/mundya/followers","following_url":"https://api.github.com/users/mundya/following{/other_user}","gists_url":"https://api.github.com/users/mundya/gists{/gist_id}","starred_url":"https://api.github.com/users/mundya/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mundya/subscriptions","organizations_url":"https://api.github.com/users/mundya/orgs","repos_url":"https://api.github.com/users/mundya/repos","events_url":"https://api.github.com/users/mundya/events{/privacy}","received_events_url":"https://api.github.com/users/mundya/received_events","type":"User","site_admin":false},"body":"New methods `float_to_fp` and `fp_to_float` work with signed and unsigned values as expected. New methods and `NumpyFloatToFixConverter` can now have negative numbers of fractional bits or more fractional bits than can be represented in the result - as requested by @neworderofjamie.","created_at":"2016-05-13T11:01:13Z","updated_at":"2016-05-13T14:51:29Z","closed_at":null,"merged_at":null,"merge_commit_sha":"5aeb223cc0e9000df21d4112e8dcfb469fb790f4","assignee":{"login":"neworderofjamie","id":6793242,"avatar_url":"https://avatars.githubusercontent.com/u/6793242?v=3","gravatar_id":"","url":"https://api.github.com/users/neworderofjamie","html_url":"https://github.com/neworderofjamie","followers_url":"https://api.github.com/users/neworderofjamie/followers","following_url":"https://api.github.com/users/neworderofjamie/following{/other_user}","gists_url":"https://api.github.com/users/neworderofjamie/gists{/gist_id}","starred_url":"https://api.github.com/users/neworderofjamie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/neworderofjamie/subscriptions","organizations_url":"https://api.github.com/users/neworderofjamie/orgs","repos_url":"https://api.github.com/users/neworderofjamie/repos","events_url":"https://api.github.com/users/neworderofjamie/events{/privacy}","received_events_url":"https://api.github.com/users/neworderofjamie/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/project-rig/rig/pulls/245/commits","review_comments_url":"https://api.github.com/repos/project-rig/rig/pulls/245/comments","review_comment_url":"https://api.github.com/repos/project-rig/rig/pulls/comments{/number}","comments_url":"https://api.github.com/repos/project-rig/rig/issues/245/comments","statuses_url":"https://api.github.com/repos/project-rig/rig/statuses/71b4c76dca40f49512eb5dc15f06c72973eee170","head":{"label":"project-rig:better-fp","ref":"better-fp","sha":"71b4c76dca40f49512eb5dc15f06c72973eee170","user":{"login":"project-rig","id":11227885,"avatar_url":"https://avatars.githubusercontent.com/u/11227885?v=3","gravatar_id":"","url":"https://api.github.com/users/project-rig","html_url":"https://github.com/project-rig","followers_url":"https://api.github.com/users/project-rig/followers","following_url":"https://api.github.com/users/project-rig/following{/other_user}","gists_url":"https://api.github.com/users/project-rig/gists{/gist_id}","starred_url":"https://api.github.com/users/project-rig/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/project-rig/subscriptions","organizations_url":"https://api.github.com/users/project-rig/orgs","repos_url":"https://api.github.com/users/project-rig/repos","events_url":"https://api.github.com/users/project-rig/events{/privacy}","received_events_url":"https://api.github.com/users/project-rig/received_events","type":"Organization","site_admin":false},"repo":{"id":29497314,"name":"rig","full_name":"project-rig/rig","owner":{"login":"project-rig","id":11227885,"avatar_url":"https://avatars.githubusercontent.com/u/11227885?v=3","gravatar_id":"","url":"https://api.github.com/users/project-rig","html_url":"https://github.com/project-rig","followers_url":"https://api.github.com/users/project-rig/followers","following_url":"https://api.github.com/users/project-rig/following{/other_user}","gists_url":"https://api.github.com/users/project-rig/gists{/gist_id}","starred_url":"https://api.github.com/users/project-rig/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/project-rig/subscriptions","organizations_url":"https://api.github.com/users/project-rig/orgs","repos_url":"https://api.github.com/users/project-rig/repos","events_url":"https://api.github.com/users/project-rig/events{/privacy}","received_events_url":"https://api.github.com/users/project-rig/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/project-rig/rig","description":"A collection of tools for developing SpiNNaker applications.","fork":false,"url":"https://api.github.com/repos/project-rig/rig","forks_url":"https://api.github.com/repos/project-rig/rig/forks","keys_url":"https://api.github.com/repos/project-rig/rig/keys{/key_id}","collaborators_url":"https://api.github.com/repos/project-rig/rig/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/project-rig/rig/teams","hooks_url":"https://api.github.com/repos/project-rig/rig/hooks","issue_events_url":"https://api.github.com/repos/project-rig/rig/issues/events{/number}","events_url":"https://api.github.com/repos/project-rig/rig/events","assignees_url":"https://api.github.com/repos/project-rig/rig/assignees{/user}","branches_url":"https://api.github.com/repos/project-rig/rig/branches{/branch}","tags_url":"https://api.github.com/repos/project-rig/rig/tags","blobs_url":"https://api.github.com/repos/project-rig/rig/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/project-rig/rig/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/project-rig/rig/git/refs{/sha}","trees_url":"https://api.github.com/repos/project-rig/rig/git/trees{/sha}","statuses_url":"https://api.github.com/repos/project-rig/rig/statuses/{sha}","languages_url":"https://api.github.com/repos/project-rig/rig/languages","stargazers_url":"https://api.github.com/repos/project-rig/rig/stargazers","contributors_url":"https://api.github.com/repos/project-rig/rig/contributors","subscribers_url":"https://api.github.com/repos/project-rig/rig/subscribers","subscription_url":"https://api.github.com/repos/project-rig/rig/subscription","commits_url":"https://api.github.com/repos/project-rig/rig/commits{/sha}","git_commits_url":"https://api.github.com/repos/project-rig/rig/git/commits{/sha}","comments_url":"https://api.github.com/repos/project-rig/rig/comments{/number}","issue_comment_url":"https://api.github.com/repos/project-rig/rig/issues/comments{/number}","contents_url":"https://api.github.com/repos/project-rig/rig/contents/{+path}","compare_url":"https://api.github.com/repos/project-rig/rig/compare/{base}...{head}","merges_url":"https://api.github.com/repos/project-rig/rig/merges","archive_url":"https://api.github.com/repos/project-rig/rig/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/project-rig/rig/downloads","issues_url":"https://api.github.com/repos/project-rig/rig/issues{/number}","pulls_url":"https://api.github.com/repos/project-rig/rig/pulls{/number}","milestones_url":"https://api.github.com/repos/project-rig/rig/milestones{/number}","notifications_url":"https://api.github.com/repos/project-rig/rig/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/project-rig/rig/labels{/name}","releases_url":"https://api.github.com/repos/project-rig/rig/releases{/id}","deployments_url":"https://api.github.com/repos/project-rig/rig/deployments","created_at":"2015-01-19T21:20:40Z","updated_at":"2016-01-13T08:19:41Z","pushed_at":"2016-05-13T14:36:23Z","git_url":"git://github.com/project-rig/rig.git","ssh_url":"git@github.com:project-rig/rig.git","clone_url":"https://github.com/project-rig/rig.git","svn_url":"https://github.com/project-rig/rig","homepage":"","size":2216,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":12,"forks":1,"open_issues":12,"watchers":0,"default_branch":"master"}},"base":{"label":"project-rig:master","ref":"master","sha":"d130cc563542837ed4d31fdacda0fb23a50da587","user":{"login":"project-rig","id":11227885,"avatar_url":"https://avatars.githubusercontent.com/u/11227885?v=3","gravatar_id":"","url":"https://api.github.com/users/project-rig","html_url":"https://github.com/project-rig","followers_url":"https://api.github.com/users/project-rig/followers","following_url":"https://api.github.com/users/project-rig/following{/other_user}","gists_url":"https://api.github.com/users/project-rig/gists{/gist_id}","starred_url":"https://api.github.com/users/project-rig/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/project-rig/subscriptions","organizations_url":"https://api.github.com/users/project-rig/orgs","repos_url":"https://api.github.com/users/project-rig/repos","events_url":"https://api.github.com/users/project-rig/events{/privacy}","received_events_url":"https://api.github.com/users/project-rig/received_events","type":"Organization","site_admin":false},"repo":{"id":29497314,"name":"rig","full_name":"project-rig/rig","owner":{"login":"project-rig","id":11227885,"avatar_url":"https://avatars.githubusercontent.com/u/11227885?v=3","gravatar_id":"","url":"https://api.github.com/users/project-rig","html_url":"https://github.com/project-rig","followers_url":"https://api.github.com/users/project-rig/followers","following_url":"https://api.github.com/users/project-rig/following{/other_user}","gists_url":"https://api.github.com/users/project-rig/gists{/gist_id}","starred_url":"https://api.github.com/users/project-rig/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/project-rig/subscriptions","organizations_url":"https://api.github.com/users/project-rig/orgs","repos_url":"https://api.github.com/users/project-rig/repos","events_url":"https://api.github.com/users/project-rig/events{/privacy}","received_events_url":"https://api.github.com/users/project-rig/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/project-rig/rig","description":"A collection of tools for developing SpiNNaker applications.","fork":false,"url":"https://api.github.com/repos/project-rig/rig","forks_url":"https://api.github.com/repos/project-rig/rig/forks","keys_url":"https://api.github.com/repos/project-rig/rig/keys{/key_id}","collaborators_url":"https://api.github.com/repos/project-rig/rig/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/project-rig/rig/teams","hooks_url":"https://api.github.com/repos/project-rig/rig/hooks","issue_events_url":"https://api.github.com/repos/project-rig/rig/issues/events{/number}","events_url":"https://api.github.com/repos/project-rig/rig/events","assignees_url":"https://api.github.com/repos/project-rig/rig/assignees{/user}","branches_url":"https://api.github.com/repos/project-rig/rig/branches{/branch}","tags_url":"https://api.github.com/repos/project-rig/rig/tags","blobs_url":"https://api.github.com/repos/project-rig/rig/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/project-rig/rig/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/project-rig/rig/git/refs{/sha}","trees_url":"https://api.github.com/repos/project-rig/rig/git/trees{/sha}","statuses_url":"https://api.github.com/repos/project-rig/rig/statuses/{sha}","languages_url":"https://api.github.com/repos/project-rig/rig/languages","stargazers_url":"https://api.github.com/repos/project-rig/rig/stargazers","contributors_url":"https://api.github.com/repos/project-rig/rig/contributors","subscribers_url":"https://api.github.com/repos/project-rig/rig/subscribers","subscription_url":"https://api.github.com/repos/project-rig/rig/subscription","commits_url":"https://api.github.com/repos/project-rig/rig/commits{/sha}","git_commits_url":"https://api.github.com/repos/project-rig/rig/git/commits{/sha}","comments_url":"https://api.github.com/repos/project-rig/rig/comments{/number}","issue_comment_url":"https://api.github.com/repos/project-rig/rig/issues/comments{/number}","contents_url":"https://api.github.com/repos/project-rig/rig/contents/{+path}","compare_url":"https://api.github.com/repos/project-rig/rig/compare/{base}...{head}","merges_url":"https://api.github.com/repos/project-rig/rig/merges","archive_url":"https://api.github.com/repos/project-rig/rig/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/project-rig/rig/downloads","issues_url":"https://api.github.com/repos/project-rig/rig/issues{/number}","pulls_url":"https://api.github.com/repos/project-rig/rig/pulls{/number}","milestones_url":"https://api.github.com/repos/project-rig/rig/milestones{/number}","notifications_url":"https://api.github.com/repos/project-rig/rig/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/project-rig/rig/labels{/name}","releases_url":"https://api.github.com/repos/project-rig/rig/releases{/id}","deployments_url":"https://api.github.com/repos/project-rig/rig/deployments","created_at":"2015-01-19T21:20:40Z","updated_at":"2016-01-13T08:19:41Z","pushed_at":"2016-05-13T14:36:23Z","git_url":"git://github.com/project-rig/rig.git","ssh_url":"git@github.com:project-rig/rig.git","clone_url":"https://github.com/project-rig/rig.git","svn_url":"https://github.com/project-rig/rig","homepage":"","size":2216,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":12,"forks":1,"open_issues":12,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/project-rig/rig/pulls/245"},"html":{"href":"https://github.com/project-rig/rig/pull/245"},"issue":{"href":"https://api.github.com/repos/project-rig/rig/issues/245"},"comments":{"href":"https://api.github.com/repos/project-rig/rig/issues/245/comments"},"review_comments":{"href":"https://api.github.com/repos/project-rig/rig/pulls/245/comments"},"review_comment":{"href":"https://api.github.com/repos/project-rig/rig/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/project-rig/rig/pulls/245/commits"},"statuses":{"href":"https://api.github.com/repos/project-rig/rig/statuses/71b4c76dca40f49512eb5dc15f06c72973eee170"}}}}