What is the fastest way of getting the fraction part of a floating point?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ME

    What is the fastest way of getting the fraction part of a floating point?

    What is the fastest way of getting the fraction part of a floating point
    number?

    So, if I have a float 10.3 I want to get a float with value 0.3 fast.

    Thanks




  • roberts.noah@gmail.com

    #2
    Re: What is the fastest way of getting the fraction part of a floating point?


    ME wrote:[color=blue]
    > What is the fastest way of getting the fraction part of a floating point
    > number?
    >
    > So, if I have a float 10.3 I want to get a float with value 0.3 fast.
    >
    > Thanks[/color]

    x - static_cast<int >(x);

    Comment

    • Jerry Coffin

      #3
      Re: What is the fastest way of getting the fraction part of a floating point?

      ME wrote:[color=blue]
      > What is the fastest way of getting the fraction part of a floating point
      > number?
      >
      > So, if I have a float 10.3 I want to get a float with value 0.3 fast.[/color]

      Look up modf().

      --
      Later,
      Jerry.

      Comment

      • Tom

        #4
        Re: What is the fastest way of getting the fraction part of a floating point?

        On Thu, 2 Feb 2006 20:13:09 +0100, "ME" <me@home.com> wrote:
        [color=blue]
        >What is the fastest way of getting the fraction part of a floating point
        >number?
        >
        >So, if I have a float 10.3 I want to get a float with value 0.3 fast.
        >
        >Thanks
        >
        >
        >[/color]
        From MS help. Convert to C++ style if you like.
        Use 1.0 as the "y" number to get the fractional part.

        double x = div(10.3, 1.0);
        // result: x = 0.3;

        /* DIV.C: This example takes two integers as command-line
        * arguments and displays the results of the integer
        * division. This program accepts two arguments on the
        * command line following the program name, then calls
        * div to divide the first argument by the second.
        * Finally, it prints the structure members quot and rem.
        */

        #include <stdlib.h>
        #include <stdio.h>
        #include <math.h>

        void main( int argc, char *argv[] )
        {
        int x,y;
        div_t div_result;

        x = atoi( argv[1] );
        y = atoi( argv[2] );

        printf( "x is %d, y is %d\n", x, y );
        div_result = div( x, y );
        printf( "The quotient is %d, and the remainder is %d\n",
        div_result.quot , div_result.rem );
        }


        Output

        x is 876, y is 13
        The quotient is 67, and the remainder is 5




        Comment

        • JE

          #5
          Re: What is the fastest way of getting the fraction part of a floating point?

          Tom wrote:[color=blue]
          > On Thu, 2 Feb 2006 20:13:09 +0100, "ME" <me@home.com> wrote:
          >[color=green]
          > >What is the fastest way of getting the fraction part of a floating point
          > >number?
          > >
          > >So, if I have a float 10.3 I want to get a float with value 0.3 fast.
          > >
          > >Thanks
          > >
          > >
          > >[/color]
          > From MS help. Convert to C++ style if you like.
          > Use 1.0 as the "y" number to get the fractional part.
          >
          > double x = div(10.3, 1.0);
          > // result: x = 0.3;[/color]

          < snip >

          That's not going to work. First of all, div returns a div_t or ldiv_t
          struct. Second of all, div takes two ints or two longs as arguments
          (double is trouble!). In your case, the remainder of the div_t return
          div_t::rem would be 0...

          Comment

          • Tom

            #6
            Re: What is the fastest way of getting the fraction part of a floating point?

            On 2 Feb 2006 13:08:33 -0800, "JE" <jericson@pacbe ll.net> wrote:
            [color=blue]
            >Tom wrote:[color=green]
            >> On Thu, 2 Feb 2006 20:13:09 +0100, "ME" <me@home.com> wrote:
            >>[color=darkred]
            >> >What is the fastest way of getting the fraction part of a floating point
            >> >number?
            >> >
            >> >So, if I have a float 10.3 I want to get a float with value 0.3 fast.
            >> >
            >> >Thanks
            >> >
            >> >
            >> >[/color]
            >> From MS help. Convert to C++ style if you like.
            >> Use 1.0 as the "y" number to get the fractional part.
            >>
            >> double x = div(10.3, 1.0);
            >> // result: x = 0.3;[/color]
            >
            >< snip >
            >
            >That's not going to work. First of all, div returns a div_t or ldiv_t
            >struct. Second of all, div takes two ints or two longs as arguments
            >(double is trouble!). In your case, the remainder of the div_t return
            >div_t::rem would be 0...[/color]

            Ouch. I was Totally Wrong !!

            Modf is the ticket. I think I need a nap.

            Comment

            Working...