> ruhatadiyaman wrote:
> > thanks for all replies. i tried them but still the
> > problem is continuing. this is my code with 'unsigned
> > long int';
> > /*factorial*/
<snip>
> > so after 13 the results are wrong :(
> > otherwise i confused with usage of long double.

  % type fact.c
  #include <stdio.h>
  #include <stdlib.h>

  void out_of_memory(void)
  {
    fprintf(stderr, "out of memory!\n");
    exit(EXIT_FAILURE);
  }

  int main(int argc, char **argv)
  {
    unsigned *a;
    size_t i, len, cap;
    unsigned long c, n, max_n;

    if (argc != 2) exit(0);
    max_n = strtoul(argv[1], 0, 10);
    if (max_n < 2 || max_n > 9999) exit(0);

    cap = 1;
    a = malloc(cap * sizeof *a);
    if (!a) out_of_memory();
    len = 1;
    a[0] = 1;

    for (n = 2; n <= max_n; n++)
    {
      for (c = 0, i = 0; i < len; i++)
      {
        c = a[i] * n + c;
        a[i] = c % 10000;
        c /= 10000;
      }

      while (c)
      {
        if (len == cap)
        {
          if (cap > ((size_t)-1)/2) out_of_memory();
          cap *= 2;
          a = realloc(a, cap * sizeof *a);
          if (!a) out_of_memory();
        }
        a[len++] = c % 10000;
        c /= 10000;
      }
    }

    printf("%4u", a[len - 1]);
    for (c = 1, i = len - 1; i-- > 0; )
    {
      printf("%04u", a[i]);
      if (++c == 16) { putchar('\n'); c = 0; }
    }
    if (c) putchar('\n');

    free(a);
    return 0;
  }
  
  % acc fact.c -o fact.exe
  
  % fact 1000
  4023872600770937735437024339230039857193748642107146325437999104
  2993851239862902059204420848696940480047998861019719605863166687
  2994808558901323829669944590997424504087073759918823627727188732
  ...
  00000000

> > i wrote this simple code which is below;
> > #include<stdio.h>
> > int main()
> > {
> >     
> > long double x, y;
> > scanf("%Lf", &x);
> > scanf("%Lf", &y);

Try checking the return value of scanf.

> > printf("x=%Lf y=%Lf\n", x, y);
> > printf("result=%Lf", x*y);
> > getch();
> > }
> > 
> > when i run it,for example, i give 2 for x and 4 for y,the
> > program gives this;
> > x=2.000000  y=0.000000
> > result=-2.000000
> > 
> > what's this?

I don't know.

  % type ld.c
  #include<stdio.h>
  
  int main(void)
  {
    long double x, y;
    if (scanf("%Lf%Lf", &x, &y) == 2)
    {
      printf("x=%Lf y=%Lf\n", x, y);
      printf("result=%Lf", x*y);
    }
    return 0;
  }
  
  % acc ld.c -o ld.exe
  
  % ld
  2 4
  x=2.000000 y=4.000000
  result=8.000000

Thomas Hruska <thru...@...> wrote:
> You could also use unsigned long long int and %llu (minimum
> of 64-bits, which will be more accurate than 'float' or
> 'double'). The following is  worth keeping around as well
> for future reference:
> 
> <http://cubicspot.blogspot.com/2009/03/unsigned-long-long-long-long-long-int.html>

  "Thankfully 'float' and 'double' use the IEEE floating point
  standard."

That is not a requirement of either C or C++.

  "The lack of being able to declare how many bits/bytes we
  need for integer representation is silly and only going
  to get sillier as int sizes get larger." etc...

Have you heard of <stdint.h>?

-- 
Peter

Reply via email to