[ fresh.bm @ 13.10.2008. 13:33 ] @
Interesuje me da li postoji neka funkcija za odredjivanje znaka int-a u standardnim C bibliotekama?
[ Aleksandar Ružičić @ 13.10.2008. 14:20 ] @
koliko ja znam nema, ali mozes ovaj makro da koristis:
Code:

#define sgn(x) (((x) > 0) - ((x) < 0))

Code:

printf( "%d", sgn(123) );    // 1
printf( "%d", sgn(0) );    // 0
printf( "%d", sgn(-123) );    // -1
[ fresh.bm @ 13.10.2008. 15:19 ] @
Interesovalo me da li postoji standardna funkcija za to.

Ja sam to uradio na ovaj nacin:

Code:
 
int sign(int input) 
{
   return (-(int)((unsigned int)((int) input) >> (sizeof(int) * CHAR_BIT - 1));


vraca 0, ako je pozitivan broj ili nula
vraca -1, ako je negativan;

Sta je po Vasem misljenju bolje?
[ Aleksandar Ružičić @ 13.10.2008. 16:36 ] @
pa meni vise odgovara standardan izlaz sgn fje, +1 ako je broj pozitivan, 0 ako je 0 i -1 ako je negativan (uostalom i matematicka sgn fja "radi" tako)

a ako bas hoces fju umesto makroa:
Code:

int sgn(const int x)
{
   return (x > 0) - (x < 0);
}
[ fresh.bm @ 13.10.2008. 16:56 ] @
Jos me interesuje koja od ove dve varijante trosi manje racunarskih resursa?

# novo pitanje:

Da li postoji standardna funkcija za apsolutnu vrijednost?
[ X Files @ 13.10.2008. 17:30 ] @
^
Trebalo bi da postoji int abs(int x) u stdlib.h ili math.h.
[ fresh.bm @ 13.10.2008. 18:03 ] @
Da, postoji u stdlib.h
Kako bi mogao vidjeti kod tj. kako ta funkcija radi?
Pokusao sam je naci u tom header fajlu ali nema koda.
[ X Files @ 13.10.2008. 18:47 ] @
Sad grepovah jedan Borland C++ Builder 6 folder:
Code:

/*------------------------------------------------------------------------
 * filename - abs.c
 *
 * function(s)
 *        abs - absolute value
 *-----------------------------------------------------------------------*/

/*
 *      C/C++ Run Time Library - Version 11.0
 *
 *      Copyright (c) 1987, 2002 by Borland Software Corporation
 *      All Rights Reserved.
 *
 */

/* $Revision: 9.4.2.1 $        */

#include <stdlib.h>

#undef abs              /* abs is defined as a macro in stdlib.h */

/*------------------------------------------------------------------------*

Name            abs - absolute value

Usage           int abs(int i);

Prototype in    stdlib.h

Description     abs returns the absolute value of the integer argument i

Return value    Integer value in  the range 0 to 32767,  with the exception
                that an argument of -32768 is returned as -32768.
                
*--------------------------------------------------------------------------*/

int _RTLENTRYF _EXPFUNC abs(int i)
{
    return (i >= 0 ? i : -i);
}

[ fresh.bm @ 13.10.2008. 19:18 ] @
Citat:
Code:
int _RTLENTRYF _EXPFUNC abs(int i)
{
    return (i >= 0 ? i : -i);
}


Code:

int abs(int input) {
      return (+1|(input >> (sizeof(int) * CHAR_BIT - 1)))*input;
}


Koja je funkcija preporucljivija od ove dve za implementaciju? Rade isto.
[ X Files @ 13.10.2008. 19:26 ] @
Pojma nemam, a mrzi me da sad analiziram. Verujem da ova slozenija handle-uje sve vrednosti int-a. Uvek su postojali problemi kod granicnih vrednosti, pogotovo kod najvece negativne vrednosti.
[ Aleksandar Ružičić @ 13.10.2008. 19:59 ] @
vidim da uzimas kodove sa bit twiddling hacks, gde izmedju ostalog stoji:
Citat:

All operations are assumed to take the same amount of time, which is not true in reality, but CPUs have been heading increasingly in this direction over time. There are many nuances that determine how fast a system will run a given sample of code, such as cache sizes, memory bandwidths, instruction sets, etc. In the end, benchmarking is the best way to determine whether one method is really faster than another, so consider the techniques below as possibilities to test on your target architecture.

znaci napravi neki benchmark i vidi koja je brza/efikasnija, mada to uglavnom nije problem u danasnje vreme i ne treba se zamarati time...