Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re^2: 64-bit division anomolies (Solved.)

by Discipulus (Canon)
on Feb 17, 2015 at 09:42 UTC ( #1116955=note: print w/replies, xml ) Need Help??

in reply to Re: 64-bit division anomolies (Solved.)
in thread 64-bit division anomolies (Solved.)

oh.. well.. If you have time, can i ask you to comment a bit more your code?
just to make it more appreciable by people with low mathfu as me.. ;=)?

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^3: 64-bit division anomolies (Solved.)
by salva (Canon) on Feb 17, 2015 at 10:07 UTC
    My method, first builds an approximation of the solution of a/b as...
    s1 = ((a / 2) / b) * 2;
    a / 2 is done using the bug-free shift-right operator (>>) and results always in a number with bit 64 unset, so we can divide it by b without incurring in the convert-to-NV bug. Again multiplying by 2 is done with the shift-left operator (<<).

    Then, we use the approximation to build the exact solution as:

    s = s1 + (a - s1 * b) / b

    Well, actually, in order to do everything using bug-free operations, the code uses the following equivalence:

    s2 = (a / 2) / b s1 = s2 * 2 s = s1 + (a - s1 * b) / b s = s2 * 2 + (a - (s2 * b) * 2) / b
    where $d is s2, and $e is (a - (s2 * b) * 2).

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1116955]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (1)
As of 2023-06-04 21:32 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (22 votes). Check out past polls.