CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 1999
    Posts
    388

    Functions for real and fractional part of a number

    Are ther any functions in C++, which determines the real and fractional part of a given number. If so Please let me know.
    Are the any such division operator which gives results like 7/3 = 3 NOT 2.33! It means that any fractional part if exist should be truncated to the higher number.

    thankx


  2. #2
    Join Date
    Apr 1999
    Posts
    16

    Re: Functions for real and fractional part of a number

    Its very easy:
    double a = 7.33,c;
    long b;
    //Real part:
    b = (long)a;
    //Fractional part
    c = a - b;
    //Converting to higher number (result in b)
    if (b<a) b++;


    WBR Oak

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Functions for real and fractional part of a number

    #include <math.h>

    double a = 7, b = 3;
    double c = ceil(a/b);

    Regards,

    Paul McKenzie


  4. #4
    Join Date
    May 1999
    Location
    Oregon, USA
    Posts
    302

    Re: Functions for real and fractional part of a number

    modf will separate a double into its fractional and integer parts.
    As mentioned before, ceil will round up to next highest integer.
    For more info on these see the "Floating-Point Support Routines"
    section in the docs.



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