abs question

  • Thread starter Bill Cunningham
  • Start date
B

Bill Cunningham

I have a program written with the intention to create a true range
between stocks. I had a program like this before and lost it so I am having
to re-write it. CL means current low, CH current high, PC previous close. My
system tells me abs is in stdlib.h and not just math.h. I don't know if
that's true or not. Anyway abs takes ints and my variables are all doubles.
I will post the code and anyone interested can comment. I hope it explains
itself with my brief explaination. The program takes CH CL and PC in that
order. I inputted 3.86 3.08 3.86 and there was no response. The answer is
..70.

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

int main(int argc, char *argv[])
{
double ch, cl, pc, chr, clr, pcr;
if (argc !=4) {
fprintf(stderr, "TR usage error ch cl pc\n");
exit(EXIT_FAILURE);
}
ch = strtod(argv[1], NULL);
cl = strtod(argv[2], NULL);
pc = strtod(argv[3], NULL);
chr = ch - cl;
clr = cl - pc;
pcr = ch - pc;
if (chr > clr && pcr)
printf("%.2f\n", chr);
if (clr > chr && pcr)
printf("%.2f\n", clr);
if (pcr > chr && clr)
printf("%.2f\n", pcr);
return 0;
}
 
B

Bill Cunningham

Oh god. Well never mind I have "discovered" fabs. I guess this is my
answer. I will try it anyway.

Bill
 
T

Tim Prince

Bill said:
..... My
system tells me abs is in stdlib.h and not just math.h. I don't know if
that's true or not. Anyway abs takes ints and my variables are all doubles.
abs() is of type int; fabs() was of type double in C89 <math.h>, it may be
in <tgmath.h> if your compiler has some C99 capability. Most compilers
can generate single instruction in-line implementation of fabs().
It's not difficult to check your "system," the answer would be in the
compiler or library docs, and probably in the header files in the
compiler's include folder.
I don't see a connection with the source code you showed.
 
M

Martin Ambuhl

Bill said:
I have a program written with the intention to create a true range
between stocks. I had a program like this before and lost it so I am having
to re-write it. CL means current low, CH current high, PC previous close. My
system tells me abs is in stdlib.h and not just math.h. I don't know if
that's true or not. Anyway abs takes ints and my variables are all doubles.

so use fabs()
I will post the code and anyone interested can comment. I hope it explains
itself with my brief explaination. The program takes CH CL and PC in that
order. I inputted 3.86 3.08 3.86 and there was no response. The answer is
.70.

Are you sure it isn't 0.80?
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
double ch, cl, pc, chr, clr, pcr;
if (argc !=4) {
fprintf(stderr, "TR usage error ch cl pc\n");
exit(EXIT_FAILURE);
}
ch = strtod(argv[1], NULL);
cl = strtod(argv[2], NULL);
pc = strtod(argv[3], NULL);
chr = ch - cl;
clr = cl - pc;
pcr = ch - pc;
if (chr > clr && pcr)
^^^^^^^^^^^^^^^^^^
This (and the two like it) are probably nonsense. See the version below.
printf("%.2f\n", chr);
if (clr > chr && pcr)
printf("%.2f\n", clr);
if (pcr > chr && clr)
printf("%.2f\n", pcr);
return 0;
}

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

int main(int argc, char *argv[])
{
double current_high, current_low, prevous_close, current_range,
low_vs_close, high_vs_close;
if (argc != 4) {
fprintf(stderr, "TR usage error:\n"
"program_name current_high current_low "
"prevous_close\n");
exit(EXIT_FAILURE);
}
current_high = strtod(argv[1], NULL);
current_low = strtod(argv[2], NULL);
prevous_close = strtod(argv[3], NULL);
current_range = current_high - current_low;
low_vs_close = current_low - prevous_close;
high_vs_close = current_high - prevous_close;
if (current_range > low_vs_close && current_range > high_vs_close)
printf("%.2f\n", current_range);
if (low_vs_close > current_range && low_vs_close > high_vs_close)
printf("%.2f\n", low_vs_close);
if (high_vs_close > current_range && high_vs_close > low_vs_close)
printf("%.2f\n", high_vs_close);
return 0;
}
 
B

Bill Cunningham

I don't see a connection with the source code you showed.

The thing is I really don't know where to use fabs in this code. I've
tried it in the printfs, for example,

if (chr > clr && pcr)
printf("%.2f\n", fabs(chr));
if (clr > chr && pcr)
printf("%.2f\n", fabs(clr));
if (pcr > chr && clr)
printf("%.2f\n", fabs(pcr));

This doesn't help. :( This is a math question. Where to use fabs.

Bill
 
B

Bill Cunningham

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

int main(int argc, char *argv[])
{
double current_high, current_low, prevous_close, current_range,
low_vs_close, high_vs_close;
if (argc != 4) {
fprintf(stderr, "TR usage error:\n"
"program_name current_high current_low "
"prevous_close\n");
exit(EXIT_FAILURE);
}
current_high = strtod(argv[1], NULL);
current_low = strtod(argv[2], NULL);
prevous_close = strtod(argv[3], NULL);
current_range = current_high - current_low;
low_vs_close = current_low - prevous_close;
high_vs_close = current_high - prevous_close;
if (current_range > low_vs_close && current_range > high_vs_close)
printf("%.2f\n", current_range);
if (low_vs_close > current_range && low_vs_close > high_vs_close)
printf("%.2f\n", low_vs_close);
if (high_vs_close > current_range && high_vs_close > low_vs_close)
printf("%.2f\n", high_vs_close);
return 0;
}

I think that might just be the answer martin :) thanks I will
incorporate this into my code.

Bill
 
N

Nick Keighley

    The thing is I really don't know where to use fabs in this code. I've
tried it in the printfs, for example,

if (chr > clr && pcr)
        printf("%.2f\n", fabs(chr));
    if (clr > chr && pcr)
        printf("%.2f\n", fabs(clr));
    if (pcr > chr && clr)
        printf("%.2f\n", fabs(pcr));

This doesn't help. :( This is a math question. Where to use fabs.

You use fabs() when you want the absolute value of a floating
point number (I can't be bothered to check if fabs() takes
a float or a double).

fabs(1.0) -> 1.0
fabs(-1.0) -> 1.0

I know nothing about finance so I have no idea if you actually
want to calculate fabs or where.

So
- what output did you get?
- what did you expect?

You may have noticed me asking this before...
 
R

Richard Tobin

Nick Keighley said:
You use fabs() when you want the absolute value of a floating
point number (I can't be bothered to check if fabs() takes
a float or a double).

fabs() predates prototypes, so it naturally takes a double because
of the default promotions.

-- Richard
 

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

C language. work with text 3
code question 74
Command Line Arguments 0
I'm facing a run-time error 14
std::abs problem 13
How to fix this code? 1
root(s) 6
average true range 21

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top