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


Groups > comp.lang.c > #54904

Re: C program. run time abend, range fault.

From jt@toerring.de (Jens Thoms Toerring)
Newsgroups comp.lang.c
Subject Re: C program. run time abend, range fault.
Date 2014-12-18 01:00 +0000
Organization Freie Universitaet Berlin
Message-ID <cfen9eFeib6U1@mid.uni-berlin.de> (permalink)
References <3636bf65-fa27-43a6-b54f-45477b249b5d@googlegroups.com> <cfag0nF9r44U1@mid.uni-berlin.de> <m6q1m6$jh6$1@speranza.aioe.org> <cfc030Fn3o1U1@mid.uni-berlin.de> <e3eb318e-355d-411b-8215-f2f022485b08@googlegroups.com>

Show all headers | View raw


Shiva <subramanian.klu@gmail.com> wrote:
> Hi all,

> Thanks for your immediate responses and valuable answers. It seems that the
> above code I posted from memory (hell let lose) is wrong. There were so many
> reasons why I did not post the exact code before. Some reasons I can't state
> here. But I am doing so now, please find the correct code below. Any faults
> in the below program are possibly typo. My confidence in saying that the
> below code is right, is from experience. The code works for the correct
> inputs. It just fails when the wrong input is provided and that's where I
> want to correct the code.

I'd love to try your code but there are still too many issues

> Code: 

>   double A,v,w,x,y,z;
>   double funcC (int, int, int, double, double, double);
>   double funcB (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, pB, pC;
>      int pD, pF, pN, count, limit;
>      long int r;
>      double q;
>      pA = (double) Lp1/100;
>      pB = (double) Lp2/100;
>      pC = (double) Lp3/100;
>      pD = Lp4;
>      pF = Lp5;
>      pN = Lp6;
>      limit = 100;
>      q = pow (10,7);

This could more easily written as 'q = 1e7;'.

>       A = 10000001/q;
>      z = funcB (pF, pN, pD, pC, pB, pA);
>      v = A;
>      w = z;

None of the variables 'A', 'z', 'v' or 'w' are defined in your
program. And 'w' and 'w' are never agin used in this function.
Are they global variables for which you haven't show the the
source lines?

>      A = 1.05;
>      z = funcB (pF, pN, pD, pC, pB, pA);
>      x = A;
>      y = z;

There's also no definition for 'x' and 'y'. And they never get
used again in thsi function, so what they are goof for? 

>      count = 0;
>      do
>       { 
>         A = fuc (pF, pN, pD, pC, pB, pA);

'fuc()' is not a function anywhere in sight. Probably you meant
'funcB'? Or perhaps 'funcC'?

>         count = count + 1;
>        } while ((( z > = 0.0001) || ( z <= -0.0001)) && (count < limit));

You're never changing 'z' in this loop. So the loop will either
be run just once (if 'z' you started with is not in the interval
[-1e-4,1e-4]) or it will be run exacty 'limit' times.

>       if ( count == limit)
>       A = -1;
>       else
>       A = (pow (A, 12) - 1) * 100; 
>       A = (double) ((long long int) (( A + 0.005) * 100 )) / 100;

This looks a bit like you want to round 'A' to two digits
after the decimal point. In that case a more elegant so-
lution (without all these casts) might be to be

        A = 0.01 * floor( 100 * A + 0.5 );

Note that this (as your method) may not be what you want should
'A' ever be negative...

>       }

This closing curly brace would be the end of the function.
So something must be missing here/

>       A = A * 1000;
>       r = (long int) A;
>       return (r);
>   }
>    

> double funcB (int pF, int pN, int pD, double pC, double pB, double pA) 
> { 
>   double F1, F2; 
>   F1 = pow (ACY, -parmN); 
>   F2 = pow (ACY, parmF); 

Neither 'ACY' nor 'paramN' or 'paramH' are defined (and assigned a
value) anywhere I can see.

>   z = ((F2 * ACY - 1) ) / (1 - F1));

'z' is not defined. And there's one too many ')' here.

>   z = pB/ z;
>   z = z - pA - (pC/ pow (ACY, pD));
>   return (z);
> }


>   double funcC (int pF, int pN, int pD, double pC, double pB, double pA) 
>    { 
>       double a,b,c,d,e;
>       a = x - v; 
>       b = w - y;  
>       A = V + (fabs(w)/ ((fabs(b)) / (a)));
>       z = funcB (pF, pN, pD, PC, pB, pA); 

No definitions for 'x', 'v', 'w', 'y', 'A', 'V' 'w' or 'z'. Note
that C distinguishes between upper- and lower-case variable names,
so 'v' is a different variable than 'V'. Same for 'a' and 'A'.

>      if (z > 0) 
>      { 
>      v = A; 
>      w = z; 
>      } else if (z < 0) 
>      { 
>      x = A ;  
>      y = z; 
>      } 

What about the case that 'z' is exactly 0?

>      return (A); 
>      } 

> Not using a C++ compiler. Just using the C89 compiler. Of that I'm sure.
> Will try some C++ code and try to execute using the same compiler tomorrow,
> just to be double sure.

> -- I'd try to simply print out the value of the nacro DBL_MIN from 
> -- <limits.h> to check if this what's going on your machine. 

> I'd try the above also tomorrow.

> Also just to state it clear the source file has only the above code. And it
> is compiled.

Then the source code you have is definitely not what you show
above. There's no chance that what you posted here would ever
compile. Not with a C (nor with a C++) compiler, sorry.

> funcB and funcC are local here. (by local I mean, not used
> anywhere else outside this source file) But the funcA is called by the Cobol
> program who passes the input values onto the funcA. Those six Lp1, Lp2, etc.

> And somebody mentioned NaN on seeing the while condition for z. I'm not sure
> what NaN is.

'NaN' stands for "not a number". Floating points on modern
systems typically can be either valid numbers, NaN or Inf
(for infinity, and there's positive and negative infinity).
'NaN' can be used to mark a varuable not to hold a valid
numerical value. Several built-in C math functions may return
NaN if the input is NaN. And pow() may return NaN if the first
argument is negative and the second argument is a non-integer
- in that case 'errno' will be set to the value 'EDOM'. See
the documentation for your system on pow() to figure out what
exactly it will do on unsuitable input - it's not specified in
all detail in the C89 standard. All stated there is:

   The pow function computes x raised to the power y .  A domain error
   occurs if x is negative and y is not an integer.  A domain error
   occurs if the result cannot be represented when x is zero and y is
   less than or equal to zero.  A range error may occur.

The finer details are left to the implementors, i.e. who-ever
wrote the libm (C math library) for your system. It should be
documented somewhere.

> And this: 

> The general technique is to: 

> (a) include errno.h 
> (b) set errno = 0 before the function call in question (here, pow) 
> (c) test for errno == ERANGE after the call. 

Well, yes. First, there's a second value 'errno' can be set
to, 'EDOM' (domain error, i.e. the input was something pow()
can't be applied to). And then your program, as far as I un-
derstood, stops with some message about a "range error". That
sounds like your system is set up not to tolerate any such
errors and to ubstead kill the program instead. If that's
the case this will hapen before you have any chance to
check 'errno'. This could also be something that is set
up by the COBOL program from which you call the C functions
from. In that case the only practical solution might be to
check everything you pass to pow() to make sure there are
not any arguments that could trigger a program abort. Or
you may have to catch some "floating point exception" -
but how to do this is system-dependent.

> Below is the correct input that's usually passed into the program. (correct
> input)

> Lp1 = 1000000;
> Lp2 = 25000;
> Lp3 = 2;
> Lp4 = 60;
> Lp5 = 0;
> Lp6 = 0;

> For which the A value comes around 17.something which is right. 

> Below is the bad input for which the code does not stumble, but picks out
> the correct error catch - setting A to -1 ...

> Lp1 = 1759672;
> Lp2 = 23306;
> Lp3 = 2;
> Lp4 = 60;
> Lp5 = 0;
> Lp6 = 0;

> Another example.

> Lp1 = 1636100;
> Lp2 = 13844;
> Lp3 = 2;
> Lp4 = 60;
> Lp5 = 0;
> Lp6 = 0;

> Because for the above two bad inputs, the count reaches the limit (100) set
> in the program and sets the -1 value for A back to the calling COBOL
> program.

> Now the following is really bad input for which the code fails, unable to
> handle the range fault.

> Lp1 = 200000;
> Lp2 = 7345;
> Lp3 = 2;
> Lp4 = 12;
> Lp5 = 0;
> Lp6 = 0;

> The above input when reaching count 66, produces the following value for F1;

> F1 = 2.189569 e -77

> So at the 67th count, when it goes to funcC who calls funcB at line 5 of
> it's function - the first calculation of funcB is F1 where this time it
> hits a value far below the previous one. And it fails.

Give me some code I can compile and I'll try it with these values.
At the moment there's not enough information to even start guessing
what might happen.

> I've debugged the code, I've put in displays, introduced main () to print
> these outputs and so much more. I can find this really bad input even before
> passing onto funcC who passes it onto funcB - but I don't want to be the
> judge of that. I want the program to decide whether the range fault occurs
> and if so, then let the calling program know that the input is bad - by
> setting the value of A as -1, just like the other bad inputs do.

If you have already a working compiling C program for testing
then I would strongly recommend to post it exactly as it is
and not something that is only a rough approximation. Copy
and paste and don't re-type it! This will safe everyones
time;-)
                     Regards, Jens
-- 
  \   Jens Thoms Toerring  ___      jt@toerring.de
   \__________________________      http://toerring.de

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