double and %
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: double and %

  1. #1
    Join Date
    Sep 2003
    Posts
    815

    double and %

    Hi,

    I have a very big number (up to 15 digits) which is stored as a double
    I need to know if this number is integer or not
    but % function doesn't exist for double

    I wanted to do chaeck myDoubleNum%1 is not equal to 0

    any suggestions?

    thanks

  2. #2
    Join Date
    May 2000
    Location
    Scotland, Livingston.
    Posts
    728
    Given that doubles cannot represent all values with absolute precision you might find that you assign (or enter) an integer (say 15.0) but that the double represents it as 14.999999999999.

    What are you trying to do?
    Dave Mclelland.

  3. #3
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    There is a possible solution, but it has a serious caveat.

    The solution is to use the function modf
    modf
    Splits a floating-point value into fractional and integer parts.

    double modf( double x, double *intptr );

    Return Value

    This function returns the signed fractional portion of x. There is no error return.
    The problem with this is that the fractional part may not be exactly zero, if you have worked with the number a bit. This is a standard problem when handling floating point numbers and the usual solution is to treat everything smaller than a given epsilon as zero.

    So the code could look like this:
    Code:
    #include <math.h>
    
    const double epsilon = 0.00001; // you should adjust this as needed
    
    bool IsInteger(double d)
    {
      double intpart;
      return fabs(modf(d, &intpart)) < epsilon;
    }
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  4. #4
    Join Date
    Sep 2003
    Posts
    815
    well

    I found an ugly soloution to my specific problem

    I get my gig number as a string and I have to divide it by 100 before using it.

    since the string doesn't contain . (that's one of the rules)

    then I simply check its last 2 chars I need them both to be 00 inorder to get an Integer

    something like that:

    double dMyDouble = atof(sMyTmpString.c_str());
    dMyDouble /= 100;

    if((sMyTmpString[14]== '0') && (sMyTmpString[13]== '0'))
    {
    //an integer
    }
    else
    //not an integer

    note: sMyTmpString is maximum 15 chars long and does not '.'

    What do you think?

  5. #5
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Why don't you just use an integer in the first place ? Many compilers include 64 bit integers which would be enough to handle 15 decimal digits. On MS VC, the datatype in question is called __int64.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  6. #6
    Join Date
    Sep 2003
    Posts
    815
    well I can't use int because the number may not be an Integer and then I need to treat it differently, but you gave ma another idea, check the difference between the double value to the __int64 value, meaning:

    double dMyDouble = atof(sMyTmpString.c_str());
    dMyDouble /= 100;

    double dDiff = dMyDouble - (__int64)dMyDouble;
    if(dDiff)
    printf("Not an integer");
    else
    printf("An integer");

    What do you think?
    Is it better than my privious one?

  7. #7
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    There may be problems with both approaches. The first one will not work correctly if the string doesn't contain exactly 14 digits. What if the string is only "12" ? The second approach suffers from the same rounding problems that Dave and myself have pointed out above.

    If you really need doubles then just use my IsInteger function.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  8. #8
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    what about:

    double d = 5.1;
    if( double(int(d)) == d ) // is a integer

  9. #9
    Join Date
    May 2000
    Location
    Scotland, Livingston.
    Posts
    728
    Originally posted by mwilliamson
    what about:

    double d = 5.1;
    if( double(int(d)) == d ) // is a integer
    Same representation problem. Floating point numbers may not exactly correspond with what you assign them. You cant safely do an == with a double, its not a precise representation.
    Dave Mclelland.

  10. #10
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    avi123,

    Since you already captured the int/double value in the form of string, why not scan for the decimal point or '.'. If there exists non-zero value character(s) after the decimal point, the string presentation must be a double.

  11. #11
    Join Date
    Apr 2003
    Posts
    893

    Re: double and %

    Originally posted by avi123
    Hi,

    I have a very big number (up to 15 digits) which is stored as a double
    I need to know if this number is integer or not
    but % function doesn't exist for double

    I wanted to do chaeck myDoubleNum%1 is not equal to 0

    any suggestions?

    thanks
    May I ask why it has to be 15 digits ?

    Thanks a lot...

    Regards,

    -Nina
    Last edited by hometown; October 13th, 2003 at 09:47 PM.

  12. #12
    Join Date
    Jul 2003
    Location
    Singapore
    Posts
    1,822

    Re: Re: double and %

    Originally posted by hometown
    May I ask why it has to be 15 digits ?

    Thanks a lot...

    Regards,

    -Nina
    different program have differ specifications and requirements..In what he is doin he needs to manipulate a 15 didgit number...
    or he simply wants to learn how to manipulate in case of a 15 digit no...
    <edit>BTW : he said UPTO 15..</edit>
    Last edited by Joseph_R_Thomas; October 13th, 2003 at 10:36 PM.
    R. Thomas
    "Be anxious for nothing, but in everything by prayer and supplication, with thanksgiving, let your requests be made know to God; and the peace of God, which surpasses all understanding, will guard your hearts and minds through Christ Jesus."Philippians 4:6-7
    "Rejoice always, pray without ceasing, in everything give thanks; for this is the will of God in Christ Jesus for you."1Thess. 5:16-18

  13. #13
    Join Date
    Dec 2002
    Posts
    47
    Instead of modf, use floor() instead. Do use the "epsilon" approach suggested by Yves. I like using "FLT_MIN" for that purpose.

    Code:
    #include <math.h>
    #include <float.h>
    
    int function(double dbl)
    {
    
    /* ... */
    
      if (  ( dbl - floor(dbl) ) < FLT_MIN )
      {
        /* treat as integer. */
      }
      else
      {
        /* not integer. */
      }
    
    
    /* ... */
    
    }
    -rick
    Last edited by rfmobile; October 14th, 2003 at 08:17 AM.

  14. #14
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    If my memory serves me right, this doesn't work for negative numbers.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  15. #15
    Join Date
    Dec 2002
    Posts
    47
    Yup, I forgot the fabs().
    -rick

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center