Thread: Assigning values to double vars

  1. #1
    Registered User
    Join Date
    Nov 2006
    Location
    Greece
    Posts
    10

    Assigning values to double vars

    Hi everyone,
    Is there any way that i could assign a large amount of decimal parts to a double variable?
    Thx in advance

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Post the code showing what it is you are trying to do and the unexpected results you receive.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Nov 2006
    Location
    Greece
    Posts
    10
    Well ok,
    This is actually an issue that arose when i was trying to compare 2 doubles.For example
    Code:
     
     
      double lim = 1.0000000000000000001 ; 
      double res = 0.0 ; 
    
    /*later on */
      res = some_calculation( ) ; 
       
      if( res < lim )
        do_some_work() ; 
     else
        do_something_else() ;
    But to put it more generally .What precision do double vars handle ??

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    All floating point number as inaccurate. You'll want to use something like abs to compare them. Search the forum for something like floating point accuracy and you should get plenty on the topic.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Levia8an
    What precision do double vars handle ??
    It varies.
    http://c-faq.com/fp/fpequal.html
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Nov 2006
    Location
    Greece
    Posts
    10
    Hmmm ,
    After searching a bit with google , it seems that the precision is defined in float.h . In that case
    the number of decimal parts a double can handle is 15 ( system dependent ). Well the question now is how can i hanlde floating point constants of about ,let's say, 20 decimal parts.
    To be more specific in the code i posted previously i assumed that the
    " res < lim " comparison will depend on the 15 first decimal parts .Is that right?If yes is there a way i could work around this problem?


    P.S Thank you Dave_Sinkula , quzah for responding.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well there's long double, but that doesn't buy much in terms of precision (most of it goes on the exponent).

    Failing that, you need a maths library like GMP
    http://www.swox.com/gmp/
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Nov 2006
    Location
    Greece
    Posts
    10
    in microsoft visual studio 98 platform.
    Code:
    /*float.h*/
    #define DBL_DIG         15                      /* # of decimal digits of precision */
    
    /*...*/
    #define LDBL_DIG        18                 /* # of decimal digits of precision */
    So it can't be much of a help

    The gmp library has very cool stuf.Thanx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assigning Multiple Values to Array/ Vector
    By bengreenwood in forum C++ Programming
    Replies: 2
    Last Post: 07-02-2009, 01:15 PM
  2. Replies: 5
    Last Post: 06-10-2007, 05:54 AM
  3. Assigning a Double to a long
    By natalia in forum C Programming
    Replies: 16
    Last Post: 09-24-2004, 11:18 AM
  4. error message, what does it mean?
    By Shy_girl_311 in forum C++ Programming
    Replies: 5
    Last Post: 11-09-2001, 09:54 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM