473,498 Members | 1,704 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

returning a floating point value from a function

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

Ivan.

Nov 14 '05 #1
12 2627
Ivan Leo Puoti wrote:
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;
}

--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #2

"Joe Wright" <jo********@comcast.net> wrote in message
Ivan Leo Puoti wrote:
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;
}

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 .
Nov 14 '05 #3
Joe Wright wrote:
Ivan Leo Puoti wrote:
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;
}


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
Nov 14 '05 #4
Martin Ambuhl wrote:
Joe Wright wrote:
Ivan Leo Puoti wrote:
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;
}


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.
--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #5

"Ivan Leo Puoti" <pu***@inwind.it> wrote in message news:40c1f045$1@nntp....
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
Nov 14 '05 #6
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?);
}
Nov 14 '05 #7
Ivan Leo Puoti wrote:

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
Nov 14 '05 #8
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?*/
}

Nov 14 '05 #9
On Mon, 07 Jun 2004 01:08:23 +0100, Ivan Leo Puoti <pu***@inwind.it>
wrote:
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>>
Nov 14 '05 #10
Ivan Leo Puoti wrote:

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

Ivan.

"Stephen L." wrote:
Ivan Leo Puoti wrote:

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

Ivan Leo Puoti wrote:
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
Nov 14 '05 #11
Thanks I think I've worked it out.

Ivan.

Nov 14 '05 #12
Converting to int also involkes a costly CPU -> memory -> CPU move on
most risc processors like PowerPC.

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

--
Enjoy,
George Warner,
Schizophrenic Optimization Scientist
Apple Developer Technical Support (DTS)

Nov 14 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
3614
by: JS | last post by:
We have the same floating point intensive C++ program that runs on Windows on Intel chip and on Sun Solaris on SPARC chips. The program reads the exactly the same input files on the two platforms....
5
3723
by: Anton Noll | last post by:
We are using Visual Studio 2003.NET (C++) for the development of our software in the fields digital signal processing and numerical acoustics. One of our programs was working correctly if we are...
3
3532
by: 00steve | last post by:
Hi, I am looking for tips on how to go about creating a function verifies floating point input from a text box. For the sake of simplicity I would like the function the take 3 parameters: the...
12
3782
by: Dave Rahardja | last post by:
Does the C++ standard specify the behavior of floating point numbers during "exceptional" (exceptional with respect to floating point numbers, not exceptions) conditions? For example: double...
5
13183
by: rocknbil | last post by:
Hello everyone! I'm new here but have been programming for the web in various languages for 15 years or so. I'm certainly no "expert" but can keep myself out of trouble (or in it?) most of the time....
18
2810
by: n.torrey.pines | last post by:
I understand that with floats, x*y == y*x, for example, might not hold. But what if it's the exact same operation on both sides? I tested this with GCC 3.4.4 (Cygwin), and it prints 0 (compiled...
10
3792
by: ratcharit | last post by:
Currently using cosine function in math.h Currently I get: 1 = cos(1e^-7) Is there another way for cos to return value of high accuracy say: 0.999999 = cos(1e^-7)
160
5730
by: DiAvOl | last post by:
Hello everyone, Please take a look at the following code: #include <stdio.h> typedef struct person { char name; int age; } Person;
11
2043
by: Peter | last post by:
I have written this small app to explain an issue I'm having with a larger program. In the following code I'm taking 10 ints from the keyboard. In the call to average() these 10 ints are then...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7002
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7205
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6887
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5462
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4910
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1419
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.