473,847 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

average true range

I have create these 2 files. Called main.c and atr.c. They seem to work
pretty well. I just wanted to submit them to see what if any errors others
that know more might find. Thanks.

atr.c

#include <stdio.h>

double
atr (double th, double tl, double wc)
{
if (th - tl th - wc && tl - wc < th - tl)
{
return th - tl;
}
if (th - wc th - tl && tl - wc < th - wc)
{
return th - wc;
}
if (tl - wc th - wc && th - tl < tl - wc)
{
return tl - wc;
}

}

main.c

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

extern double atr ( double th, double tl, double wc );

int
main (int argc, char *argv[])
{
if (argc != 4)
{
fprintf (stderr, "usage error th, tl, wc\n");
exit (EXIT_FAILURE);
}
double th, tl, wc;
th = strtod (argv[1], NULL);
tl = strtod (argv[2], NULL);
wc = strtod (argv[3], NULL);
printf("%.2f\n" , atr(th, tl, wc));

}

Bill
Jun 27 '08 #1
21 3269
Oh yes and tl means today's low. Th means today's high and wc means week's
close.

Bill
Jun 27 '08 #2
On Jun 11, 11:11*am, "Bill Cunningham" <nos...@nspam.c omwrote:
* * I have create these 2 files. Called main.c and atr.c. They seem towork
pretty well. I just wanted to submit them to see what if any errors others
that know more might find. Thanks.

atr.c

#include <stdio.h>

double
atr (double th, double tl, double wc)
{
* if (th - tl th - wc && tl - wc < th - tl)
* * {
* * * return th - tl;
* * }
* if (th - wc th - tl && tl - wc < th - wc)
* * {
* * * return th - wc;
* * }
* if (tl - wc th - wc && th - tl < tl - wc)
* * {
* * * return tl - wc;
* * }

}

main.c

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

extern double atr ( double th, double tl, double wc );

int
main (int argc, char *argv[])
{
* if (argc != 4)
* * {
* * * fprintf (stderr, "usage error th, tl, wc\n");
* * * exit (EXIT_FAILURE);
* * }
* double th, tl, wc;
* th = strtod (argv[1], NULL);
* tl = strtod (argv[2], NULL);
* wc = strtod (argv[3], NULL);
* printf("%.2f\n" , atr(th, tl, wc));

}
You haven't said what the intent of the atr function is, so how are
we to know whether it is getting the right answer?

Just a bit of math shows that there are cleaner comparisons.
For example,

if ( th - tl th - wc )
is the same as
if ( wc tl )

--
Fred Kleinschmidt
Jun 27 '08 #3
On Jun 11, 11:11*am, "Bill Cunningham" <nos...@nspam.c omwrote:
* * I have create these 2 files. Called main.c and atr.c. They seem towork
pretty well. I just wanted to submit them to see what if any errors others
that know more might find. Thanks.

atr.c

#include <stdio.h>

double
atr (double th, double tl, double wc)
{
* if (th - tl th - wc && tl - wc < th - tl)
* * {
* * * return th - tl;
* * }
* if (th - wc th - tl && tl - wc < th - wc)
* * {
* * * return th - wc;
* * }
* if (tl - wc th - wc && th - tl < tl - wc)
* * {
* * * return tl - wc;
* * }

}

main.c

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

extern double atr ( double th, double tl, double wc );

int
main (int argc, char *argv[])
{
* if (argc != 4)
* * {
* * * fprintf (stderr, "usage error th, tl, wc\n");
* * * exit (EXIT_FAILURE);
* * }
* double th, tl, wc;
* th = strtod (argv[1], NULL);
* tl = strtod (argv[2], NULL);
* wc = strtod (argv[3], NULL);
* printf("%.2f\n" , atr(th, tl, wc));

}
Having fixed some of the more obvious errors, I would try this one:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

static double atr(double th, double tl, double wc)
{
if (th - tl th - wc && tl - wc < th - tl) {
return th - tl;
}
if (th - wc th - tl && tl - wc < th - wc) {
return th - wc;
}
if (tl - wc th - wc && th - tl < tl - wc) {
return tl - wc;
}
assert(0);
return sqrt(-1.);
}
int main(int argc, char *argv[])
{
double th,
tl,
wc;
if (argc != 4) {
fprintf(stderr, "usage error th, tl, wc\n");
exit(EXIT_FAILU RE);
}
th = strtod(argv[1], NULL);
tl = strtod(argv[2], NULL);
wc = strtod(argv[3], NULL);
printf("%.2f\n" , atr(th, tl, wc));
return 0;
}

But then give it inputs of 20 20 20 and see what happens.
Jun 27 '08 #4
In article <75************ *************** *******@x1g2000 prh.googlegroup s.com>,
Fred <fr************ *****@boeing.co mwrote:
>Just a bit of math shows that there are cleaner comparisons.
For example,
if ( th - tl th - wc )
is the same as
if ( wc tl )
Alegebraically the same, but not the same in the domain of
binary floating point numbers... especially if th is a NaN.
--
"To the modern spirt nothing is, or can be rightly known,
except relatively and under conditions." -- Walter Pater
Jun 27 '08 #5

"user923005 " <dc*****@connx. comwrote in message
news:7e******** *************** ***********@2g2 000hsn.googlegr oups.com...

Having fixed some of the more obvious errors, I would try this one:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

static double atr(double th, double tl, double wc)
{
if (th - tl th - wc && tl - wc < th - tl) {
return th - tl;
}
if (th - wc th - tl && tl - wc < th - wc) {
return th - wc;
}
if (tl - wc th - wc && th - tl < tl - wc) {
return tl - wc;
}
assert(0);
return sqrt(-1.);
}

Assert. Hum never used it. I'll check it out.

Bill
Jun 27 '08 #6

"Fred" <fr************ *****@boeing.co mwrote in message
news:759c09a0-b2fe-4ed3-8073-

You haven't said what the intent of the atr functio
n is, so how are
we to know whether it is getting the right answer?

Ceck my second post. Any answer that is a postive number might be right.

Just a bit of math shows that there are cleaner comparisons.
For example,

if ( th - tl th - wc )
is the same as
if ( wc tl )

--
Fred Kleinschmidt
Jun 27 '08 #7

"user923005 " <dc*****@connx. comwrote in message
news:7ee08bd4-890e-434f-8bdd-

[snip]

assert(0);
return sqrt(-1.);
}

What's the sqrt function called for ? And the 0 passed to assert?

Bill
Jun 27 '08 #8

"Fred" <fr************ *****@boeing.co mwrote in message
news:759c09a0-b2fe-4ed3-8073-

You haven't said what the intent of the atr function is, so how are
we to know whether it is getting the right answer?

Just a bit of math shows that there are cleaner comparisons.
For example,

if ( th - tl th - wc )
is the same as
if ( wc tl )

http://www.meta-formula.com/average-true-range.html

Jun 27 '08 #9
Keith Thompson wrote:
In your last few messages, quoted text is not marked. In the above,
the text starting with "Having fixed some ..." plus the C code is
quoted from user923005's previous article; only the last line,
starting "Assert. Hum ..." is new.

Whatever you're doing differently when posting followups, please go
back to what you were doing before.

Probably Bill is using Outlook Express (I have him killfiled, so I
can't check), and is running into the incompatibility with Google
Groups, which Mr. Corbit is using. If so, that can be fixed either by
switching newsreaders (recommended) or downloading and installing OE
Quotefix.


Brian
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
742
by: Steve | last post by:
Hi all How would I find out the average date when given a bunch of dates? For example, I want to find the average length in time from the following dates: ---------------------------------------------------- Start Date End Date 01/01/2004 12:50pm 02/01/2004 18:40pm 02/01/2004 13:40pm 02/01/2004 13:57pm 02/01/2004 14:30pm 02/01/2004 19:50pm
1
21174
by: HEMH6 | last post by:
Weihted Average Write a C program to calculate and print the weighted average of a list of N floating point number, using the formula Xave = F1X1 + F2X2+...+ FnXn where the F's are fractional weighting factors, i.e. 0<=F1<1, and F1+F2+...+Fn=1
2
13566
by: chrisale | last post by:
Hi All, I've been racking my brain trying to figure out some sort of Sub-Select mySQL statement that will create a result with multiple rows of averaged values over a years time. What I have is weather data. There is a new record every 5 minutes, every day. So. What I want to do with one SQL statement is figure out the Average of those 5 minute records over each day, for every day of the year.
40
2743
by: nufuhsus | last post by:
Hello all, First let me appologise if this has been answered but I could not find an acurate answer to this interesting problem. If the following is true: C:\Python25\rg.py>python Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) on win32 Type "help", "copyright", "credits" or "license" for more
1
3539
by: wetsprockets | last post by:
Hello, I have two tables, WindVector (wind data) and DOH_FC88to07 (water quality samples). The WindVector table has the following fields: Date (short date) Time (short time) WindSpeed WindDir WindDirRads V
5
4599
by: jamesnkk | last post by:
I want to calculate the average price, I have a cost price table- tblcostPrice as below,Same part no can be purchase from different supplier at different cost. In my tblcostPrice I have 2 suppliers for the same part at different cost Part No - KN-12345 Product ID....SUPPLIER......Qty......Cost price A0123.........AAA CO.......1000..... 0.90 A0123.........AAA CO...... 5000..... 0.70 A0123.........AAA CO.......9000..... 0.55
13
4910
by: vaneric | last post by:
hi i have a set of RGB images of diff faces (of people )as a 2 dim numpyarray ...something like threefaces=array(, , ]) where xa1,xa2,xa3 are tuples each representing rgb values of a pixel of first image ..
0
1247
by: JuAn2226 | last post by:
hi this my code Private Sub Form_Load() car_count = 0 Cumulative_Speed = 0 End Sub Private Sub Timer1_Timer() Dim tmpNumber As Integer
5
2708
by: adarshyam | last post by:
Hi friends, I have an interesting problem in vb.net. And I am struggling to get a solution for this..m trying for the past 3days.. It’s to calculate moving average for the inputs given by the user.. The following are the steps.. 1, get 2user inputs in textbox(1st input is number is periods, 2nd is moving range) 2, after getting both the user inputs, the user will click on an input button which must dynamically generate rows for the number...
0
9730
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10706
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9482
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7056
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5719
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5911
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4528
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4119
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3164
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.