SQRT Problem.

This is a discussion on SQRT Problem. within the C Programming forums, part of the General Programming Boards category; Code: long DesvioPadrao(long pesos[100], u16_t tamanho) { double ld_raiz; long total = 0; long media; long ll_resultado; int iCont; char ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    158

    SQRT Problem.

    Code:
    long DesvioPadrao(long pesos[100], u16_t tamanho)
    {
    	double ld_raiz;
    	long total = 0;
    	long media;
    	long ll_resultado;
    	int iCont;
    
    	char strTexto[100];
    
    	for(iCont=0; iCont<tamanho; iCont++)
    		total += pesos[iCont];
    
    	media = total / tamanho;
    	total = 0;
    
    	for(iCont=0; iCont<tamanho; iCont++)
    	{
    		ll_resultado = pesos[iCont] - media;
    		total = total + (ll_resultado * ll_resultado);
    	}
    
    	total = (long) total / (tamanho - 1);
    
           /*
           here total = 10
           */
    	ld_raiz = sqrt(total);
    
    	total = (long)ld_raiz;
    	return total;
    }
    My function return 528062108, when i call SQRT function passing value 10 like argument, and cast (or no cast) to long.

  2. #2
    Bit Fiddler
    Join Date
    Sep 2009
    Posts
    67
    sqrt takes a double as argument, not long.
    Code:
    ld_raiz = sqrt((double) total);

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    158
    Quote Originally Posted by Fader_Berg View Post
    sqrt takes a double as argument, not long.
    Code:
    ld_raiz = sqrt((double) total);
    The problems continue..

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Post the smallest and simplest compilable program that demonstrates the problem. Also, post your input, expected output and actual output.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Quote Originally Posted by Fader_Berg View Post
    sqrt takes a double as argument, not long.
    Code:
    ld_raiz = sqrt((double) total);
    Type promotion should happily convert the long value to a double before making the call, so nothing to worry about there.

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    158
    Maybe, my enviroment get an error...
    a simple sqrt(2); showed in "%d" on a printf get same error.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    What is the error? Are you sure you are compiling as C?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Bit Fiddler
    Join Date
    Sep 2009
    Posts
    67
    Quote Originally Posted by sergioms View Post
    Maybe, my enviroment get an error...
    a simple sqrt(2); showed in "%d" on a printf get same error.
    You can't print doubles with %d [printf("%d", sqrt(2))], you must use %lf.

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    158
    Yes, i'm compiling by GCC ARM using Yargato lib.
    YAGARTO - Yet another GNU ARM toolchain

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    QUOTE=Fader_Berg; You can't print doubles with %d [printf("%d", sqrt(2))], you must use %lf.

  11. #11
    Registered User
    Join Date
    Nov 2008
    Posts
    158
    Quote Originally Posted by Adak View Post
    QUOTE=Fader_Berg; You can't print doubles with %d [printf("%d", sqrt(2))], you must use %lf.
    erros continue...

  12. #12
    Bit Fiddler
    Join Date
    Sep 2009
    Posts
    67
    Code:
    printf("%lf", sqrt(2))
    ...doesn't give you the right answer?

  13. #13
    Registered User
    Join Date
    Nov 2008
    Posts
    158
    Code:
    unsigned long isqrt(const unsigned long n);
    
    /*-- isqrt -----------------------------------------------------------------*/
    
    unsigned long isqrt(unsigned long n) {
      unsigned long s, t;
    
    #define sqrtBit(k) \
      t = s+(1UL<<(k-1)); t <<= k+1; if (n >= t) { n -= t; s |= 1UL<<k; }
    
      s = 0UL;
    #ifdef __alpha
      if (n >= 1UL<<62) { n -= 1UL<<62; s = 1UL<<31; }
      sqrtBit(30); sqrtBit(29); sqrtBit(28); sqrtBit(27); sqrtBit(26);
      sqrtBit(25); sqrtBit(24); sqrtBit(23); sqrtBit(22); sqrtBit(21);
      sqrtBit(20); sqrtBit(19); sqrtBit(18); sqrtBit(17); sqrtBit(16);
      sqrtBit(15);
    #else
      if (n >= 1UL<<30) { n -= 1UL<<30; s = 1UL<<15; }
    #endif
      sqrtBit(14); sqrtBit(13); sqrtBit(12); sqrtBit(11); sqrtBit(10);
      sqrtBit(9); sqrtBit(8); sqrtBit(7); sqrtBit(6); sqrtBit(5);
      sqrtBit(4); sqrtBit(3); sqrtBit(2); sqrtBit(1);
      if (n > s<<1) s |= 1UL;
    
    #undef sqrtBit
    
      return s;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21