Thread: Detect Decimal

  1. #1
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    231

    Detect Decimal

    Hey guys I was wondering If it was possible to detect if a number is a decimal like this 5.5 but not an integer. Thanks for the help (sorry if you expecting a longer question )

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    And how is this number stored? For example, if it's a string, it's pretty simple: if(strchr('.'))
    If you understand what you're doing, you're not learning anything.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    There are several ways to do this...

    For example you can use the abs() function...
    Code:
    float Num = 5.5;
    
    if (abs(Num) != Num)
      puts"It is not a whole number";

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    abs() ?

    Perhaps floor().

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nonoob View Post
    abs() ?

    Perhaps floor().
    You're right... my bad.

  6. #6
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    231
    Thanks for the help, I have started with my programming, it will do synthetic division, but I have ran into a slight problem. To be honest I don't know what is happening but I know its in the while loop and when I step through the program on my debugger it just quits when it gets to the while loop. Here is my code...

    Code:
    #include <stdio.h>
    #include <math.h>
    
    main()
    {
          double test;
          
          int c;
          
          int a;
          
          int divide = 0;
          
          double cdivide;
          
          double pmax;
          
          test = 5.5;
          
          printf("enter c:");
          
          scanf("%d",&c);
          
          printf("\nnow enter a:");
          
          scanf("%d",&a);
          
          pmax = 2 / c;
    
          while(divide <= pmax)
          {
                     cdivide = c / divide;
    
          if( floor(cdivide) != cdivide)
          {
              printf("not an int");
              
          }
          else
          {
              printf("new p found: %d",cdivide);
          
          }
          divide++;
          }
          printf("done");
          getch();
          return 0;
    }
    thanks again

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if( (float)((int) f) != f )
        print( "Houston, we have a decimal.\n" );
    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    modf(3) - Linux man page
    No worry about overflow.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
         while(divide <= pmax)
          {
                     cdivide = c / divide;
    Take a close look at your code... what is the value of the divide variable when it gets to the bit above?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. return decimal number back caller function
    By andy09 in forum C Programming
    Replies: 7
    Last Post: 10-10-2009, 08:10 AM
  2. hex to binary,hex to decimal
    By groovy in forum C Programming
    Replies: 2
    Last Post: 01-25-2006, 02:14 AM
  3. Confused by expression.
    By Hulag in forum C Programming
    Replies: 3
    Last Post: 04-07-2005, 07:52 AM
  4. (C++) How to detect decimal points?
    By jeffcoulter in forum C++ Programming
    Replies: 6
    Last Post: 10-17-2002, 02:36 PM