DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

1. Registered User
Join Date
Jan 2006
Posts
6

## Finding a decimal

As a beginner, I have been looking round and round for a function or any piece of code that will find decimal of a calculated number. If anyone can help with this, it will be greatly appreciated.

2. Registered User
Join Date
Aug 2005
Location
Melbourne...Australia
Posts
279
Have you got anything so far?
I just wrote one in one line?

3. Registered User
Join Date
Feb 2006
Posts
14
just try to save that float number to int and subtract it from that float number and save the result again back to float. Some type conversion mmay be required.

4. Registered User
Join Date
Jan 2006
Posts
55

5. Registered User
Join Date
Aug 2005
Location
Melbourne...Australia
Posts
279
<Math.h> is an old header file which has been replaced by <cmath>..

You should use the fmod() function from <cmath>.. ;)

6. Registered User
Join Date
Jan 2006
Posts
6
So, I guessing the the fmod() works in the semi-sorta way (correct me if I'm wrong) that fpart( in TI basic works, by finding the value after the decimal?

7. Registered User
Join Date
Jan 2006
Posts
6
Ok, so, I was right. It does find the decimal value. I guess I should tell you the program I am making. I am making a factoring program to match the speed of my calculator to my computer (it's actually no match, I know, but I thought it would be fun). Anywho, the program works for some values, but for others, the program closes after it finds some values. Here's the code. Please note that it is probely very messy and has no notes.

#include <iostream>
#include <stdio.h>
#include <cmath>

using namespace std;

long double s = 1;
long double t = 1;
long double u = 1;

int testnum();

int findfrac()
{
long double dec,ff,gg;
gg = 1;

t = s/u;

dec = fmod(t,gg);

if (dec == 0)
{
cout << u << ",\a";
u = u + 1;
}

if (dec != 0)
{
u = u + 1;
}

testnum();
}

int exit()
{
cout << s << "\a";
int po;
cin >> po;
}

int testnum()
{
if (s == u)
{
exit();
}
if (s != u)
{
findfrac();
}

return 0;
}

int main()
{

cout << "Factoring Number: ";
cin >> s;
testnum();
return 0;
}
If anyone has any reasons why the program quits on me, please, do tell.

8. Registered User
Join Date
Aug 2005
Location
Melbourne...Australia
Posts
279
Are you trying to do prime factorisation? What sort of factoring do you mean? Could you show an example?
Code:
```double decimal(double x)
{
return((fmod(x,10))-((int)fmod(x,10)));
}```
There is a function to give the decimal part like you were after..
Last edited by Code_Nerd; 02-02-2006 at 12:34 AM.

9. Registered User
Join Date
Jan 2006
Posts
55
Originally Posted by Code_Nerd
<Math.h> is an old header file which has been replaced by <cmath>..

You should use the fmod() function from <cmath>.. ;)
How does fmod() solves the purpose.fmod() returns the floating-point remainder of x / y.
It doesn't give the fractional part.

10. Registered User
Join Date
Jan 2006
Posts
6
The program that I am making should find all the possible factors of a given number. What it does is ask for the number to be factored, start by dividing by one (which is always a factor), and displays "1" on the screen. Next, it adds 1 to int "u", which is the variable by which the number to be factored is divided by. Finds the fractional part of the answer, if 0, display, if not, add 1 to "u", and repeat. I know there is probely an easier way to make this program, but this is how I designed it on my calculator. At this point, the program works, exept that the program quits when it works with any number, at any point that is about greater 18-19 thousand. Any solutions, comments?

11. Registered User
Join Date
Aug 2005
Location
Melbourne...Australia
Posts
279
Originally Posted by Nokia2280
How does fmod() solves the purpose.fmod() returns the floating-point remainder of x / y.
It doesn't give the fractional part.
Yes thats correct.. If you USE fmod() like I said..
It will do what the OP was after..
This function returns the decimal section of a number..
Code:
```double decimal(double x)
{
return((fmod(x,10))-((int)fmod(x,10)));
}```
Im sorry bubbster I dont get what your trying to do.. Can you post up what the output should look like for a certain number?
Thanks mate
Last edited by Code_Nerd; 02-02-2006 at 12:34 AM.

12. Registered User
Join Date
Feb 2006
Posts
25

## working too hard

I think you all are working too hard, I made this and if I understand what you want it works.

Code:
```long double s = 1;
long double u = 1;

void testnum()
{
//test all nums <= s
do{
if(s % u == 0) // %(mod) returns 0 if u divides evenly into s
{
cout << endl << u;
}
}while(u++ <= s);
}

int main()
{
cout << "Enter num: ";
cin >> s;
testnum();

//get input to allow user to see results
cin >> s;

return 0;
}```
Last edited by Nathan87; 02-02-2006 at 01:56 AM.

13. Registered User
Join Date
Jan 2006
Posts
55
Originally Posted by Nathan87
I think you all are working too hard, I made this and if I understand what you want it works.

testnum()
{
//test all nums <= s
do{
if(s % u == 0) // %(mod) returns 0 if u divides evenly into s
{
cout << endl << u;
}
}while(u++ <= s);
}

void main()
{
cout << "Enter num: ";
cin >> s;
testnum();

//get input to allow user to see results
cin >> s;

return 0;
}
void main and return 0;...where did you learn that??
use int main

14. Registered User
Join Date
Aug 2005
Location
Melbourne...Australia
Posts
279
Code:
`void main()`

Nathan, how does your function testnum() know the value of s?

You dont send it there in any way?

15. Registered User
Join Date
Feb 2006
Posts
25
durr woops

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise