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

M

ME

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
 
R

roberts.noah

ME said:
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

x - static_cast<int>(x);
 
J

Jerry Coffin

ME said:
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.

Look up modf().
 
T

Tom

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
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
 
J

JE

Tom said:
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;

< 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...
 
T

Tom

< 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...

Ouch. I was Totally Wrong !!

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top