DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 3 123 LastLast
Results 1 to 15 of 33

Thread: Finding a decimal

  1. #1
    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. #2
    Join Date
    Aug 2005
    Location
    Melbourne...Australia
    Posts
    279
    Have you got anything so far?
    I just wrote one in one line?

  3. #3
    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. #4
    Join Date
    Jan 2006
    Posts
    55

  5. #5
    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. #6
    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. #7
    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. #8
    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. #9
    Join Date
    Jan 2006
    Posts
    55
    Quote 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. #10
    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. #11
    Join Date
    Aug 2005
    Location
    Melbourne...Australia
    Posts
    279
    Quote 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. #12
    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. #13
    Join Date
    Jan 2006
    Posts
    55
    Quote 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. #14
    Join Date
    Aug 2005
    Location
    Melbourne...Australia
    Posts
    279
    Code:
    void main()
    ***??? Very bad.. :(

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

    You dont send it there in any way?

  15. #15
    Join Date
    Feb 2006
    Posts
    25
    durr woops

Similar Threads

  1. Replies: 2
    Last Post: 09-27-2006, 07:29 PM
  2. c# - Decimal conversion
    By cema in forum .NET
    Replies: 2
    Last Post: 01-18-2006, 09:00 PM
  3. Conversion of decimal in Oracle to Access 2000
    By Villy Olsson in forum Database
    Replies: 4
    Last Post: 04-06-2002, 09:01 PM
  4. Replies: 2
    Last Post: 09-26-2001, 06:08 PM
  5. Textbox for Decimal Number
    By Dante in forum VB Classic
    Replies: 2
    Last Post: 06-23-2000, 02:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center