Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

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.
[196 byte] By [bubbster] at [2007-11-11 7:53:42]
# 1 Re: Finding a decimal
Have you got anything so far?
I just wrote one in one line?
Code_Nerd at 2007-11-11 21:01:51 >
# 2 Re: Finding a decimal
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.
ashishprem at 2007-11-11 21:02:51 >
# 3 Re: Finding a decimal
Use modf() function of <math.h>
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcecrt/htm/_wcecrt_modf.asp
Nokia2280 at 2007-11-11 21:03:45 >
# 4 Re: Finding a decimal
<Math.h> is an old header file which has been replaced by <cmath>..

You should use the fmod() function from <cmath>.. ;)
Code_Nerd at 2007-11-11 21:04:50 >
# 5 Re: Finding a decimal
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?
bubbster at 2007-11-11 21:05:49 >
# 6 Re: Finding a decimal
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.
bubbster at 2007-11-11 21:06:53 >
# 7 Re: Finding a decimal
Are you trying to do prime factorisation? What sort of factoring do you mean? Could you show an example?
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..
Code_Nerd at 2007-11-11 21:07:57 >
# 8 Re: Finding a decimal
<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.
Nokia2280 at 2007-11-11 21:08:51 >
# 9 Re: Finding a decimal
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?
bubbster at 2007-11-11 21:09:59 >
# 10 Re: Finding a decimal
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..
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
Code_Nerd at 2007-11-11 21:10:54 >
# 11 Re: Finding a decimal
I think you all are working too hard, I made this and if I understand what you want it works.

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;
}
Nathan87 at 2007-11-11 21:11:59 >
# 12 Re: Finding a decimal
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
Nokia2280 at 2007-11-11 21:13:03 >
# 13 Re: Finding a decimal
void main()

***? Very bad.. :(

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

You dont send it there in any way?
Code_Nerd at 2007-11-11 21:13:59 >
# 14 Re: Finding a decimal
durr woops
Nathan87 at 2007-11-11 21:14:55 >
# 15 Re: Finding a decimal
durr woops

You dont make any sense mate..
Code_Nerd at 2007-11-11 21:15:59 >
# 16 Re: Finding a decimal
void main()

***? Very bad.. :(

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

You dont send it there in any way?

all variables are assumed global as in orig code submitted by bubbster.
Nathan87 at 2007-11-11 21:16:57 >
# 17 Re: Finding a decimal
void main(), Global variables.. what next?? <iostream.h>..

Not a prob Nathan.. I dont actually get what the OP is trying to do..
Code_Nerd at 2007-11-11 21:18:05 >
# 18 Re: Finding a decimal
if i understand correctly he just wants to show all the factors of a user supplied number (i.e. all numbers that divide evenly in to that number)
Nathan87 at 2007-11-11 21:18:59 >
# 19 Re: Finding a decimal
variables s and u are global as in the original code submitted by bubbster

Yes they are..

I have no idea why he would do that? :eek: :eek:
Code_Nerd at 2007-11-11 21:20:03 >
# 20 Re: Finding a decimal
Yes they are..

I have no idea why he would do that? :eek: :eek:
yes, not good practice but I just thought I would leave things the way they were as much as possible.
Nathan87 at 2007-11-11 21:21:05 >
# 21 Re: Finding a decimal
better variable names wouldn't hurt either...
Nathan87 at 2007-11-11 21:22:12 >
# 22 Re: Finding a decimal
if i understand correctly he just wants to show all the factors of a user supplied number (i.e. all numbers that divide evenly in to that number)

Is that all?

So if you enter 20 the answer is 1, 2, 4, 5, 10..
Code_Nerd at 2007-11-11 21:23:04 >
# 23 Re: Finding a decimal
if i understand correctly he just wants to show all the factors of a user supplied number (i.e. all numbers that divide evenly in to that number)

If thats all they want..

#include <iostream>

using namespace std;

int main()
{
int x = 200;
for(int y = 1; y <= x ; y++)
{
if((x % y) == 0)
cout<<y<<" ";
}
return 0;
}
Code_Nerd at 2007-11-11 21:24:05 >
# 24 Re: Finding a decimal
Is that all?

So if you enter 20 the answer is 1, 2, 4, 5, 10..
and 20.
Nathan87 at 2007-11-11 21:25:11 >
# 25 Re: Finding a decimal
that would work, except the number to find the factors of must be accepted from the user, it can't be hard-coded.
Nathan87 at 2007-11-11 21:26:10 >
# 26 Re: Finding a decimal
that would work, except the number to find the factors of must be accepted from the user, it can't be hard-coded.

Of course..
If the OP cant add that, they really shouldnt be here..
Code_Nerd at 2007-11-11 21:27:10 >
# 27 Re: Finding a decimal
#include <iostream>

using namespace std;

int main()
{
int x = 200;
for(int y = 1; y <= x ; y++)
{
if((x % y) == 0)
cout<<y<<" ";
}
return 0;
}

See...I knew there was an easier way to do it. It works, except I just modded it with a cin for x. Thanks a bunch, but, I still want to get my code working.
bubbster at 2007-11-11 21:28:16 >
# 28 Re: Finding a decimal
No worries mate..

If you would just explain what your after better, we would get there in under 2 pages ;)
Code_Nerd at 2007-11-11 21:29:17 >
# 29 Re: Finding a decimal
so this long winded thread was all about a cin>>x instead of int x=200; ?
oh dear...
Danny at 2007-11-11 21:30:12 >
# 30 Re: Finding a decimal
so this long winded thread was all about a cin>>x instead of int x=200; ?
oh dear...
I badly wanted to close the thread. :mad:
Nokia2280 at 2007-11-11 21:31:20 >
# 31 Re: Finding a decimal
I can close it but I see no reason for it. Yes, it has deviated from the main topic and digressed into stylistic issues, and yes I believe that the OP's question has been answered about a dozen times, but that doesn't warrant closing a lively thread.
Besides, the comments about the use of globals and void main were certainly notable. Anyway, can we focus on other threads now? I need your energy evenly divided among all threads.
Danny at 2007-11-11 21:32:20 >
# 32 Re: Finding a decimal
so this long winded thread was all about a cin>>x instead of int x=200; ?
oh dear...

Yes it was quite painful Danny.. :(

LOL
Code_Nerd at 2007-11-11 21:33:13 >