rational.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * rational numbers
  3. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/rational.c"
  22. #include "libavutil/integer.h"
  23. int main(void)
  24. {
  25. AVRational a,b,r;
  26. int i,j,k;
  27. static const int64_t numlist[] = {
  28. INT64_MIN, INT64_MIN+1, INT64_MAX, INT32_MIN, INT32_MAX, 1,0,-1,
  29. 123456789, INT32_MAX-1, INT32_MAX+1LL, UINT32_MAX-1, UINT32_MAX, UINT32_MAX+1LL
  30. };
  31. for (a.num = -2; a.num <= 2; a.num++) {
  32. for (a.den = -2; a.den <= 2; a.den++) {
  33. for (b.num = -2; b.num <= 2; b.num++) {
  34. for (b.den = -2; b.den <= 2; b.den++) {
  35. int c = av_cmp_q(a,b);
  36. double d = av_q2d(a) == av_q2d(b) ?
  37. 0 : (av_q2d(a) - av_q2d(b));
  38. if (d > 0) d = 1;
  39. else if (d < 0) d = -1;
  40. else if (d != d) d = INT_MIN;
  41. if (c != d)
  42. av_log(NULL, AV_LOG_ERROR, "%d/%d %d/%d, %d %f\n", a.num,
  43. a.den, b.num, b.den, c,d);
  44. r = av_sub_q(av_add_q(b,a), b);
  45. if(b.den && (r.num*a.den != a.num*r.den || !r.num != !a.num || !r.den != !a.den))
  46. av_log(NULL, AV_LOG_ERROR, "%d/%d ", r.num, r.den);
  47. }
  48. }
  49. }
  50. }
  51. for (i = 0; i < FF_ARRAY_ELEMS(numlist); i++) {
  52. int64_t a = numlist[i];
  53. for (j = 0; j < FF_ARRAY_ELEMS(numlist); j++) {
  54. int64_t b = numlist[j];
  55. if (b<=0)
  56. continue;
  57. for (k = 0; k < FF_ARRAY_ELEMS(numlist); k++) {
  58. int64_t c = numlist[k];
  59. int64_t res;
  60. AVInteger ai;
  61. if (c<=0)
  62. continue;
  63. res = av_rescale_rnd(a,b,c, AV_ROUND_ZERO);
  64. ai = av_mul_i(av_int2i(a), av_int2i(b));
  65. ai = av_div_i(ai, av_int2i(c));
  66. if (av_cmp_i(ai, av_int2i(INT64_MAX)) > 0 && res == INT64_MIN)
  67. continue;
  68. if (av_cmp_i(ai, av_int2i(INT64_MIN)) < 0 && res == INT64_MIN)
  69. continue;
  70. if (av_cmp_i(ai, av_int2i(res)) == 0)
  71. continue;
  72. // Special exception for INT64_MIN, remove this in case INT64_MIN is handled without off by 1 error
  73. if (av_cmp_i(ai, av_int2i(res-1)) == 0 && a == INT64_MIN)
  74. continue;
  75. av_log(NULL, AV_LOG_ERROR, "%"PRId64" * %"PRId64" / %"PRId64" = %"PRId64" or %"PRId64"\n", a,b,c, res, av_i2int(ai));
  76. }
  77. }
  78. }
  79. for (a.num = 1; a.num <= 10; a.num++) {
  80. for (a.den = 1; a.den <= 10; a.den++) {
  81. if (av_gcd(a.num, a.den) > 1)
  82. continue;
  83. for (b.num = 1; b.num <= 10; b.num++) {
  84. for (b.den = 1; b.den <= 10; b.den++) {
  85. int start;
  86. if (av_gcd(b.num, b.den) > 1)
  87. continue;
  88. if (av_cmp_q(b, a) < 0)
  89. continue;
  90. for (start = 0; start < 10 ; start++) {
  91. int acc= start;
  92. int i;
  93. for (i = 0; i<100; i++) {
  94. int exact = start + av_rescale_q(i+1, b, a);
  95. acc = av_add_stable(a, acc, b, 1);
  96. if (FFABS(acc - exact) > 2) {
  97. av_log(NULL, AV_LOG_ERROR, "%d/%d %d/%d, %d %d\n", a.num,
  98. a.den, b.num, b.den, acc, exact);
  99. return 1;
  100. }
  101. }
  102. }
  103. }
  104. }
  105. }
  106. }
  107. for (a.den = 1; a.den < 0x100000000U/3; a.den*=3) {
  108. for (a.num = -1; a.num < (1<<27); a.num += 1 + a.num/100) {
  109. float f = av_int2float(av_q2intfloat(a));
  110. float f2 = av_q2d(a);
  111. if (fabs(f - f2) > fabs(f)/5000000) {
  112. av_log(NULL, AV_LOG_ERROR, "%d/%d %f %f\n", a.num,
  113. a.den, f, f2);
  114. return 1;
  115. }
  116. }
  117. }
  118. return 0;
  119. }