How to convert a negative value to positive

Discussion in 'C Programming' started by Angus Comber, Nov 27, 2004.

  1. Angus Comber

    Angus Comber Guest

    Hello

    Sorry this really is a newbie question!

    I am doing some floating point arithmetic and calculating the time
    difference between two dates. The date values being comparied are actually
    double values. I was going to compare the dates by calculating the
    difference betwen the two numbers. But sometimes you get a -ve and
    sometimes a +ve value. I want to disregard a small variance eg of up to two
    minutes but what is best way to compare values.

    ie calculate difference and then convert any -ve values to +ve then check if
    (diff > x)

    or some other way?

    AC
     
    Angus Comber, Nov 27, 2004
    #1
    1. Advertising

  2. >Sorry this really is a newbie question!
    >
    >I am doing some floating point arithmetic and calculating the time
    >difference between two dates. The date values being comparied are actually
    >double values. I was going to compare the dates by calculating the
    >difference betwen the two numbers. But sometimes you get a -ve and
    >sometimes a +ve value.


    The unary minus operator will convert a negative value to a positive one.

    >I want to disregard a small variance eg of up to two
    >minutes but what is best way to compare values.
    >
    >ie calculate difference and then convert any -ve values to +ve then check if
    >(diff > x)


    You may want to use fabs(), in code similar to this:
    #include <math.h>
    double x1;
    double x2;
    double delta;

    ... put values in x1, x2, and delta ...;
    if (fabs(x1-x2) > delta) { ... too far apart ... ; }
    else { ... pretty close ... ; }

    Gordon L. Burditt
     
    Gordon Burditt, Nov 28, 2004
    #2
    1. Advertising

  3. Angus Comber wrote:

    > I am doing some floating point arithmetic and calculating the time
    > difference between two dates. The date values being comparied are actually
    > double values. I was going to compare the dates by calculating the
    > difference betwen the two numbers. But sometimes you get a -ve and
    > sometimes a +ve value. I want to disregard a small variance eg of up to two
    > minutes but what is best way to compare values.


    Personally, storing dates as floating point seems
    very strange to me. Yes, time is continuous(*), but not the
    way it is usually measured, and not the way you want it
    measured.

    (*) I believe it is still unknown if quantum mechanics quantizes
    time or not.

    -- glen
     
    glen herrmannsfeldt, Nov 28, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    1,393
    Patricia Shanahan
    Sep 29, 2006
  2. Johs
    Replies:
    19
    Views:
    505
    Old Wolf
    Jan 7, 2007
  3. rawebadvert
    Replies:
    0
    Views:
    331
    rawebadvert
    Mar 2, 2007
  4. rawebadvert
    Replies:
    0
    Views:
    340
    rawebadvert
    Mar 6, 2007
  5. Hicham Mouline
    Replies:
    2
    Views:
    835
    Keith Thompson
    Apr 23, 2010
Loading...

Share This Page