# [FFmpeg-devel] [PATCH 10/10] Document how av_cmp_q() deal when one of the values to be compared is 0/0.

Michael Niedermayer michaelni
Tue Oct 5 03:48:32 CEST 2010

```On Mon, Oct 04, 2010 at 11:24:25AM +0200, Stefano Sabatini wrote:
> On date Monday 2010-10-04 01:54:21 +0200, Michael Niedermayer encoded:
> > On Mon, Oct 04, 2010 at 12:14:34AM +0200, Stefano Sabatini wrote:
> [...]
> > > @@ -43,10 +44,13 @@ typedef struct AVRational{
> > >   * Compare two rationals.
> > >   * @param a first rational
> > >   * @param b second rational
> > > - * @return 0 if a==b, 1 if a>b and -1 if a<b
> > > + * @return 0 if a==b, 1 if a>b, -1 if a<b, and INT_MIN if one of the
> > > + * values is of the form 0/0
> > >   */
> > >  static inline int av_cmp_q(AVRational a, AVRational b){
> > >      const int64_t tmp= a.num * (int64_t)b.den - b.num * (int64_t)a.den;
> > > +    if (a.num == 0 && a.den == 0 || b.num == 0 && b.den == 0)
> > > +        return INT_MIN;
> > >
> > >      if(tmp) return ((tmp ^ a.den ^ b.den)>>63)|1;
> > >      else    return 0;
> >
> > this doesnt work with infinites
> > i would suggest:
> >
> > Index: rational.h
> > ===================================================================
> > --- rational.h  (revision 25329)
> > +++ rational.h  (working copy)
> > @@ -49,7 +49,9 @@
> >      const int64_t tmp= a.num * (int64_t)b.den - b.num * (int64_t)a.den;
> >
> >      if(tmp) return ((tmp ^ a.den ^ b.den)>>63)|1;
> > -    else    return 0;
> > +    else if(b.den && a.den) return 0;
> > +    else if(a.num && b.num) return (a.num>>31) - (b.num>>31);
> > +    else                    return INT_MIN;
> >  }
> >
> >  /**
> >
> > if you agree ill commit that and leave the dox to you
>
> OK, and patch updated with a test. Result is:
>
> Testing av_cmp_q()
> 1/0 cmp 1/1 -> 1
> -1/0 cmp 1/1 -> -1
> 1/0 cmp -1/1 -> 1
> 1/0 cmp 1/0 -> 0
> 2/0 cmp 1/0 -> 0
> 1/0 cmp -1/0 -> 1
> -1/0 cmp 1/0 -> -1
> -1/0 cmp -1/0 -> 0
> 0/0 cmp 1/1 -> -2147483648
> 0/0 cmp -1/1 -> -2147483648
> 0/0 cmp 1/0 -> -2147483648
> 1/0 cmp 0/0 -> -2147483648
> 0/0 cmp -1/0 -> -2147483648
> 0/0 cmp 0/0 -> -2147483648
>
> Tell if you want to keep the test or apply the test as a separate
> patch or apply the patch yourself (but in that case remember to bump
> lavu micro version).

applied mine

[...]
--

Rewriting code that is poorly written but fully understood is good.
Rewriting code that one doesnt understand is a sign that one is less smart
then the original author, trying to rewrite it will not make it better.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/attachments/20101005/ae83afbd/attachment.pgp>

```