By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,900 Members | 1,353 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,900 IT Pros & Developers. It's quick & easy.

Re: abs question

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.