CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 19 of 19

Thread: double and %

  1. #16
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Not only that. The thing is that floor(-1.000001) is -2.0, so even if your FLT_MIN is only 0.0001 then it will fail.
    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.

  2. #17
    Join Date
    Oct 2001
    Location
    Dublin, Eire
    Posts
    880
    Originally posted by Kheun
    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.
    As said before, the string will not contain the '.' character. From what I understand, the captured number is kind of a monetary number, captured without the '.', but it still include 2 decimals (cents).

    Checking the last 2 digits might be the more accurate solution, easy enough to implement.
    Elrond
    A chess genius is a human being who focuses vast, little-understood mental gifts and labors on an ultimately trivial human enterprise.
    -- George Steiner

  3. #18
    Join Date
    Dec 2002
    Posts
    47
    Yves - depends on where/how you use fabs() !

    double abs = fabs(dbl);
    if ( ( abs - floor(abs) ) < FLT_MIN ) ...

    (but you've made good point either way)

    -rick

  4. #19
    Join Date
    Apr 2003
    Location
    Sydney, Australia
    Posts
    38
    Yeek. There has been some dodgy code posted here.

    Firstly, you cannot use a simple floor() or modf() function. If you do, then 14.99999999999 will be rounded to 14, not 15.

    On VC++ 6.0, FLT_MIN is 1.175494351e-38F, which is way too small for your epsilon value.

    You might use FLT_EPS, which is 1.192092896e-07F, however you would be better looking at the data and deciding on a suitable epsilon value. You need to consider where the data came from, and how accurate it truely is. If it is only recorded to the nearest 0.1, then an epsilon value of 1.0e-7 is silly.

    You might get something like this:

    Code:
    #include <cmath>
    
    const double epsilon = 0.000001;  // choose an appropriate value for this.
    
    double Round(double val)
    {
        return floor(val+0.5);
    }
    
    bool IsInt(double val)
    {
        return epsilon > fabs(val-Round(val));
    }

Page 2 of 2 FirstFirst 12

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured