473,606 Members | 2,818 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 2643
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********@com cast.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.i t> 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("Fractio nal 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.i t>
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

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

Similar topics

31
3649
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. However, they generate slightly different results for floating point numbers. Are they really supposed to generate exactly the same results? I guess so because both platforms are supposed to be IEEE floating point standard (754?) compliant. ...
5
3732
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 using the Debug-Version of the program, but it fails (or leads to false results) if we are using the Release-Version. After a long debugging session we found out, that our program was working correctly, but the floating point processing...
3
3543
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 scale, precision and error msg given if the entered value exceeds the max length dictated by the scale and precision, or if non-numeric input is provided. As I have never done anything quite like this with javascript before, I was wondering if...
12
3795
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 a = 1.0 / 0.0; // What is the value of a? Infinity? double b = 0.0 / 0.0; // What is the value of b? NaN? What about overflow/underflow conditions in the library? Is HUGE_VAL always a
5
13198
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. This particular problem has plagued me for years; it is making me very, very, old. :-( It deals with the way Javascript's method of floating point precision takes the simplest math calculations and steals a penny - in the example below, a simple...
18
2820
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 with - g flag) #include <cmath> #include <iostream> int main() {
10
3818
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
5813
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
2059
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 added and Divided by the number of ints to return the average, a float. Can someone see why I get for example: if the total is 227 then divided by 10 I get a return of 22.00000 instead of the correct answer 22.7.
0
8045
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7981
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8467
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8462
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8127
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6803
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5470
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4011
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2458
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 we have to send another system

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.