You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
2.9KB

  1. /*
  2. * Rational numbers
  3. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. /**
  21. * @file rational.c
  22. * Rational numbers
  23. * @author Michael Niedermayer <michaelni@gmx.at>
  24. */
  25. //#include <math.h>
  26. #include <limits.h>
  27. #include "common.h"
  28. #include "mathematics.h"
  29. #include "rational.h"
  30. int av_reduce(int *dst_nom, int *dst_den, int64_t nom, int64_t den, int64_t max){
  31. AVRational a0={0,1}, a1={1,0};
  32. int sign= (nom<0) ^ (den<0);
  33. int64_t gcd= ff_gcd(ABS(nom), ABS(den));
  34. nom = ABS(nom)/gcd;
  35. den = ABS(den)/gcd;
  36. if(nom<=max && den<=max){
  37. a1= (AVRational){nom, den};
  38. den=0;
  39. }
  40. while(den){
  41. int64_t x = nom / den;
  42. int64_t next_den= nom - den*x;
  43. int64_t a2n= x*a1.num + a0.num;
  44. int64_t a2d= x*a1.den + a0.den;
  45. if(a2n > max || a2d > max) break;
  46. a0= a1;
  47. a1= (AVRational){a2n, a2d};
  48. nom= den;
  49. den= next_den;
  50. }
  51. assert(ff_gcd(a1.num, a1.den) == 1);
  52. *dst_nom = sign ? -a1.num : a1.num;
  53. *dst_den = a1.den;
  54. return den==0;
  55. }
  56. /**
  57. * returns b*c.
  58. */
  59. AVRational av_mul_q(AVRational b, AVRational c){
  60. av_reduce(&b.num, &b.den, b.num * (int64_t)c.num, b.den * (int64_t)c.den, INT_MAX);
  61. return b;
  62. }
  63. /**
  64. * returns b/c.
  65. */
  66. AVRational av_div_q(AVRational b, AVRational c){
  67. av_reduce(&b.num, &b.den, b.num * (int64_t)c.den, b.den * (int64_t)c.num, INT_MAX);
  68. return b;
  69. }
  70. /**
  71. * returns b+c.
  72. */
  73. AVRational av_add_q(AVRational b, AVRational c){
  74. av_reduce(&b.num, &b.den, b.num * (int64_t)c.den + c.num * (int64_t)b.den, b.den * (int64_t)c.den, INT_MAX);
  75. return b;
  76. }
  77. /**
  78. * returns b-c.
  79. */
  80. AVRational av_sub_q(AVRational b, AVRational c){
  81. av_reduce(&b.num, &b.den, b.num * (int64_t)c.den - c.num * (int64_t)b.den, b.den * (int64_t)c.den, INT_MAX);
  82. return b;
  83. }
  84. /**
  85. * Converts a double precission floating point number to a AVRational.
  86. * @param max the maximum allowed numerator and denominator
  87. */
  88. AVRational av_d2q(double d, int max){
  89. AVRational a;
  90. int exponent= FFMAX( (int)(log(ABS(d) + 1e-20)/log(2)), 0);
  91. int64_t den= 1LL << (61 - exponent);
  92. av_reduce(&a.num, &a.den, (int64_t)(d * den + 0.5), den, max);
  93. return a;
  94. }