COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Sign of a number in C?

• Email
• Follow

```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.
```
 0
Reply glhansen (662) 2/20/2004 5:31:58 PM

See related articles to this posting

```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/
```
 0
Reply 2/20/2004 5:47:32 PM

```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:
>
>> 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?

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
```
 0
Reply glhansen (662) 2/20/2004 6:15:50 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
```
 0
Reply max78 (1220) 2/21/2004 1:22:00 AM

```In article <4036B2B8.302F3738@alcyone.com>,
Erik Max Francis  <max@alcyone.com> wrote:
>"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)

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
```
 0
Reply glhansen (662) 2/21/2004 2:04:50 PM

4 Replies
28 Views

Similar Articles

12/9/2013 10:03:08 PM
[PageSpeed]

 Reply:

Similar Artilces:

format number...
Hi, Here is my following script : #!/bin/bash jour=01 JOUR=10 while ((\$jour <= \$JOUR)) do echo "Bonjour(\$jour)" let jour+=1 jour=`echo \$jour | awk ' { printf("%02d",\$1) }'` done When I execute the script, have the following message : Bonjour(01) Bonjour(02) Bonjour(03) Bonjour(04) Bonjour(05) Bonjour(06) Bonjour(07) ../steph: line 6: ((: 08: value too great for base (error token is "08") Any idea? Regards Steph On 2004-05-12, Stephane wrote: > Hi, > > Here is my following script : > >

Number decomposition
precision floating-point number to two hundred fifty-eight and two tenths. If you're willing to accept the approximation, SPRINTF the number using an appropriate format string then delete the '.' character from the string. s = sprintf('%3.1f', 258.2) s(s=='.') = [] abcd = s-'0' -- Steve Lord slord@mathworks.com comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ To contact Technical Support use the Contact Us link on http://www.mathworks.com "Steven Lord" <slord@mathworks.com> wrote in message <hu5qfm\$g4h\$1@fred.mathworks.com>... > > "Benoit " <igneben@gmail.com> wrote in message > news:hu5pva\$b2a\$1@fred.mathworks.com... > > Hello, > > > > Is there a function is MATLAB that decomposes a numbers as follows: > > > > [a b c d] = function(258.2) > > > > where a = 2; b = 5; c = 8; and d = 2? > > Be careful ... 258.2 is not exactly two hundred fifty-eight and two tenths. > It's the closest IEEE double precision floating-point number to two hundred > fifty-eight and two tenths. >

Page number
Hi, SAS assigns page numbers on the top corner of the page. But I am interested in putting Page numbers at the bottom of the report. How can I do that? I would appreciate your response. Thank you! -Ran S

format number...
Hi, Here is my following script : #!/bin/bash jour=01 JOUR=10 while ((\$jour <= \$JOUR)) do echo "Bonjour(\$jour)" let jour+=1 jour=`echo \$jour | awk ' { printf("%02d",\$1) }'` done When I execute the script, have the following message : Bonjour(01) Bonjour(02) Bonjour(03) Bonjour(04) Bonjour(05) Bonjour(06) Bonjour(07) ../steph: line 6: ((: 08: value too great for base (error token is "08") Any idea? Regards Steph Hi Stephane, This is an issue with bash, not awk. From the bash manual page: "Constants with a leading 0 are interpreted as octal numbers" So when it says value to great for base this means that 08 is not a valid number in octal. You'll probably get a fuller reply on comp.unix.shell HTH, tristan. Stephane wrote: > Hi, > > Here is my following script : > > #!/bin/bash > > jour=01 > JOUR=10 > > while ((\$jour <= \$JOUR)) > > do > > echo "Bonjour(\$jour)" > > let jour+=1 > jour=`echo \$jour | awk ' { printf("%02d",\$1) }'` > > done

consecutive number
Hi, I have a pdf form that has an invoice number I would like to make the number in the text field add (1) each time the document open and save it, so that the next time the document is opened it will display a consecutive number. Example: 9078, 9079, 9080, 9081...etc. I appreciate your help.

calculate number
(130/width*height)+10"/> > > How do i save the result from this calculation in my variable? > > height and width im my xml contains the height and width of an image. > > Frederik From a current project: <xsl:value-of select="format-number(translate((((\$upris - \$ipris) * 100) div \$upris),',','.'), '#.##0,0', 'european' )"/> As you can see, your variable best sport a '\$' or they're not variables ;-)

biggest number
I'm learning ruby, and I want to know if it is any way to know the biggest number to be represented with a specific type of number (Fixnum, Bignum, Float). Thank you. -- Posted via http://www.ruby-forum.com/. Javier Esteve wrote: > I'm learning ruby, and I want to know if it is any way to know the > biggest number to be represented with a specific type of number (Fixnum, > Bignum, Float). Float is a standard 64-bit IEEE floating point value. There are several constants on the Float class that can tell you whatever you need: ➔ jruby -e "p Float::constants"... if it is any way to know the > biggest number to be represented with a specific type of number (Fixnum, > Bignum, Float). > > Thank you. irb(main):005:0> (2**30).class => Bignum irb(main):006:0> (2**30-1).class => Fixnum irb(main):009:0> RUBY_VERSION => "1.8.6" -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Record Number
Whenever you use a continous form each row represents a record. Does this record have a number? If so how do you access it. Right now I have a field using auto number. But if access generates its own number for that row I'd like to use that. Thanks DS Me.CurrentRecord will give you the same number you see in the navigation buttons at the bottom left of the screen. In a continous form, that number will be for whichever record is the current record. -- PC Datasheet Your Resource For Help With Access, Excel And Word Applications resource@pcdatasheet.com www.pcdatasheet.com "DS" <bootybox@optonline.net> wrote in message news:fe4Rd.31\$Nh3.20@fe09.lga... > Whenever you use a continous form each row represents a record. Does > this record have a number? If so how do you access it. Right now I > have a field using auto number. But if access generates its own number > for that row I'd like to use that. > Thanks > DS PC Datasheet wrote: > Me.CurrentRecord will give you the same number you see in the navigation > buttons

Adding a positive number and a negative number
In two=E2=80=99s complement representation, can adding one positive and one= negative give you overflow?

How to print a 32-bit number as a base-10 number
Hi, Assuming that "division" is a very costly operation (except by any multiples of 2), whats the fastest way of printing a 32-bit number as a base-10 unsigned integer. Thanks Steve On 13 Dec 2004 04:15:11 -0800 "Steve" <steven_mark_99@yahoo.com> wrote: > Assuming that "division" is a very costly operation (except by any > multiples of 2), whats the fastest way of printing a 32-bit number as > a base-10 unsigned integer. puts("a 32-bit number as a base-10 unsigned integer."); The C language does not define the relative speed... read it. Steve wrote: > Hi, > > Assuming that "division" is a very costly operation (except by any > multiples of 2), Why do you say that? Have you looked at the time complexity of integer division/multiplication? > whats the fastest way of printing a 32-bit number as a > base-10 unsigned integer. > > Thanks > Steve printf("%d",number); gtoomey "Gregory Toomey" <nospam@bigpond.com> wrote in message news:325n41F3ita88U1@individual.net... > Steve wrote: > > > Hi, > > > > Assuming that "