Fraction of real numbers.

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
Project
New poster
Posts: 12
Joined: Tue Jul 19, 2005 9:29 am

Fraction of real numbers.

Post by Project »

If there is any function in C/C++ for getting a fraction of real numbers?

For example f(345.123456789) = 0.123456789 .
Cho
A great helper
Posts: 274
Joined: Wed Oct 20, 2004 11:51 pm
Location: Hong Kong

Post by Cho »

Code: Select all

#include <cmath>
...
double x=345.123456789;
double y=fmod(x, 1);
But I remeber I read some post which said fmod is faulty.
Project
New poster
Posts: 12
Joined: Tue Jul 19, 2005 9:29 am

Post by Project »

Cho wrote:

Code: Select all

#include <cmath>
...
double x=345.123456789;
double y=fmod(x, 1);
But I remeber I read some post which said fmod is faulty.
Thank you for the answer!
Yeah, really this one does not work properly.
Another solution "y=x-floor(x)" also.
But why so I can't understand.

Here is a code, which works faulty:

Code: Select all

#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

	void print_fnum(double d)
	{
		if (d<0) {
			printf("-");
			d=-d;
		}
		printf("%.0lf", floor(d));
		d -= floor(d);
		if (d>0) {
			printf(".");
			while (d>0) {
				d *= 10;
				printf("%.0lf", floor(d));
				d -= floor(d);
			}
		}
	}

int main() {

	double d = -21.874823734;

	print_fnum(d);

	return 0;

}
chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Post by chunyi81 »

try this code:

Code: Select all

d -= (int)d;
printf("%lf\n",d);
Project
New poster
Posts: 12
Joined: Tue Jul 19, 2005 9:29 am

Post by Project »

chunyi81 wrote:try this code:

Code: Select all

d -= (int)d;
printf("%lf\n",d);
hm, this one works like previous :(
misof
A great helper
Posts: 430
Joined: Wed Jun 09, 2004 1:31 pm

Post by misof »

Project wrote:
chunyi81 wrote:try this code:

Code: Select all

d -= (int)d;
printf("%lf\n",d);
hm, this one works like previous :(
And just how is your code supposed to work? Give us a few examples of what do you expect...
chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Post by chunyi81 »

d is a double, and by casting it to an int, you can get the floor of d.

For example,

double d = 1.2345;

d -= (int)d should give:

d = d - 1

which should leave the value of d as 0.2345. Of course, this is subject to floating point error.
Project
New poster
Posts: 12
Joined: Tue Jul 19, 2005 9:29 am

Post by Project »

Sorry, I had problems with Internet and could not answer yesterday.
misof wrote:And just how is your code supposed to work? Give us a few examples of what do you expect...
Function print_fnum(double d) must print the value of d. That's all.
For example in code it must print "-21.874823734" instead of "-21.874823733999999575416950392536818981170654296875".
more examples:
print_fnum(0) = 0;
print_fnum(17) = 17;
print_fnum(235.35) = 235.35;
print_fnum(-123.7) = -123.7;

2chunyi81. Yeah, yeah, I've got your approach. Thanks for it. But with it print_fnum works as with -floor or fmod(d, 1).
But can you tell about "floating point error" ? I've heard something, but I can't imaging how does it happen.
Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Post by Dominik Michniewski »

Use formatting string when you output the number

i.e.

printf("%.*lf",number_of_digits_after_dot,number);

this line prints "number" with "number_of_digits_after_dot" numbers afer decimal dot. You must know how many numbers after decimal point you want to output :)

Best regards
DM

PS. Floating point error = overflow, underflow, divide by zero - source of problem could be different ;)
If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)
Project
New poster
Posts: 12
Joined: Tue Jul 19, 2005 9:29 am

Post by Project »

Thank you, Dominik, for your response! :).
Buy the way, in printf there is also "lg" if you want print all significant digits after decimal point.
I've tried to code a function that's doing this job just for example of incorrect using of such constructions as "-floor, fmod(x, 1), -int" for finding fraction of double number. Something like in Pascal there is a function frac(d) which doing this job without errors.
Post Reply

Return to “C++”