Thread: Problem with abs(double)

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    10

    Problem with abs(double)

    It's very basic but I have problem getting the absolute value of a double type; here:

    Code:
    double *Dist_cb1= new double[2400];
    for (i=0; i<2400; i++){
    		Dist_cb1[i]=0;
    		for (j=0; j<6; j++)
    			
    			Dist_cb1[i] = Dist_cb1[i] + abs(abs(Dataf[j] - train1[i][j])); printf("%f        ", Dist_cb1[i]);
    			
    	}
    when I try to get the absolute value of the subtraction of two doubles I receive a large value which should be an address.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    You want to be using 'fabs' here.
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I don't know if you're doing it deliberately, but putting that printf statement on the same line as the assignment to Dist_cb1[i] would lead people to think that it will happen every time through the loop, when it won't.

    There's no point in doing abs() twice I wouldn't think either. Note that abs is overloaded to work with floats and doubles and things if you use std::abs instead of just abs.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by tabstop View Post
    There's no point in doing abs() twice I wouldn't think either. Note that abs is overloaded to work with floats and doubles and things if you use std::abs instead of just abs.
    ..... and you #include <cmath> (at least, for the float/double overloading).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

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