Exemple #1
0
int main(int argc, char *argv[]) {
  frac_init();

  Frac* frac = frac_make(2, 6);
  frac_println(stdout, frac);

  Frac* frac2 = frac_multiply(frac_alloc(), frac, frac);
  frac_println(stdout, frac2);

  Frac* frac3 = frac_add(frac_alloc(), frac2, frac);
  frac_println(stdout, frac3);

  Frac* frac4 = frac_divide(frac_alloc(), frac3, frac);
  frac_println(stdout, frac4);

  frac_free(frac4);
  frac_free(frac3);
  frac_free(frac2);
  frac_free(frac);

  // compute a summation approximation of e^x
  int x = 2;
  Frac* e = frac_make(0, 1);
  for(int k = 0; k < 17; k++) {
    Frac* term = frac_make(ipow(x, k), factorial(k));
    frac_add(e, e, term);
    frac_free(term);
  }
  frac_print(stdout, e);
  printf(" ~= %lf\n", frac_approx(e));


  // allocate and free a "large" number of fractions but never with
  // many alive at once. A pool of only 2 fractions should be
  // sufficient to satisfy this test.
  for(int ii = 0; ii < 10000; ++ii) {
    Frac* aFrac = frac_make(1,2);
    frac_free(aFrac);
  }

  // then make sure nothing obviously broke
  frac_println(stdout, e);
  frac_free(e);

  return 0;
}
Exemple #2
0
void matrix_add_row(MATRIX *m, int row, int add_row) {
	int i ;
	for(i = 0; i < m->cols ; i++) {
		FRAC *t = matrix_get_val(m, row, i) ;
		FRAC *a = matrix_get_val(m, add_row, i) ;
		FRAC f ;
		frac_add(&f, t, a) ;
		frac_copy(t, &f) ;
	}
}
void testFractions(void)
{
	FRACTION twoThirds;
	printf("Creating two thirds...");
	twoThirds = frac_with(2, 3);
	frac_show(twoThirds); printf("\n");

	FRACTION threeHalves;
	printf("Its reciprocal is...");
	threeHalves = frac_reciprocal(twoThirds);
	frac_show(threeHalves); printf("\n");

	FRACTION thirteenSixths;
	printf("Adding 2/3 + 3/2...");
	frac_show(frac_add(twoThirds, threeHalves)); printf("\n");

	printf("The first numer is unmodified: ");
	frac_show(twoThirds); printf("\n");
	

}
Exemple #4
0
static void test_fractions(CuTest *tc) {
    variant a, b;
    a = frac_make(120, 12000);
    CuAssertIntEquals(tc, 1, a.sa[0]);
    CuAssertIntEquals(tc, 100, a.sa[1]);
    b = frac_make(23, 2300);
    a = frac_add(a, b);
    CuAssertIntEquals(tc, 1, a.sa[0]);
    CuAssertIntEquals(tc, 50, a.sa[1]);
    a = frac_mul(a, b);
    CuAssertIntEquals(tc, 1, a.sa[0]);
    CuAssertIntEquals(tc, 5000, a.sa[1]);
    a = frac_div(b, b);
    CuAssertIntEquals(tc, 1, a.sa[0]);
    CuAssertIntEquals(tc, 1, a.sa[1]);
    a = frac_sub(a, a);
    CuAssertIntEquals(tc, 0, a.sa[0]);
    a = frac_sub(frac_one, a);
    CuAssertIntEquals(tc, 1, a.sa[0]);
    CuAssertIntEquals(tc, 1, a.sa[1]);
    a = frac_mul(a, frac_zero);
    CuAssertIntEquals(tc, 0, a.sa[0]);
    CuAssertIntEquals(tc, 1, frac_sign(frac_make(-1, -1)));
    CuAssertIntEquals(tc, 1, frac_sign(frac_make(1, 1)));
    CuAssertIntEquals(tc, -1, frac_sign(frac_make(-1, 1)));
    CuAssertIntEquals(tc, -1, frac_sign(frac_make(1, -1)));
    CuAssertIntEquals(tc, 0, frac_sign(frac_make(0, 1)));

    /* we reduce large integers by calculating the gcd */
    a = frac_make(480000, 3000);
    CuAssertIntEquals(tc, 160, a.sa[0]);
    CuAssertIntEquals(tc, 1, a.sa[1]);

    /* if num is too big for a short, and the gcd is 1, we cheat: */
    a = frac_make(480001, 3000);
    CuAssertIntEquals(tc, 32000, a.sa[0]);
    CuAssertIntEquals(tc, 200, a.sa[1]);
}
FRACTION frac_subtract(FRACTION lhs, FRACTION rhs) 
{
	return frac_add(lhs, frac_negate(rhs));
}
Exemple #6
0
int MP4Parser::locate_sample(Track *trak, ReadStatus *rstatus,
                             SampleEntry *sentry)
{
  if (rstatus->sample_idx == trak->stsz->sample_count) {
    // No more frames to read
    return -1;
  }

  if (rstatus->stts.offset ==
      trak->stts->sample_count[rstatus->stts.cnt]) {
    // Handle next sample_count in "stts"
    ++rstatus->stts.cnt;
    rstatus->stts.offset = 0;
  }

  if (trak->ctts && rstatus->ctts.offset ==
                    trak->ctts->sample_count[rstatus->ctts.cnt]) {
    // Handle next sample_count in "ctts"
    ++rstatus->ctts.cnt;
    rstatus->ctts.offset = 0;
  }

  // Get chunk containing this sample and chunk's first sample idx
  uint32_t first_sample_in_chunk;
  uint32_t chunk = chunk_containing_sample(
      rstatus->sample_idx, trak->stsc, first_sample_in_chunk,
      &rstatus->lcc);
#ifdef XDEBUG
  LOGI("sample_idx: %u, chunk: %u, first_sample_in_chunk: %u",
       rstatus->sample_idx, chunk, first_sample_in_chunk);
#endif

  // Get sample's offset in file
  if (trak->large_offset)
    rstatus->sample_offset = trak->co64->chunk_offset[chunk];
  else
    rstatus->sample_offset = trak->stco->chunk_offset[chunk];
  if (first_sample_in_chunk != rstatus->sample_idx) {
    for (uint32_t i = first_sample_in_chunk;
         i < rstatus->sample_idx;
         ++i) {
      rstatus->sample_offset += trak->stsz->entry_size[i];
    }
  }
  // Get sample's size
  uint32_t sample_sz =
    trak->stsz->entry_size[rstatus->sample_idx];
#ifdef XDEBUG
  LOGI("sample_idx: %u, FILE offset: 0x%08x, size: 0x%08x",
       rstatus->sample_idx,
       rstatus->sample_offset,
       sample_sz);
#endif

  if (sentry) {
    sentry->decode_ts = rstatus->dts.val;
    sentry->sample_idx = rstatus->sample_idx;
    sentry->sample_offset = rstatus->sample_offset;
    sentry->sample_sz = sample_sz;
  }

  // Update the next frame's decode timestamp
  frac_add(&rstatus->dts,
           trak->stts->sample_delta[rstatus->stts.cnt]*1000);
  if (trak->ctts && sentry) {
    sentry->composition_time =
      trak->ctts->sample_offset[rstatus->ctts.cnt]*1000/trak->timescale;
  }
  // Handle next sample in this sample_count in "stts"
  ++rstatus->stts.offset;
  if (trak->ctts) {
    // Handle next sample in this sample_count in "ctts"
    ++rstatus->ctts.offset;
  }
  // Update the sample idx to read next time
  ++rstatus->sample_idx;
  return 0;
}