moving average

B

Bill Cunningham

I have written this little program and I'm sure there's errors in it but
not like I imagined the compiler says EXIT_FAILURE passed to exit is
recognized. WHat's wrong here.

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

int main(int argc, char **argv)
{
double ma, ab1, price, res;
int a;
if (argc != 3) {
puts("ma usage error: ma, price");
exit(EXIT_FAILURE);
}
if ((a = isdigit(argv[1])) != 0) {
puts("ma must be pos number");
exit(EXIT_FAILURE);
}
ma = strtod(argv[1], NULL);
price = strtod(argv[2], NULL);
res = (fabs(price) - fabs(ma)) / fabs(ma);
printf("%f.2\n", res);
return 0;
}

This is suppoed to take two args. The first, the number of days in a
moving average. The second the price of a security. This is the formula.

(price-ma)/ma and the result in res can be positive or negative.

Bill
 
M

Martin Ambuhl

Bill said:
I have written this little program and I'm sure there's errors in it but
not like I imagined the compiler says EXIT_FAILURE passed to exit is
recognized. WHat's wrong here.

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

Your headers do not include <stdlib.h>, which is where the function
exit() is prototyped and the macros EXIT_FAILURE and EXIT_SUCCESS are
defined. Without EXIT_FAILURE defined, the compiler need not recognize
that identifier. I really don't understand your sentence above: it
would be very unusual for a compiler to tell you that a macro is
recognized. Did you leave out the word "not"?

In C99 the function _Exit is also prototyped in <stdlib.h> is terminate
a program normaly but without calling any functions that your may have
installed with atexit() or any signal handlers you have installed with
signal(). The function abort, also prototyped in <stdlib.h>, is a
somewhat more draconian way to end things.
 
B

Barry Schwarz

I have written this little program and I'm sure there's errors in it but
not like I imagined the compiler says EXIT_FAILURE passed to exit is
recognized. WHat's wrong here.

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

int main(int argc, char **argv)
{
double ma, ab1, price, res;
int a;
if (argc != 3) {
puts("ma usage error: ma, price");

If you are going to print an error about insufficient arguments, why
not tell the user what the correct usage is? Something like
puts("ma usage error; should be \"ma days price\"");
exit(EXIT_FAILURE);
}
if ((a = isdigit(argv[1])) != 0) {

As Joachim pointed out, this requires a diagnostic. If you knew how
to fix the problem, why didn't you before posting? If you didn't
know, why didn't ask about it? Waiting to see how many of us noticed
it?
puts("ma must be pos number");
exit(EXIT_FAILURE);
}

If you fix this to test all the characters in argv[1], it raises the
question of why you didn't see the need to test argv[2].
ma = strtod(argv[1], NULL);

This value is a number of days.
price = strtod(argv[2], NULL);

This value is a price.
res = (fabs(price) - fabs(ma)) / fabs(ma);

1 - What are the units when you compute 4 dollars - 3 days? It is not
dollars; it is not days. Does the result have any possible meaning?

2 - fabs simply returns the mathematical absolute value of a double.
Is there any way either ma or price could be negative? What would it
mean to have -4 days? Or a security that cost -8 dollars (would they
pay you to buy it)?

3 - The formula is similar to a percentage change formula
(value1 - value2) / value2
than to a moving average formula. By definition, a moving average has
multiple values, one for each time period of interest. In order to
compute its values, you would need the number of days and a price for
each day.
printf("%f.2\n", res);

The format string can contain explanatory text in addition to format
specifications. A number by itself on the screen usually has little
meaning.
return 0;
}

This is suppoed to take two args. The first, the number of days in a
moving average. The second the price of a security. This is the formula.

(price-ma)/ma and the result in res can be positive or negative.

Would you care to tell us where you saw such a formula?
 
B

Bill Cunningham

Barry Schwarz said:
I have written this little program and I'm sure there's errors in it
but
not like I imagined the compiler says EXIT_FAILURE passed to exit is
recognized. WHat's wrong here.

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

int main(int argc, char **argv)
{
double ma, ab1, price, res;
int a;
if (argc != 3) {
puts("ma usage error: ma, price");

If you are going to print an error about insufficient arguments, why
not tell the user what the correct usage is? Something like
puts("ma usage error; should be \"ma days price\"");
exit(EXIT_FAILURE);
}
if ((a = isdigit(argv[1])) != 0) {

The intention I had was to make sure that a negative number was not a
first parameter.
As Joachim pointed out, this requires a diagnostic. If you knew how
to fix the problem, why didn't you before posting? If you didn't
know, why didn't ask about it? Waiting to see how many of us noticed
it?
puts("ma must be pos number");
exit(EXIT_FAILURE);
}

If you fix this to test all the characters in argv[1], it raises the
question of why you didn't see the need to test argv[2].
ma = strtod(argv[1], NULL);

This value is a number of days.
price = strtod(argv[2], NULL);

This value is a price.
res = (fabs(price) - fabs(ma)) / fabs(ma);

1 - What are the units when you compute 4 dollars - 3 days? It is not
dollars; it is not days. Does the result have any possible meaning?

2 - fabs simply returns the mathematical absolute value of a double.
Is there any way either ma or price could be negative? What would it
mean to have -4 days? Or a security that cost -8 dollars (would they
pay you to buy it)?

3 - The formula is similar to a percentage change formula
(value1 - value2) / value2
than to a moving average formula. By definition, a moving average has
multiple values, one for each time period of interest. In order to
compute its values, you would need the number of days and a price for
each day.
printf("%f.2\n", res);

The format string can contain explanatory text in addition to format
specifications. A number by itself on the screen usually has little
meaning.
return 0;
}

This is suppoed to take two args. The first, the number of days in a
moving average. The second the price of a security. This is the formula.

(price-ma)/ma and the result in res can be positive or negative.

Would you care to tell us where you saw such a formula?
I think I may have confused *brain fart* an oscillator formula I use
with a moving average (which would be perfect to store in trees that I'm
having trouble with. As you noticed my user friendliness in my coding isn't
good and there's a reason for that. 1- Most of my code is designed for me
and all I need is a reminder if I forget usage terms and 2- I am a bit of a
purist. I like the old OSs like ITS and older systems. Talk about not user
friendly *Whew* if you can navigate ITS you better have some kind of text on
you.

Bill
 
C

CBFalconer

Bill said:
I have written this little program and I'm sure there's errors in
it but not like I imagined the compiler says EXIT_FAILURE passed
to exit is recognized. WHat's wrong here.

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

int main(int argc, char **argv)
{
double ma, ab1, price, res;
int a;
if (argc != 3) {
puts("ma usage error: ma, price");
exit(EXIT_FAILURE);
}
if ((a = isdigit(argv[1])) != 0) {
puts("ma must be pos number");
exit(EXIT_FAILURE);
}
ma = strtod(argv[1], NULL);
price = strtod(argv[2], NULL);
res = (fabs(price) - fabs(ma)) / fabs(ma);
printf("%f.2\n", res);
return 0;
}

The function 'exit' is not recognized. You need <stdlib.h>.
Simpler to simply use "return EXIT_FAILURE".
 
O

osmium

CBFalconer said:
Bill said:
I have written this little program and I'm sure there's errors in
it but not like I imagined the compiler says EXIT_FAILURE passed
to exit is recognized. WHat's wrong here.

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

int main(int argc, char **argv)
{
double ma, ab1, price, res;
int a;
if (argc != 3) {
puts("ma usage error: ma, price");
exit(EXIT_FAILURE);
}
if ((a = isdigit(argv[1])) != 0) {
puts("ma must be pos number");
exit(EXIT_FAILURE);
}
ma = strtod(argv[1], NULL);
price = strtod(argv[2], NULL);
res = (fabs(price) - fabs(ma)) / fabs(ma);
printf("%f.2\n", res);
return 0;
}

The function 'exit' is not recognized. You need <stdlib.h>.
Simpler to simply use "return EXIT_FAILURE".

Why should exit() not be allowed? Isn't there a presumed definition of int
exit() in some other translation unit? Bill specifically mentioned a
*compiler* error, not a linker error..
 
B

Bill Cunningham

[snip]
The function 'exit' is not recognized. You need <stdlib.h>.
Simpler to simply use "return EXIT_FAILURE".

Is stdlib.h needed for return EXIT_FAILURE ?

Bill
 
B

Ben Bacarisse

Bill Cunningham said:
[snip]
The function 'exit' is not recognized. You need <stdlib.h>.
Simpler to simply use "return EXIT_FAILURE".

Is stdlib.h needed for return EXIT_FAILURE ?

Yes. It does mean that you might as well use exit, but whichever way
you decide to go you need to #include <stdlib.h>.
 
C

CBFalconer

osmium said:
:
.... snip ...


Why should exit() not be allowed? Isn't there a presumed
definition of int exit() in some other translation unit? Bill
specifically mentioned a *compiler* error, not a linker error.

Because it is prototyped in <stdlib.h>.

7.20.4.3 The exit function
Synopsis
[#1]
#include <stdlib.h>
void exit(int status);

Description

[#2] The exit function causes normal program termination to
occur. If more than one call to the exit function is
executed by a program, the behavior is undefined.
 
M

Martin Ambuhl

Bill said:
Is stdlib.h needed for return EXIT_FAILURE ?

This message from you <[email protected]> is
time stamped
Date: Sat, 27 Dec 2008 21:07:07 -0500

I answered this question fully in my response
<[email protected]> time stamped
Date: Sat, 27 Dec 2008 03:23:06 -0500

In the 18 hours between my response and your posting you might have
bothered to read my response. Why should anyone try to help you if you
just ignore that help?
 
K

Keith Thompson

CBFalconer said:
Because it is prototyped in <stdlib.h>.
[snip]

Yes, it's prototyped in <stdlib.h>, but in C90 a call to exit()
creates an implicit declaration "int exit();". In C99 no such
implicit declaration is created, but (a) full C99 compilers are rare,
and (b) even a fully conforming C99 compiler isn't required to do more
than issue a warning for a call to exit() with no visible prototype.
(Note that exit() returns void, not int, but in many implementations
this won't cause any visible problems.)

On the other hand, since EXIT_FAILURE isn't a function, any reference
to it without a visible declaration is a constraint violation in
either C90 or C99, and under most compilers will result in a
compilation failure.

Of course, if you're going to refer to either exit or EXIT_FAILURE,
you absolutely should have a "#include <stdio.h>".

As for whether "exit(EXIT_FAILURE);" or "return EXIT_FAILURE;" is
preferred, if it occurs within main() it makes essentially no
difference.
 
O

osmium

Keith Thompson said:
CBFalconer said:
Because it is prototyped in <stdlib.h>.
[snip]

Yes, it's prototyped in <stdlib.h>, but in C90 a call to exit()
creates an implicit declaration "int exit();". In C99 no such
implicit declaration is created, but (a) full C99 compilers are rare,
and (b) even a fully conforming C99 compiler isn't required to do more
than issue a warning for a call to exit() with no visible prototype.
(Note that exit() returns void, not int, but in many implementations
this won't cause any visible problems.)

On the other hand, since EXIT_FAILURE isn't a function, any reference
to it without a visible declaration is a constraint violation in
either C90 or C99, and under most compilers will result in a
compilation failure.

Of course, if you're going to refer to either exit or EXIT_FAILURE,
you absolutely should have a "#include <stdio.h>".

Thanks Keith, that explains the annoying result I was seeing when I tried
this, I seem to have a "pre 99" compiler; neither fish nor fowl. Are there
other implicit declarations and a list of them somewhere? I have tried to
ignore C99 but that doesn't seem to be a good idea any more.

ISTM the "C Language" per se, is a thing of the past. There are just
various dialects implemented by various compiler writers and depend on their
tastes for what to implement and what to ignore. The only thing I know of
in C99 that looked useful to me was the ability to handle complex numbers.
(And the // comments.) In the case at hand they have taken a simple rule
and made it more complex, with no obvious (to me) gain.
 
K

Keith Thompson

osmium said:
Thanks Keith, that explains the annoying result I was seeing when I tried
this, I seem to have a "pre 99" compiler; neither fish nor fowl. Are there
other implicit declarations and a list of them somewhere? I have tried to
ignore C99 but that doesn't seem to be a good idea any more.

Most C compilers these days are pre-C99. Most have a mode in which
they at least attempt to conform to the C90 standard.

In C90, a call to a function with no visible declaration results in an
implicit declaration of that function, assumed to return int and to
have a fixed but unspecified number and type of arguments. This rule
exists for compatibilty with pre-C90 (K&R) implementations, which
didn't support function prototypes. Even in pure C90 code, it's best
not to depend on this. Always provide prototypes for all functions
you call; if at all possible, do this by #include'ing the proper
header.
ISTM the "C Language" per se, is a thing of the past. There are just
various dialects implemented by various compiler writers and depend on their
tastes for what to implement and what to ignore. The only thing I know of
in C99 that looked useful to me was the ability to handle complex numbers.
(And the // comments.) In the case at hand they have taken a simple rule
and made it more complex, with no obvious (to me) gain.

There are a number of "dialects", roughly one (or more) per
implementation. But these dialects typically consist of the core
language plus some number of extensions. If you want to write
portable code, you can always just ignore the extensions -- and most
compilers provide an option to disable them. There are currently just
two versions of the core language (where "core language" includes the
standard library): C90 and C99. (Pre-C90 K&R C is largely obsolete,
and C201X is in the early design stages.)

As for implicit function declarations, C99 has made the rule much
*simpler*, with the obvious gain of better compile-time checking. In
C90, if you call a function with no visible declaration, the compiler
assumes a particular declaration, and if that doesn't match the actual
function, you lose. In C99, there are no implicit function
declarations.
 
C

CBFalconer

Martin said:
This message from you <[email protected]>
is time stamped: Date: Sat, 27 Dec 2008 21:07:07 -0500

I answered this question fully in my response
<[email protected]> time stamped
Date: Sat, 27 Dec 2008 03:23:06 -0500

In the 18 hours between my response and your posting you might
have bothered to read my response. Why should anyone try to help
you if you just ignore that help?

While it is not likely, remember that Usenet is a 'best efforts'
delivery promise, and Cunningham may never have seen your message.
More likely he didn't understand it, though.
 
B

Bill Cunningham

Martin Ambuhl said:
This message from you <[email protected]> is
time stamped
Date: Sat, 27 Dec 2008 21:07:07 -0500

I answered this question fully in my response
<[email protected]> time stamped
Date: Sat, 27 Dec 2008 03:23:06 -0500

In the 18 hours between my response and your posting you might have
bothered to read my response. Why should anyone try to help you if you
just ignore that help?

Martin went back and checked the thread and I'm not quite sure what your
talking about. Are you referring to this post?

----

Bill said:
I have written this little program and I'm sure there's errors in it
but not like I imagined the compiler says EXIT_FAILURE passed to exit is
recognized. WHat's wrong here.

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

Your headers do not include <stdlib.h>, which is where the function
exit() is prototyped and the macros EXIT_FAILURE and EXIT_SUCCESS are
defined. Without EXIT_FAILURE defined, the compiler need not recognize
that identifier. I really don't understand your sentence above: it
would be very unusual for a compiler to tell you that a macro is
recognized. Did you leave out the word "not"?

In C99 the function _Exit is also prototyped in <stdlib.h> is terminate
a program normaly but without calling any functions that your may have
installed with atexit() or any signal handlers you have installed with
signal(). The function abort, also prototyped in <stdlib.h>, is a
somewhat more draconian way to end things.
--------
What is your question? The question I see is did you leave out the word
not and I went back through the thread to my posts and I didn't see where I
made any typos. Can you ask your question again ?

Bill
 
O

osmium

Bill said:
Martin went back and checked the thread and I'm not quite sure
what your talking about. Are you referring to this post?

How do *you* know that "Martin went back..." ??????

Look up the word "your" in a dictionary. No, this is not a trick question.
 
B

Ben Bacarisse

Bill Cunningham said:
Martin went back and checked the thread and I'm not quite sure what your
talking about. Are you referring to this post?

Yes he is. Have gave the post's unique ID so there should be no doubt
about which message is being talked about. Look in the full message
headers to see the ID -- it is the "Message-ID:" header.

Your headers do not include <stdlib.h>, which is where the function
exit() is prototyped and the macros EXIT_FAILURE and EXIT_SUCCESS are
defined. Without EXIT_FAILURE defined, the compiler need not recognize
that identifier. I really don't understand your sentence above: it
would be very unusual for a compiler to tell you that a macro is
recognized. Did you leave out the word "not"?

In C99 the function _Exit is also prototyped in <stdlib.h> is terminate
a program normaly but without calling any functions that your may have
installed with atexit() or any signal handlers you have installed with
signal(). The function abort, also prototyped in <stdlib.h>, is a
somewhat more draconian way to end things.
What is your question? The question I see is did you leave out the word
not and I went back through the thread to my posts and I didn't see where I
made any typos. Can you ask your question again ?

He wanted to know (as did I) what you meant by "the compiler says
EXIT_FAILURE passed to exit is recognized". This is an odd thing to
say since "recognise" is not a well-defined technical term. My
compiler tells me something more useful: that the identifier
EXIT_FAILURE is undeclared (it may or may not have recognised it, but
the fact that it is undeclared is much more important). In fact this
error is bad enough to stop it generating an executable.

In general it best to include the compiler's error message word for
word unless you are sure you know what it means. If you are not sure,
there is the danger that you will paraphrase it in a way that is
puzzling.

I think everyone has said "you need to #include <stdlib.h>" and that
is likely to explain whatever the compiler is telling you. People
have pointed out other things that are wrong, but that is just the
bonus you get from Usenet.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

oscillator 51
Command Line Arguments 0
average true range 21
Linux: using "clone3" and "waitid" 0
pow type problem 6
percentage 8
code 50
completely stuck 14

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top