(42, all flavours) Integer Division - how?
|
12-18-2020, 12:08 AM
(This post was last modified: 12-19-2020 12:56 AM by Albert Chan.)
Post: #39
|
|||
|
|||
RE: (42, all flavours) Integer Division - how?
remainder() based idiv is very elegant.
PHP Code: function idiv6(a, b) -- assumed b > 0 Above is similar to this (6a version possibly faster, only tried to get remainder sign) PHP Code: function idiv6a(a, b) -- assumed b > 0 All the code for mod/half-way test is the last if line. R < h: if residual is less than half of b, q had rounded-down. For floor divide, no need for correction. r == h: if residual is half of b, q (because of round-to-even) is even. But, r also guaranteed even q -> no need for correction. Update: slightly faster version 6b, removed R, and r shifted, with range = [-2h, 2h) PHP Code: function idiv6b(a, b) -- assumed b > 0 |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 12 Guest(s)