Fraction of real numbers.
Moderator: Board moderators
Fraction of real numbers.
If there is any function in C/C++ for getting a fraction of real numbers?
For example f(345.123456789) = 0.123456789 .
For example f(345.123456789) = 0.123456789 .
Code: Select all
#include <cmath>
...
double x=345.123456789;
double y=fmod(x, 1);
Thank you for the answer!Cho wrote:But I remeber I read some post which said fmod is faulty.Code: Select all
#include <cmath> ... double x=345.123456789; double y=fmod(x, 1);
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;
}
try this code:
Code: Select all
d -= (int)d;
printf("%lf\n",d);
hm, this one works like previouschunyi81 wrote:try this code:
Code: Select all
d -= (int)d; printf("%lf\n",d);

And just how is your code supposed to work? Give us a few examples of what do you expect...Project wrote:hm, this one works like previouschunyi81 wrote:try this code:
Code: Select all
d -= (int)d; printf("%lf\n",d);
Sorry, I had problems with Internet and could not answer yesterday.
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.
Function print_fnum(double d) must print the value of d. That's all.misof wrote:And just how is your code supposed to work? Give us a few examples of what do you expect...
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.
-
- Guru
- Posts: 834
- Joined: Wed May 29, 2002 4:11 pm
- Location: Wroclaw, Poland
- Contact:
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
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)
Born from ashes - restarting counter of problems (800+ solved problems)
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.

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.