Re: comparing products of integers



In article <1114606012.207570.165560@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>
analyst41@xxxxxxxxxxx writes:
>I want to compare A.B with C.D where A,B,C,D are 32 bit integers. Is
>there a way to do the comparison without creating the potentially 64
>bit products ?
>
>Thanks for any help.
>
A (slow) method manipulates 2 x 2 determinants.
Start with

M = (A C) sign = +1
(D B)

Then A*B - C*D = sign*det(M).

If A < 0 or D < 0, negate the corresponding row and
replace sign by -sign. Swap rows and change the sign,
if necessary, to make A >= D >= 0.

If D = 0, then det(M) has the same sign as A*B.
Otherwise A and D are positive.

If C < 0, negate the second column and the sign.

If B < 0, then A*B < 0 and C*D >= 0, so you know the sign of det(M).

If B > C, then A*B > C*D.

Otherwise let q = MIN(FLOOR(A/D), FLOOR(C/B)) (an integer).
Subtract q*(second row) from (first row).


You can instead write A, B, C, D each in the form
a1*2^16 + a0. If your variables are unsigned, then
a1, a0 are nonnegative. Manipulate

(a1*2^16 + a0) * (b1*2^16 + b0)
- (c1*2^16 + c0) * (d1*2^16 + d0)

algebraically. If you have 53-bit floating point
mantissas, you can cast a1, a0, ... to floating point,
then evaluate the coefficients of

(a1*b1 - c1*d1)*2^32 + ...

and soon get the determinant accurately.

--
If same-sex marriages are so bad, why do most
college dormitories assign same-sex roommates?

pmontgom@xxxxxx Microsoft Research and CWI Home: Bellevue, WA
.



Relevant Pages

  • Re: comparing products of integers
    ... Peter L. Montgomery wrote: ... >>I want to compare A.B with C.D where A,B,C,D are 32 bit integers. ... > If same-sex marriages are so bad, ...
    (sci.math.num-analysis)
  • Re: [PHP] fgetcsv
    ... You probably don't have the fgetcsv inside the while loop to get past ... So are you trying to compare the first column or the first row? ...
    (php.general)
  • Re: [PHP] fgetcsv
    ... I'm trying to compare a value to the first field in a csv fILE (example ... So are you trying to compare the first column or the first row? ... "Some men are born to greatness, some achieve greatness, ...
    (php.general)
  • Re: How to set up a sub-total routine?
    ... off the cuff. ... > My first thought is to build a string consisting of all the text ... > values in the first row, drop a row and build a similar string, and ... > compare. ...
    (microsoft.public.excel.programming)
  • Re: Why CSST?
    ... COMPARE AND SWAP AND STORE may be used in conjunction with other ... COMPARE AND SWAP AND STORE should not be used to manipulate ... For IBM-MAIN subscribe / signoff / archive access instructions, ...
    (bit.listserv.ibm-main)