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


Groups > comp.lang.c > #55647

Re: C program. run time abend, range fault. Is this the correct solution?

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: C program. run time abend, range fault. Is this the correct solution?
Date 2014-12-31 21:51 +0000
Organization A noiseless patient Spider
Message-ID <877fx7wks0.fsf@bsb.me.uk> (permalink)
References <3636bf65-fa27-43a6-b54f-45477b249b5d@googlegroups.com> <m71ng8$ofl$1@speranza.aioe.org> <m73jbn$3dc$1@speranza.aioe.org> <ea8bbea1-0471-4243-bb70-205ca6522d4c@googlegroups.com>

Show all headers | View raw


Shiva <subramanian.klu@gmail.com> writes:
<snip>
> Like some of you pointed out, I still had posted wrong code. Too bad
> of me, I could've at least compiled it. And the input I provided was
> in the wrong order as well.
>
> Use the following: 
>
> #include<stdio.h> 
>
> #include<stdlib.h> 
>
> #include<math.h> 
>
> double A,v,w,x,y,z; 
>
> double funcC (int, int, int, double, double, double); 
>
> double funcB (int, int, int, double, double, double); 

...etc.

I found that my brain stared to hurt trying to follow the code.  It
computes by setting six one-letter globals that seem to ties all the
functions together.  It hurt so much that I tried to re-write it without
the globals.  It's not that hard:

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

double funcB(double, int, int, int, double, double, double); 

long int funcA(long long int Lp1, long long int Lp2,
               long long int Lp3, long long int Lp4,
               long long int Lp5, long long int Lp6)
{ 
     double pA = Lp1/100.0, pB = Lp2/100.0, pC = Lp3/100.0;
     int pD = Lp4, pF = Lp5, pN = Lp6;

     double v = 10000001 / pow(10,7);
     double w = funcB(v, pF, pN, pD, pC, pB, pA);

     double x = 1.05;
     double y = funcB(x, pF, pN, pD, pC, pB, pA); 

     int count = 0, limit = 100;
     double A, z;
     do {
          A = v + fabs(w) / (fabs(w - y) / (x - v));
          z = funcB(A, pF, pN, pD, pC, pB, pA); 
          if (z > 0) {
               v = A;
               w = z;
          }
          else if (z < 0) {
               x = A;
               y = z;
          }
     } while ((z >= 0.0001 || z <= -0.0001) && ++count < limit);
      
     if (count == limit) 
          A = -1; 
     else {
          A = (pow(A, 12) - 1) * 100;
          A = (long long int)((A + 0.005) * 100) / 100.0;
     }
     return A * 1000;
}   

double funcB(double A, int pF, int pN, int pD, double pC, double pB, double pA) 
{
     return pB * (1 - pow(A, -pN)) / pow(A, pF) / (A - 1) - pA - pC / pow(A, pD);
}

int main(void)
{
     printf("A value = %ld \n", funcA(1000000, 25000, 0, 0, 0, 60));
}

I've also removed redundant parentheses and removed many temporary
variables.  I know not everyone likes that sort of change, but I find
shorter code with fewer names almost always simpler to debug.

I know this won't help you (the OP) but it may cause more people to look
harder at the problem you are having.

It did make one thing clear.  All you power operations are integer
powers, so you could very simply re-write with your own pow function
that checks for multiplication overflow in a loop (either a naive one or
one using the squaring algorithm).

<snip>
> 3) Something like what Jacob has suggested. Well thought and a good
> one too. A check for the inputs before we go into the pow function,
> now this would avoid the problem that I might face in point 2. But I
> think, like Jen says, even if I do these checks before I go to the pow
> function - this is going to make my code a lot different than where I
> started because I might have to eventually add this check to every
> single line of the above code that calls to pow function (because the
> line that I mentioned which failed is not the only pow function in the
> code, sadly)

No, just write a function that does the check.

<snip>
> 4) Now this might be more or less like point 3 extended. I might have
> to write a pow function myself. Now that's going to be a real pain. A
> flawless, accurate pow function that could handle the error that
> math.h is letting go. But if accomplished, this could be my safest way
> out.

This is not too hard given the fact that your exponents are all
integers.

I note that the example call given does not fail on my system.  Did you
post input that fails on yours?

<snip>
-- 
Ben.

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


Thread

C program. run time abend, range fault. Shiva <subramanian.klu@gmail.com> - 2014-12-16 00:47 -0800
  Re: C program. run time abend, range fault. jt@toerring.de (Jens Thoms Toerring) - 2014-12-16 10:31 +0000
    Re: C program. run time abend, range fault. Shiva <subramanian.klu@gmail.com> - 2014-12-16 11:33 -0800
      Re: C program. run time abend, range fault. Barry Schwarz <schwarzb@dqel.com> - 2014-12-16 14:27 -0800
      Re: C program. run time abend, range fault. jt@toerring.de (Jens Thoms Toerring) - 2014-12-16 23:08 +0000
      Re: C program. run time abend, range fault. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-12-16 23:44 +0000
    Re: C program. run time abend, range fault. glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-12-16 19:37 +0000
      Re: C program. run time abend, range fault. jt@toerring.de (Jens Thoms Toerring) - 2014-12-17 00:12 +0000
        Re: C program. run time abend, range fault. Shiva <subramanian.klu@gmail.com> - 2014-12-17 13:05 -0800
          Re: C program. run time abend, range fault. Barry Schwarz <schwarzb@dqel.com> - 2014-12-17 15:06 -0800
            Re: C program. run time abend, range fault. Richard Heathfield <invalid@see.sig.invalid> - 2014-12-17 23:15 +0000
              Re: C program. run time abend, range fault. Keith Thompson <kst-u@mib.org> - 2014-12-17 16:00 -0800
            Re: C program. run time abend, range fault. Keith Thompson <kst-u@mib.org> - 2014-12-17 15:56 -0800
            Re: C program. run time abend, range fault. Shiva <subramanian.klu@gmail.com> - 2014-12-18 12:28 -0800
              Re: C program. run time abend, range fault. Barry Schwarz <schwarzb@dqel.com> - 2014-12-18 14:07 -0800
              Re: C program. run time abend, range fault. Barry Schwarz <schwarzb@dqel.com> - 2014-12-18 14:16 -0800
              Re: C program. run time abend, range fault. jacob navia <jacob@spamsink.net> - 2014-12-19 00:55 +0100
              Re: C program. run time abend, range fault. jt@toerring.de (Jens Thoms Toerring) - 2014-12-19 00:18 +0000
              Re: C program. run time abend, range fault. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-12-19 01:12 +0000
          Re: C program. run time abend, range fault. jt@toerring.de (Jens Thoms Toerring) - 2014-12-18 01:00 +0000
          Re: C program. run time abend, range fault. James Kuyper <jameskuyper@verizon.net> - 2014-12-17 22:29 -0500
            Re: C program. run time abend, range fault. gazelle@shell.xmission.com (Kenny McCormack) - 2014-12-18 09:18 +0000
              Re: C program. run time abend, range fault. Martin Shobe <martin.shobe@yahoo.com> - 2014-12-18 11:10 -0600
                Re: C program. run time abend, range fault. Shiva <subramanian.klu@gmail.com> - 2014-12-18 11:38 -0800
                Re: C program. run time abend, range fault. James Kuyper <jameskuyper@verizon.net> - 2014-12-18 15:24 -0500
                Re: C program. run time abend, range fault. Shiva <subramanian.klu@gmail.com> - 2014-12-18 12:37 -0800
  C program. run time abend, range fault. asetofsymbols@gmail.com - 2014-12-18 22:21 -0800
  Re: C program. run time abend, range fault. Is this the correct solution? jacob navia <jacob@spamsink.net> - 2014-12-19 18:32 +0100
    Re: C program. run time abend, range fault. Is this the correct solution? jacob navia <jacob@spamsink.net> - 2014-12-20 11:34 +0100
      Re: C program. run time abend, range fault. Is this the correct solution? Shiva <subramanian.klu@gmail.com> - 2014-12-31 11:48 -0800
        Re: C program. run time abend, range fault. Is this the correct solution? James Kuyper <jameskuyper@verizon.net> - 2014-12-31 15:49 -0500
        Re: C program. run time abend, range fault. Is this the correct solution? Keith Thompson <kst-u@mib.org> - 2014-12-31 13:07 -0800
          Re: C program. run time abend, range fault. Is this the correct solution? Geoff <geoff@invalid.invalid> - 2014-12-31 13:57 -0800
            Re: C program. run time abend, range fault. Is this the correct solution? Keith Thompson <kst-u@mib.org> - 2014-12-31 16:02 -0800
              Re: C program. run time abend, range fault. Is this the correct solution? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-12-31 22:55 -0800
            Re: C program. run time abend, range fault. Is this the correct solution? raltbos@xs4all.nl (Richard Bos) - 2015-01-05 14:56 +0000
        Re: C program. run time abend, range fault. Is this the correct solution? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-12-31 21:51 +0000
        Re: C program. run time abend, range fault. Is this the correct solution? jacob navia <jacob@spamsink.net> - 2015-01-01 02:51 +0100
          Re: C program. run time abend, range fault. Is this the correct solution? raltbos@xs4all.nl (Richard Bos) - 2015-01-05 15:00 +0000
            Re: C program. run time abend, range fault. Is this the correct solution? Shiva <subramanian.klu@gmail.com> - 2015-01-05 09:45 -0800
              Re: C program. run time abend, range fault. Is this the correct solution? Geoff <geoff@invalid.invalid> - 2015-01-05 11:02 -0800
                Re: C program. run time abend, range fault. Is this the correct solution? Shiva <subramanian.klu@gmail.com> - 2015-01-06 10:33 -0800
                Re: C program. run time abend, range fault. Is this the correct solution? James Kuyper <jameskuyper@verizon.net> - 2015-01-06 14:09 -0500
                Re: C program. run time abend, range fault. Is this the correct solution? Shiva <subramanian.klu@gmail.com> - 2015-01-06 12:32 -0800
                Re: C program. run time abend, range fault. Is this the correct solution? James Kuyper <jameskuyper@verizon.net> - 2015-01-06 15:47 -0500
                Re: C program. run time abend, range fault. Is this the correct solution? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-01-06 23:58 +0000
                Re: C program. run time abend, range fault. Is this the correct solution? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-01-07 04:31 +0000
                Re: C program. run time abend, range fault. Is this the correct solution? Robert Wessel <robertwessel2@yahoo.com> - 2015-01-06 23:36 -0600
                Re: C program. run time abend, range fault. Is this the correct solution? Richard Heathfield <invalid@see.sig.invalid> - 2015-01-07 07:08 +0000
                Re: C program. run time abend, range fault. Is this the correct solution? Shiva <subramanian.klu@gmail.com> - 2015-01-09 13:46 -0800
            Re: C program. run time abend, range fault. Is this the correct solution? Geoff <geoff@invalid.invalid> - 2015-01-05 09:56 -0800
  Re: C program. run time abend, range fault. Geoff <geoff@invalid.invalid> - 2015-01-01 14:38 -0800

csiph-web