Sign of a number in C?
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix questions > Sign of a number in C?




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Sign of a number in C?  
Gregory L. Hansen


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-20-04 09: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.





[ Post a follow-up to this message ]



    Re: Sign of a number in C?  
Tim Haynes


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-20-04 09: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/





[ Post a follow-up to this message ]



    Re: Sign of a number in C?  
Gregory L. Hansen


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-20-04 10: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





[ Post a follow-up to this message ]



    Re: Sign of a number in C?  
Erik Max Francis


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-20-04 05: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





[ Post a follow-up to this message ]



    Re: Sign of a number in C?  
Gregory L. Hansen


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-21-04 05: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





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 04:34 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register