returning a floating point value from a function

J

Joe Wright

Ivan said:
How can I get a function to return the .xxx part of a floating point
number?
Subtract the integral part and return the difference.

double frac(double d) {
int i = d;
return d - i;
}
 
M

Malcolm

Joe Wright said:
Subtract the integral part and return the difference.

double frac(double d) {
int i = d;
return d - i;
}
Use the floor() function and also consider how you want to handle negative
numbers.
Casting to int will give wrong results if the size of the parameter
overflows the range of an integer .
 
I

Ivan Leo Puoti

How can I get a function to return the .xxx part of a floating point number?

Ivan.
 
M

Martin Ambuhl

Joe said:
Subtract the integral part and return the difference.

double frac(double d) {
int i = d;
return d - i;
}

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
 
J

Joe Wright

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

You are quite right and I am extremely embarrassed. My apologies to
Ivan and to all of you.
 
M

Mike Wahler

Ivan Leo Puoti said:
How can I get a function to return the .xxx part of a floating point
number?

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

double frac(double value)
{
double i = 0;
return modf(value, &i);
}

int main()
{
double d = 3.14;
printf("Fractional part of %f is %f\n", d, frac(d));
return 0;
}

-Mike
 
S

Stephen L.

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?);
}

#include <math.h>

...


double frac(double value)
{
double unused;
double i = 3.15;

return (modf(i, &unused));
}



-
Stephen
 
I

Ivan Leo Puoti

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

Ivan Leo Puoti

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?*/
}
 
B

Barry Schwarz

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();

This is your problem. There is no prototype in scope for frac so your
C89 compiler is obligated to assume that frac returns an int. Since
frac does not return an int, you have invoked undefined behavior. Add
a prototype and remember to include stdio.h for printf.

If you can increase the warning level on your compiler, it may tell
you when you call functions with no prototype.

While you are at it, fix main to return an int. And specify the void
argument list for both main and frac.
printf("the value is %.2f\n", a);
}

float frac()

Why when all you code uses doubles does frac return a float?
{
double i = 1.23;
return (i); /*this is the code that doesn't work, is there a way to
obtain the intended result?*/
}



<<Remove the del for email>>
 
S

Stephen L.

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?*/
}


I give up...

-
Stephen
 
G

geowar

Converting to int also involkes a costly CPU -> memory -> CPU move on
most risc processors like PowerPC.

Use the "floor" (Luke). ;-)
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top