Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #19418

Re: template metaprogramming in C?

Message-ID <4F7F7A2A.3303@mindspring.com> (permalink)
Date 2012-04-06 19:20 -0400
From pete <pfiland@mindspring.com>
Organization PF
Newsgroups comp.lang.c
Subject Re: template metaprogramming in C?
References (2 earlier) <fd4f60c4-a447-47fe-94a6-b003918d88c2@fk28g2000vbb.googlegroups.com> <4f7d7070$0$1387$4fafbaef@reader2.news.tin.it> <4f7d7c00$0$1374$4fafbaef@reader1.news.tin.it> <4f7d82b9$0$1381$4fafbaef@reader2.news.tin.it> <4F7F7185.2958@mindspring.com>

Show all headers | View raw


pete wrote:
> 
> io_x wrote:
> 
> > u32  rsqrt(double*  r, double  p, u32  ncs)
> 
> > what about result? the last number of each line is the sys_sqrt(p)
> 
> If you want to compare the result of rsqrt(&r, p, ncs) to sqrt(p),
> 
> Then you should compare
>     (p / rsqrt(&r, p, ncs) - rsqrt(&r, p, ncs))
> against
>     (p / sqrt(p) - sqrt(p))
> 
> The expression with the smaller magnitude,
> contains the more accurate square root function.

/* BEGIN new.c output*/

square_root(0.000000) == 0.000000
       sqrt(0.000000) == 0.000000

square_root(1.000000) == 1.000000
       sqrt(1.000000) == 1.000000
x / square_root(x) - square_root(x) == 0
x /        sqrt(x) -        sqrt(x) == 0

square_root(1.001000) == 1.000500
       sqrt(1.001000) == 1.000500
x / square_root(x) - square_root(x) == 0
x /        sqrt(x) -        sqrt(x) == 0

square_root(2.000000) == 1.414214
       sqrt(2.000000) == 1.414214
x / square_root(x) - square_root(x) == 2.22045e-016
x /        sqrt(x) -        sqrt(x) == -2.22045e-016

square_root(10.000000) == 3.162278
       sqrt(10.000000) == 3.162278
x / square_root(x) - square_root(x) == 4.44089e-016
x /        sqrt(x) -        sqrt(x) == -4.44089e-016

square_root(100.000000) == 10.000000
       sqrt(100.000000) == 10.000000
x / square_root(x) - square_root(x) == 0
x /        sqrt(x) -        sqrt(x) == 0

square_root(1000.000000) == 31.622777
       sqrt(1000.000000) == 31.622777
x / square_root(x) - square_root(x) == 0
x /        sqrt(x) -        sqrt(x) == 0

square_root(10000.000000) == 100.000000
       sqrt(10000.000000) == 100.000000
x / square_root(x) - square_root(x) == 0
x /        sqrt(x) -        sqrt(x) == 0

square_root(0.100000) == 0.316228
       sqrt(0.100000) == 0.316228
x / square_root(x) - square_root(x) == 0
x /        sqrt(x) -        sqrt(x) == 0

square_root(0.250000) == 0.500000
       sqrt(0.250000) == 0.500000
x / square_root(x) - square_root(x) == 0
x /        sqrt(x) -        sqrt(x) == 0

square_root(0.001000) == 0.031623
       sqrt(0.001000) == 0.031623
x / square_root(x) - square_root(x) == 6.93889e-018
x /        sqrt(x) -        sqrt(x) == 6.93889e-018

square_root(0.000100) == 0.010000
       sqrt(0.000100) == 0.010000
x / square_root(x) - square_root(x) == 0
x /        sqrt(x) -        sqrt(x) == 0

square_root(0.000010) == 0.003162
       sqrt(0.000010) == 0.003162
x / square_root(x) - square_root(x) == 0
x /        sqrt(x) -        sqrt(x) == 0

/* END new.c output*/




/* BEGIN new.c */

#include <stdio.h>
#include <math.h>

double square_root(double x)
{
    double root = x;

    if (root > 0) {
        double next = root / 2 + 0.5;

        do {
            root = next;
            next = (x / root + root) / 2;
        } while (root > next);
    }
    return root;
}

int 
main(void)
{
    double input[] = {
        0, 1, 1.001, 2, 10, 100, 1000, 10000, 
        0.1, 0.25, 0.001, 0.0001, 0.00001
    };
    size_t x;

    puts("/* BEGIN new.c output*/\n");
    for (x = 0; x != sizeof input / sizeof *input; ++x) {
        printf("square_root(%f) == %f\n", input[x], 
            square_root(input[x]));
        printf("       sqrt(%f) == %f\n", input[x], 
            sqrt(input[x]));
        if (input[x] > 0) {
        printf("x / square_root(x) - square_root(x) == %g\n",
            input[x] / square_root(input[x])
            - square_root(input[x]));
        printf("x /        sqrt(x) -        sqrt(x) == %g\n\n",
            input[x] / sqrt(input[x]) - sqrt(input[x]));
        } else {
            putchar('\n');
        }
    }
    puts("/* END new.c output*/");
    return 0;
}

/* END new.c */

-- 
pete

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

template metaprogramming in C? Rui Maciel <rui.maciel@gmail.com> - 2012-03-24 11:57 +0000
  Re: template metaprogramming in C? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-03-24 07:25 -0700
    Re: template metaprogramming in C? Markus Wichmann <nullplan@gmx.net> - 2012-03-26 11:46 +0200
      Re: template metaprogramming in C? Philipp Klaus Krause <pkk@spth.de> - 2012-03-27 21:09 +0200
        Re: template metaprogramming in C? Markus Wichmann <nullplan@gmx.net> - 2012-03-28 09:25 +0200
          Re: template metaprogramming in C? James Kuyper <jameskuyper@verizon.net> - 2012-03-28 06:59 -0400
      Re: template metaprogramming in C? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-03-28 12:13 -0700
      Re: template metaprogramming in C? James Dow Allen <jdallen2000@yahoo.com> - 2012-04-01 02:47 -0700
  Re: template metaprogramming in C? Jens Gustedt <jens.gustedt@loria.fr> - 2012-03-24 18:01 +0100
  Re: template metaprogramming in C? Nobody <nobody@nowhere.com> - 2012-03-24 22:57 +0000
    Re: template metaprogramming in C? William Ahern <william@wilbur.25thandClement.com> - 2012-03-24 18:29 -0700
      Re: template metaprogramming in C? Ian Collins <ian-news@hotmail.com> - 2012-03-25 15:03 +1300
      Re: template metaprogramming in C? nick_keighley_nospam@hotmail.com - 2012-03-25 05:32 -0700
  Re: template metaprogramming in C? BGB <cr88192@hotmail.com> - 2012-03-24 17:02 -0700
  Re: template metaprogramming in C? jacob navia <jacob@spamsink.net> - 2012-03-25 01:21 +0100
    Re: template metaprogramming in C? Philipp Klaus Krause <pkk@spth.de> - 2012-03-27 21:17 +0200
      Re: template metaprogramming in C? jacob navia <jacob@spamsink.net> - 2012-03-28 11:10 +0200
        Re: template metaprogramming in C? Nils M Holm <news2009@t3x.org> - 2012-03-28 10:05 +0000
          Re: template metaprogramming in C? Willem <willem@toad.stack.nl> - 2012-03-28 13:36 +0000
            Re: template metaprogramming in C? Nils M Holm <news2009@t3x.org> - 2012-03-28 14:40 +0000
        Re: template metaprogramming in C? Kaz Kylheku <kaz@kylheku.com> - 2012-03-28 18:32 +0000
          Re: template metaprogramming in C? jacob navia <jacob@spamsink.net> - 2012-03-28 22:27 +0200
            Re: template metaprogramming in C? Kaz Kylheku <kaz@kylheku.com> - 2012-03-28 21:02 +0000
        Re: template metaprogramming in C? Nobody <nobody@nowhere.com> - 2012-03-29 16:30 +0100
          Re: template metaprogramming in C? jacob navia <jacob@spamsink.net> - 2012-03-29 23:39 +0200
            Re: template metaprogramming in C? "BartC" <bc@freeuk.com> - 2012-03-29 23:36 +0100
              Re: template metaprogramming in C? Kaz Kylheku <kaz@kylheku.com> - 2012-03-30 00:55 +0000
            Re: template metaprogramming in C? Kaz Kylheku <kaz@kylheku.com> - 2012-03-30 00:32 +0000
              Re: template metaprogramming in C? "BartC" <bc@freeuk.com> - 2012-03-30 10:52 +0100
                Re: template metaprogramming in C? jacob navia <jacob@spamsink.net> - 2012-04-01 09:36 +0200
            Re: template metaprogramming in C? Ian Collins <ian-news@hotmail.com> - 2012-03-30 16:32 +1300
              Re: template metaprogramming in C? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-03-31 01:52 -0700
    Re: template metaprogramming in C? ImpalerCore <jadill33@gmail.com> - 2012-03-28 08:48 -0700
      Re: template metaprogramming in C? "io_x" <a@b.c.invalid> - 2012-04-05 12:18 +0200
        Re: template metaprogramming in C? "io_x" <a@b.c.invalid> - 2012-04-05 13:08 +0200
          Re: template metaprogramming in C? "io_x" <a@b.c.invalid> - 2012-04-05 13:36 +0200
            Re: template metaprogramming in C? pete <pfiland@mindspring.com> - 2012-04-06 18:43 -0400
              Re: template metaprogramming in C? pete <pfiland@mindspring.com> - 2012-04-06 19:20 -0400
      Re: template metaprogramming in C? "io_x" <a@b.c.invalid> - 2012-04-05 13:08 +0200
  Re: template metaprogramming in C? William Ahern <william@wilbur.25thandClement.com> - 2012-03-24 19:01 -0700
    Re: template metaprogramming in C? William Ahern <william@wilbur.25thandClement.com> - 2012-03-24 19:10 -0700
  Re: template metaprogramming in C? Ian Collins <ian-news@hotmail.com> - 2012-03-25 15:25 +1300
    Re: template metaprogramming in C? Rui Maciel <rui.maciel@gmail.com> - 2012-03-25 15:13 +0100
      Re: template metaprogramming in C? Ian Collins <ian-news@hotmail.com> - 2012-03-26 07:10 +1300
        Re: template metaprogramming in C? Rui Maciel <rui.maciel@gmail.com> - 2012-03-25 20:31 +0100
          Re: template metaprogramming in C? nick_keighley_nospam@hotmail.com - 2012-03-26 00:54 -0700

csiph-web