Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Standardne funkcije u C-u

[es] :: C/C++ programiranje :: C/C++ za početnike :: Standardne funkcije u C-u

[ Pregleda: 2843 | Odgovora: 10 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

fresh.bm

Član broj: 162156
Poruke: 182
*.broadband.blic.net.



+1 Profil

icon Standardne funkcije u C-u13.10.2008. u 13:33 - pre 188 meseci
Interesuje me da li postoji neka funkcija za odredjivanje znaka int-a u standardnim C bibliotekama?
 
Odgovor na temu

Aleksandar Ružičić
Software Architect, Appricot d.o.o.
Beograd

Član broj: 26939
Poruke: 2881

Jabber: krckoorascic@gmail.com
Sajt: krcko.net


+44 Profil

icon Re: Standardne funkcije u C-u13.10.2008. u 14:20 - pre 188 meseci
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

 
Odgovor na temu

fresh.bm

Član broj: 162156
Poruke: 182
*.broadband.blic.net.



+1 Profil

icon Re: Standardne funkcije u C-u13.10.2008. u 15:19 - pre 188 meseci
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?
 
Odgovor na temu

Aleksandar Ružičić
Software Architect, Appricot d.o.o.
Beograd

Član broj: 26939
Poruke: 2881

Jabber: krckoorascic@gmail.com
Sajt: krcko.net


+44 Profil

icon Re: Standardne funkcije u C-u13.10.2008. u 16:36 - pre 188 meseci
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);
}

 
Odgovor na temu

fresh.bm

Član broj: 162156
Poruke: 182
*.broadband.blic.net.



+1 Profil

icon Re: Standardne funkcije u C-u13.10.2008. u 16:56 - pre 188 meseci
Jos me interesuje koja od ove dve varijante trosi manje racunarskih resursa?

# novo pitanje:

Da li postoji standardna funkcija za apsolutnu vrijednost?
 
Odgovor na temu

X Files
Vladimir Stefanovic
Pozarevac

SuperModerator
Član broj: 15100
Poruke: 4902
*.dynamic.sbb.rs.

Jabber: xfiles@elitesecurity.org


+638 Profil

icon Re: Standardne funkcije u C-u13.10.2008. u 17:30 - pre 188 meseci
^
Trebalo bi da postoji int abs(int x) u stdlib.h ili math.h.
 
Odgovor na temu

fresh.bm

Član broj: 162156
Poruke: 182
*.broadband.blic.net.



+1 Profil

icon Re: Standardne funkcije u C-u13.10.2008. u 18:03 - pre 188 meseci
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.
 
Odgovor na temu

X Files
Vladimir Stefanovic
Pozarevac

SuperModerator
Član broj: 15100
Poruke: 4902
*.dynamic.sbb.rs.

Jabber: xfiles@elitesecurity.org


+638 Profil

icon Re: Standardne funkcije u C-u13.10.2008. u 18:47 - pre 188 meseci
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);
}

 
Odgovor na temu

fresh.bm

Član broj: 162156
Poruke: 182
*.broadband.blic.net.



+1 Profil

icon Re: Standardne funkcije u C-u13.10.2008. u 19:18 - pre 188 meseci
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.
 
Odgovor na temu

X Files
Vladimir Stefanovic
Pozarevac

SuperModerator
Član broj: 15100
Poruke: 4902
*.dynamic.sbb.rs.

Jabber: xfiles@elitesecurity.org


+638 Profil

icon Re: Standardne funkcije u C-u13.10.2008. u 19:26 - pre 188 meseci
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.
 
Odgovor na temu

Aleksandar Ružičić
Software Architect, Appricot d.o.o.
Beograd

Član broj: 26939
Poruke: 2881

Jabber: krckoorascic@gmail.com
Sajt: krcko.net


+44 Profil

icon Re: Standardne funkcije u C-u13.10.2008. u 19:59 - pre 188 meseci
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...
 
Odgovor na temu

[es] :: C/C++ programiranje :: C/C++ za početnike :: Standardne funkcije u C-u

[ Pregleda: 2843 | Odgovora: 10 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.