468,733 Members | 2,210 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,733 developers. It's quick & easy.

Re: abs question

Bill Cunningham wrote:
..... 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.hif 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.
Oct 4 '08 #1
5 1695

"Tim Prince" <tp*****@nospamcomputer.orgwrote in message
news:Vt***************@flpi147.ffdc.sbc.com...
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

Oct 4 '08 #2
Bill Cunningham wrote:
"Tim Prince" <tp*****@nospamcomputer.orgwrote in message
news:Vt***************@flpi147.ffdc.sbc.com...
>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.
Nothing wrong with using it there, provided that you #include <math.hor
<tgmath.h>. Turn on the warnings of your compiler to catch missing
headers. If you don't understand the warnings, quote them when asking
questions.
Oct 7 '08 #3
On 4 Oct, 03:09, "Bill Cunningham" <nos...@nspam.invalidwrote:
"Tim Prince" <tpri...@nospamcomputer.orgwrote in message

news:Vt***************@flpi147.ffdc.sbc.com...
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.
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...

--
Nick Keighley


Oct 7 '08 #4
Nick Keighley said:

<snip>
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).
It's a double.

And *now* I'll look it up.

4.5.6.2 The fabs function

Synopsis

#include <math.h>
double fabs(double x);

Description

The fabs function computes the absolute value of a floating-point
number x .

Returns

The fabs function returns the absolute value of x.

I love being right. :-)

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 7 '08 #5
In article <b3**********************************@75g2000hso.g ooglegroups.com>,
Nick Keighley <ni******************@hotmail.comwrote:
>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
--
Please remember to mention me / in tapes you leave behind.
Oct 7 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Mohammed Mazid | last post: by
3 posts views Thread by Stevey | last post: by
10 posts views Thread by glenn | last post: by
53 posts views Thread by Jeff | last post: by
56 posts views Thread by spibou | last post: by
2 posts views Thread by Allan Ebdrup | last post: by
3 posts views Thread by Zhang Weiwu | last post: by
1 post views Thread by CARIGAR | last post: by
xarzu
2 posts views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.