J
Joe Wright
Subtract the integral part and return the difference.Ivan said:How can I get a function to return the .xxx part of a floating point
number?
double frac(double d) {
int i = d;
return d - i;
}
Subtract the integral part and return the difference.Ivan said:How can I get a function to return the .xxx part of a floating point
number?
Use the floor() function and also consider how you want to handle negativeJoe Wright said:Subtract the integral part and return the difference.
double frac(double d) {
int i = d;
return d - i;
}
Joe said:Subtract the integral part and return the difference.
double frac(double d) {
int i = d;
return d - i;
}
Martin said:Sorry, but no:
#include <limits.h>
#include <stdio.h>
#include <math.h>
#include <float.h>
/* Joe Wright's contribution */
double frac(double d)
{
int i = d;
return d - i;
}
int main(void)
{
double fpnum;
double fpart;
double ipart;
printf("Ivan Leo Puoti asked:\n"
" \"How can I get a function to return the\n"
" .xxx part of a floating point number?\"\n"
"Joe Wright claimed:\n"
" \"Subtract the integral part and return\n"
" the difference.\"\n" "Let's test his code.\n"
"*Warning* The results of this test will vary with\n"
" the implementation.\n\n");
fpnum = INT_MAX;
printf("INT_MAX = %d, fpnum = %.*g\n", INT_MAX, DBL_DIG, fpnum);
fpnum += 3.37;
printf("fpnum modified to %.*g\n", DBL_DIG, fpnum);
fpart = frac(fpnum);
printf("Joe Wright's code claims the fractional part is %g\n"
" (and tells us nothing of the integral part.)\n\n", fpart);
fpart = modf(fpnum, &ipart);
printf("modf returns the fractional part as %g\n"
" and the integral part as %.*g\n", fpart, DBL_DIG, ipart);
return 0;
}
Ivan Leo Puoti asked:
"How can I get a function to return the
.xxx part of a floating point number?"
Joe Wright claimed:
"Subtract the integral part and return
the difference."
Let's test his code.
*Warning* The results of this test will vary with
the implementation.
INT_MAX = 2147483647, fpnum = 2147483647
fpnum modified to 2147483650.37
Joe Wright's code claims the fractional part is 4.29497e+09
(and tells us nothing of the integral part.)
modf returns the fractional part as 0.37
and the integral part as 2147483650
number?Ivan Leo Puoti said:How can I get a function to return the .xxx part of a floating point
Ivan said:Thanks for the answer but it presumes the float is defined in the
calling function, not the called function. But what can I do if the
float number is known only the the called function (In this case frac)?
double frac(double value)
{
double i = 3.15;
return (what goes here?);
}
I've tried this and it doesn't seem to return the right value. What I
want to do is return both the integer part and the fractional part of
the float (or double) variable. The calling function has no way of
knowing the value, the called function has to return a value, say 3.15,
and the calling function assignees the value returned from the frac
function to a variable. The point is if i in frac is 3.15, the calling
function must be able to assign 3.15 to a variable, and if possible I
want to avoid using global variables. Is this possible?
Ivan.
example
void main()
{
double a;
a=frac();
printf("the value is %.2f\n", a);
}
float frac()
{
double i = 1.23;
return (i); /*this is the code that doesn't work, is there a way to
obtain the intended result?*/
}
Ivan said:How can I get a function to return the .xxx part of a floating point number?
Ivan.
Stephen L. said:#include <math.h>
...
double frac(double value)
{
double unused;
double i = 3.15;
return (modf(i, &unused));
}
-
Stephen
I've tried this and it doesn't seem to return the right value. What I
want to do is return both the integer part and the fractional part of
the float (or double) variable. The calling function has no way of
knowing the value, the called function has to return a value, say 3.15,
and the calling function assignees the value returned from the frac
function to a variable. The point is if i in frac is 3.15, the calling
function must be able to assign 3.15 to a variable, and if possible I
want to avoid using global variables. Is this possible?
Ivan.
example
void main()
{
double a;
a=frac();
printf("the value is %.2f\n", a);
}
float frac()
{
double i = 1.23;
return (i); /*this is the code that doesn't work, is there a way to
obtain the intended result?*/
}
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.