Thread: question (floating numbers)

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    33

    question (floating numbers)

    for example if i input 2 floating numbers

    number 1 : 5.7
    number 2: 7.3

    and i want to comapre them BUT i only want to comapre fractional part

    so number 1 would be bigger than number 2 because fraction of number 1 is bigger. (7 > 3)

    so My question is...how do i compare fractions only? Would formating the number work? If after inputing a number 1 i formated it to .7 ...would it work like that?

  2. #2
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    I think modf is standard in math.h, this should split the float, you'd then be able to do your comparison.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You can use the modf() function to give you the fractional part.
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(void)
    {
       double a = 5.7;
       double b = 7.3;
       double ipart;
    
       if (modf(a,&ipart) > modf(b,&ipart))
          printf("fractional part of a is greater than b.\n");
       return 0;
    }

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    4
    Hi Mak
    The answer to your question is NO, formating had nothing to
    do with the internal representation of the numbers, You see, formating only afects how WE humans see the numbers, internaly they are someting else,

    If you want to compare the fractions, you had to isolate them,
    all you had to do is substract the integer part, and compare them

    hope this was useful

    Saludos desde Mexico

    Chuky

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Converting from a floating point value to an integral value the fractional part is discarded. So if you subtract off the integral portion, you would be left with the fractional part. So, here is one way.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       double n1 = 5.7;
       double n2 = 7.3;
       int    i1 = (int)n1;
       int    i2 = (int)n2;
       double f1 = n1 - i1;
       double f2 = n2 - i2;
       if(f1 > f2)
       {
          printf("%g > %g\n", f1, f2);
       }
       return 0;
    }
    
    /* my output
    0.7 > 0.3
    */
    [edit]Damn I was poky on this one!
    Last edited by Dave_Sinkula; 10-14-2003 at 07:31 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    33
    Originally posted by swoopy
    You can use the modf() function to give you the fractional part.
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(void)
    {
       double a = 5.7;
       double b = 7.3;
       double ipart;
    
       if (modf(a,&ipart) > modf(b,&ipart))
          printf("fractional part of a is greater than b.\n");
       return 0;
    }
    why is the "ipart" there?

    and if i try using modf...i get an error:

    warning: passing arg 2 of `modf' from incompatible pointer type

  7. #7
    Registered User
    Join Date
    Oct 2003
    Posts
    33
    and does it matter if i use float insted of double?

  8. #8
    Registered User
    Join Date
    Oct 2003
    Posts
    33
    okay...here is my first try:


    Code:
    #include <stdio.h>
    
    
    int main(void){
    
    float num;
    float num2;
    int i1, i2;
    
    
    printf("Please enter a float number : ");
    scanf("%f",num);      /*GETS FIRST FLOAT NUMBER */
    
    while (num !=0)     /*MAKES SURE ITS NOT 0 */
    {
    printf("Please enter a float number : ");
    scanf("%f",num2);   /*GETS SECOND FLOAT NUMBER */
    
     /*turns it into fractions only*/
    
        i1 = (int)num;
         i2 = (int)num2;
       num = num - i1;
       num2 = num2 - i2;
       
        /*compares and prints out the bigger one*/
      if(num>num2)
      {
      printf("The number is %f.\n",num);
      num=num2;
      }
       if(num2>num)
      {
      printf("The number is %f.\n",num2);
      num2=num;
      }
      
      }
      printf("DONE");
      }

    now for some reason that i cant understand i get "Access violation" error right after i enter first float number ....

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >scanf("%f",num);
    Code:
    scanf("%f",&num);
    And my usual references here and here.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered User
    Join Date
    Oct 2003
    Posts
    33
    okay...it works now.

    I just need to work on the logical part now.....it does not work as it should.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 10-08-2004, 05:30 AM
  2. floating point question
    By Eric Cheong in forum C Programming
    Replies: 8
    Last Post: 09-10-2004, 10:48 PM
  3. floating point numbers display something wrong i think
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 04-27-2002, 03:18 AM
  4. Floating point numbers
    By bulsquare in forum C Programming
    Replies: 2
    Last Post: 04-10-2002, 04:44 AM
  5. Very large floating point numbers
    By bulsquare in forum C Programming
    Replies: 0
    Last Post: 04-08-2002, 04:10 PM