Unix questions - Sign of a number in C?

This is Interesting: Free IT Magazines  
Home > Archive > Unix questions > February 2004 > Sign of a number in C?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Sign of a number in C?
Gregory L. Hansen

2004-02-20, 4:34 am


I need to find the sign of a number in C. Maybe my reference was just
bad. Is there an easier (less computationally intensive) way than
V/fabs(V)?


--
"The average person, during a single day, deposits in his or her underwear
an amount of fecal bacteria equal to the weight of a quarter of a peanut."
-- Dr. Robert Buckman, Human Wildlife, p119.
Tim Haynes

2004-02-20, 4:34 am

glhansen@steel.ucs.indiana.edu (Gregory L. Hansen) writes:

> I need to find the sign of a number in C. Maybe my reference was just
> bad. Is there an easier (less computationally intensive) way than
> V/fabs(V)?


That's a bad plan if V==0 (or 0.0).

Why not simply

#define signum(x) = (x<0?-1:1)

or an obvious variation that takes 0 into account?

~Tim
--
no se encuentra el sistema operativo |piglet@stirfried.vegetable.org.uk
(seen mid-windows 98 installation) |http://spodzone.org.uk/
Gregory L. Hansen

2004-02-20, 5:34 am

In article <86u11l1w3v.fsf@potato.vegetable.org.uk>,
Tim Haynes <news-reply{at}stirfried.vegetable.org.uk> wrote:
>glhansen@steel.ucs.indiana.edu (Gregory L. Hansen) writes:
>
>
>That's a bad plan if V==0 (or 0.0).
>
>Why not simply
>
> #define signum(x) = (x<0?-1:1)
>
>or an obvious variation that takes 0 into account?


Mainly because I usually don't use that construct, so it didn't occur to
me. I suppose some sort of if-then is needed, then.


--
"The main, if not the only, function of the word aether has been to
furnish a nominative case to the verb 'to undulate'."
-- the Earl of Salisbury, 1894
Erik Max Francis

2004-02-20, 12:33 pm

"Gregory L. Hansen" wrote:

> I need to find the sign of a number in C. Maybe my reference was just
> bad. Is there an easier (less computationally intensive) way than
> V/fabs(V)?


#define sgn(x) ((x) > 0 ? +1 : (x) < 0 ? -1 : 0)

--
__ Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ There is _never_ no hope left. Remember.
-- Louis Wu
Gregory L. Hansen

2004-02-21, 12:33 am

In article <4036B2B8.302F3738@alcyone.com>,
Erik Max Francis <max@alcyone.com> wrote:
>"Gregory L. Hansen" wrote:
>
>
>#define sgn(x) ((x) > 0 ? +1 : (x) < 0 ? -1 : 0)


For some reason, the ()?x:y construct has always made me nervous, I'd
never used it to do anything real, and it didn't occur to me at all. But
it seems to work. I only needed it once, so I skipped the #define part.
But I felt sure that a function like that would have been included in the
math library.

--
"When the fool walks through the street, in his lack of understanding he
calls everything foolish." -- Ecclesiastes 10:3, New American Bible
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com