Thread: Double and Floats

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    204

    Double and Floats

    Whats the deal with using floats and double in same mathematical expressions?
    I have the foloowing function, and I just noticed that three parameters, eta phi and omega_2 are floats, the rest are doubles. Is this going to change anything, because at the moment my code doesnt do what I expect of it, but I cant see another issue, however I know that "curve_to_fit" is calculating incorrectly.
    Code:
    static double function2(int n, double y[])
    
    double curve_to_fit[23],curve_to_fit_sum_abs = 0, c1[23], x_coeff_2[23], v1[23], v2[23];
    float eta = y[0]; //parameters
    float phi=y[1]; //parameters
    float omega_2=y[2]; //parameters
    
    int i;
    
    
    for (i = 0; i<=22; i++)
    {
        v1[i] = (exp(a_global*2*pi)-cosh(a_global*b_global*init_T[i][0]));
        v2[i] = sinh(a_global*b_global*init_T[i][0]);
        x_coeff_2[i] = v1[i]/v2[i];
        c1[i]= b_global*b_global*(x_coeff_2[i]*x_coeff_2[i]-1);
        curve_to_fit[i] = c1[i]*exp(-2*a_global*init_T[i][1])+eta*((1+0.5*(4*a_global*a_global+1))*cos(init_T[i][1]+phi)-2*a_global*sin(init_T[i][1]+phi))+b_global*b_global-omega_2;
    }
    for (i = 0; i<=22; i++)
    {
        curve_to_fit_sum_abs = curve_to_fit_sum_abs + abs(curve_to_fit[i]);
      printf("i=%d, c1[i] = %f, x_coeff_2[i] = %f, curve_to_fit[i] = %f\n",i, c1[i],x_coeff_2[i],curve_to_fit[i]);  //  printf("curve_to_fit = %f, curve_to_fit_sum_abs = %f\n",curve_to_fit[i], curve_to_fit_sum_abs);
    }
    
        return curve_to_fit_sum_abs;
    
    }
    Cheers

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by a.mlw.walker
    I have the foloowing function, and I just noticed that three parameters, eta phi and omega_2 are floats, the rest are doubles. Is this going to change anything, because at the moment my code doesnt do what I expect of it, but I cant see another issue:
    It might since there may be a loss of information due to a loss of precision as the elements of y are doubles. In the actual expressions themselves this may or may not be a problem since the floats will be promoted to double anyway.
    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

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    You may want to peruse through the IEE 754 Reference Manual. Additionally, on this page you will find a link to the article What Every Computer Scientist Should Know about Floating-Point Arithmetic.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    204
    Have you ever tried to replicate matlab code in C? The above code is supposed to be my translation of matlab code, however its not producing the same x_coeff_2 value: Its strange because up to that point it is correct, and then that line is quite straight forward. The matlab code is: here T0 is column 0 of ini_T, and theta_f is the second column of init_T (above)
    Code:
    eta = params2(1);
    phi = params2(2);
    wf2 = params2(3);
    x = (exp(a*2*pi)-cosh(a*b*T0))./sinh(a*b*T0)
    c1 = b^2*(x.^2-1)
    f1 = c1.*exp(-2*a*theta_f)+eta*((1+0.5*(4*a^2+1))*cos(theta_f+phi)-2*a*sin(theta_f+phi))+b^2-wf2
    f2 = abs(f1);
    f = sum(f2);
    The point is the '.' operator in matlab.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do it with a calculator. Which numbers do you believe?

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Right. The '.' operator in matlab (IIRC) tells it to do the multiplication/division element-by-element, instead of using regular matrix multiplication. It would help immensely if you made your matlab and C variable names exactly the same. Your problem (I think), is one of order of operations:
    Code:
    v1[i] = (exp(a_global*2*pi)-cosh(a_global*b_global*init_T[i][0]));
    v2[i] = sinh(a_global*b_global*init_T[i][0]);
    x_coeff_2[i] = v1[i]/v2[i];
    vs
    Code:
    x = (exp(a*2*pi)-cosh(a*b*T0))./sinh(a*b*T0)
    Notice the difference in order of operations. The C code subtracts cosh() from exp() before dividing by sinh, whereas the matlab code divides cosh() by sinh() before subtracting that from exp(). Try something like:
    Code:
    #define SIZE 23  // use named constants instead of magic numbers
    double temp[SIZE];  
    
    for (i = 0; i < SIZE; i++) {  // note the more conventional for loop construct here with < instead of <=
        v1[i] = cosh(a_global*b_global*init_T[i][0])) / sinh(a_global*b_global*init_T[i][0]);
        x_coeff_2[i] - exp(a_global*2*pi) - temp[i]
        c1[i]= b_global*b_global*(x_coeff_2[i]*x_coeff_2[i]-1);
        ...
    }

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by anduril462 View Post
    Notice the difference in order of operations. The C code subtracts cosh() from exp() before dividing by sinh, whereas the matlab code divides cosh() by sinh() before subtracting that from exp().
    I think you've lost a set of parentheses.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    204
    What about the bracket before exp and before the '.'? dont they say do the subtraction first?
    anduril462, in your alternative c code do you mean '=' instead of '-'? and should v1[i] be temp[i]?

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by tabstop View Post
    I think you've lost a set of parentheses.
    Yes, I have.

    Quote Originally Posted by a.mlw.walker View Post
    What about the bracket before exp and before the '.'? dont they say do the subtraction first?
    anduril462, in your alternative c code do you mean '=' instead of '-'? and should v1[i] be temp[i]?
    Yeah, I initially rewrote that using temp, then went back to v1.

    As a matter of fact, just ignore my last post. Two weeks in the mountains and I've forgotten how to read and write.

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    204
    So to conclude (i got a little confused with that) it isnt doing what you thought it was? and therefore the following code you suggested isnt the solution? Or is it? (I believe the Matlab)
    Last edited by a.mlw.walker; 08-15-2011 at 11:30 AM.

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Forget post #6 even happened. Take a look at the following line:
    Code:
    curve_to_fit_sum_abs = curve_to_fit_sum_abs + abs(curve_to_fit[i]);
    That one is for ints, and will truncate your results. You probably want fabs for doubles.

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    204
    That did a much better job, the function is a parameter estimation, and it gets the first one correct, however the other two are close but not that close (to the matlab - and the matlab is very close to the actual values), are there any other tricks like that i should be aware of?

  13. #13
    Registered User
    Join Date
    Apr 2008
    Posts
    204
    forget last post. excellent. thanks guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double's and floats, i need their bytes.
    By Florian in forum C++ Programming
    Replies: 26
    Last Post: 07-08-2008, 05:42 PM
  2. Replace double with long double in all my code.
    By boyfarrell in forum C Programming
    Replies: 8
    Last Post: 04-30-2007, 04:17 PM
  3. Changing double time to double cost
    By chrismax2 in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2004, 10:29 AM
  4. floats
    By phptech in forum C Programming
    Replies: 6
    Last Post: 07-07-2003, 08:42 PM
  5. Floats
    By JFK in forum C Programming
    Replies: 2
    Last Post: 11-27-2001, 07:53 AM

Tags for this Thread