fraction problems
hello everyone!
i have a variable called num that is a double.
trying to seperate num into integer and the fraction:
int int_num = num;
double fraction = num - int_num.
the problem is that when i input for exapmle: 34.45
int_num gets 34 but fraction gets 0.44999999...
is there a way to fix this?
There''s no way to fix it, unless you make your own number type . Representing 34.45 with a double leads to unprecise results. But do you really need to fix it? What are you trying to do?
im trying to calculate how many times a digit is present at a number.
for example:
4 is present 3 times at the number 44,345
anyone? i really need to solve this...
for example:
4 is present 3 times at the number 44,345
anyone? i really need to solve this...
How ''bout making num fixed point? num = int + frac/base
If base == 10^x, all you have to do is convert int and frac to a string and compare characters.
If base == 10^x, all you have to do is convert int and frac to a string and compare characters.
Alternatively, If you know the maximum number of decimal places, try something like this ...
double fNum = 34.45;
int nNum;
double fFraction;
int nFraction;
nNum = long(fNum) * 10000
nFraction = fNum * 10000 - nNum
fFraction = double(nFraction) * 10000
Obviously it's only accurate up to a certain number of digits, depends what you want.
Regards
[edited by - Shag on November 2, 2002 12:25:55 PM]
double fNum = 34.45;
int nNum;
double fFraction;
int nFraction;
nNum = long(fNum) * 10000
nFraction = fNum * 10000 - nNum
fFraction = double(nFraction) * 10000
Obviously it's only accurate up to a certain number of digits, depends what you want.
Regards
[edited by - Shag on November 2, 2002 12:25:55 PM]
i'm with petewood here.
Edit: Damn [ source ]tags...
HTH, Steve
Steve
DirectX Programmer
Soon to be the new Bill Gates
[edited by - Evil Bill on November 3, 2002 4:24:32 PM]
#define PRESCISION 1000000.0int CountOccurences(const double& dNum, char chFind){char szBuff[64];char* pBuff;int nCount = 0; sprintf(szBuff,"%d",(int)(dNum*PRESCISION)); pBuff = szBuff; while(*pBuff != '\0') { if(*pBuff == chFind) nCount++; pBuff++; } return nCount;}
Edit: Damn [ source ]tags...
HTH, Steve
Steve
DirectX Programmer
Soon to be the new Bill Gates
[edited by - Evil Bill on November 3, 2002 4:24:32 PM]
Actually, there are standard C math library functions for this purpose: frexp(),ldexp(), and modf(). To get both the the characteristic (the integer part) and the mantissa (the fractional part), you'd need to do something like this:
You can use int instead of long, but I recommend using a long in case it is a very large value. To use frexp() to do the same thing, you'd change it so that:
Finally, if you want to get just the exponent, then you should use
count = (int) floor(d);
to ensure that the cast value is truncated rather than rounded to the nearest integer (this assumes that all values in question are positive).
Note that this code is not actually tested. You will probably have to make some minor modifications to get it to work as you need it to.
Trouble rather the tiger in his lair than the Scholar among his books
[edited by - Schol-R-LEA on November 6, 2002 5:12:21 PM]
#include <math.h>main(){ double value = 3.14159265358979; double characteristic, mantissa; long count; mantissa = modf(value, &characteristic); //returns 0.14159265358979 to mantissa, sets characteristic to 3.0 count = (long) characteristic;}
You can use int instead of long, but I recommend using a long in case it is a very large value. To use frexp() to do the same thing, you'd change it so that:
double exponent; mantissa = frexp(value, &exponent); // returns 0.14159265358979 to mantissa, sets exponent // to the binary power of the characteristic characteristic = pow(2, exponent);
Finally, if you want to get just the exponent, then you should use
count = (int) floor(d);
to ensure that the cast value is truncated rather than rounded to the nearest integer (this assumes that all values in question are positive).
Note that this code is not actually tested. You will probably have to make some minor modifications to get it to work as you need it to.
Trouble rather the tiger in his lair than the Scholar among his books
[edited by - Schol-R-LEA on November 6, 2002 5:12:21 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement