Thread: SQRT Problem.

  1. #1
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184

    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
    79
    sqrt takes a double as argument, not long.
    Code:
    ld_raiz = sqrt((double) total);

  3. #3
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184
    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
    28,413
    Post the smallest and simplest compilable program that demonstrates the problem. Also, post your input, expected output and actual output.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,909
    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
    Location
    Santa Catarina - Brasil
    Posts
    184
    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
    28,413
    What is the error? Are you sure you are compiling as C?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Bit Fiddler
    Join Date
    Sep 2009
    Posts
    79
    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
    Location
    Santa Catarina - Brasil
    Posts
    184
    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
    Location
    Santa Catarina - Brasil
    Posts
    184
    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
    79
    Code:
    printf("%lf", sqrt(2))
    ...doesn't give you the right answer?

  13. #13
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184
    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, 04: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, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM