Where's my program's problem? (Floating point exception?)

This is a discussion on Where's my program's problem? (Floating point exception?) within the C Programming forums, part of the General Programming Boards category; Hey guys, I've got the program to compile correctly, but every time I execute, it gives me the line says ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    11

    Arrow Where's my program's problem? (Floating point exception?)

    Hey guys, I've got the program to compile correctly, but every time I execute, it gives me the line says "Floating point exception"? Maybe my logic isn't correct or I code somewhere wrong?


    *EDIT: Well, I see the problem is the denominator can't be zero, and I correct that...and it runs now...
    But the output is not correct because it doesn't choose the smallest denomintor


    The programming I'm trying to do is to let the user input a nonnegative real number,
    and output a fraction the closest to the input, and print out the error also.
    [Error is the difference between input and output]

    The denominator has to be <= 100, and it also prints out the smallest denominator, like 0.751 are 3/4, 6/8,... 75/100, but it chooses 3/4 to print out...

    For example: My input is 0.751 -> printout 3/4, error = -00.100
    another example: input is 42.7488 -> printout 171/4, error = 0.00120

    Here's my code:


    Code:
    #include <stdio.h>
    
    int main() {
        
        //Define data types
        
        int d, n, num, lown, lowd;
        double dec, error_sq, sm_err=1, sm_err_sq;
        
        //Print this line on screen
        printf("Nonnegative real number: ");
        
        //Scan user input
        scanf("%d.%lf", &num, &dec);
        
        //Using for loop, create "d"inomirator and "n"umerator part
        //to find the closest approx.
        
        for(d=1; d<=100; d++){
                 for(n=0; n<=100; n++){
                          
            
        //In order to compare the the error
        //I have to square them both too, to make it both in positive values
        //Then compare them two
                      
                          sm_err_sq = sm_err * sm_err;
                          
                          error_sq = (dec - n/d)*(dec - n/d);
                          
                          //Using IF condition to sort the smallest error each time looping
                          if (error_sq<sm_err_sq){
                                                    lown = n;
                                                    lowd = d;
                                                    
                                                    sm_err = dec - n/d;
                          }
                 }
        }
                                      
        
        //Print the desire value
        
        printf("Closest approximation = %d/%d, Error = %lf\n", num*d+n, d, sm_err);
     
     
     
     
     return 0;   
    }

    Thanks for helping out. =]
    Last edited by readytogo; 10-28-2010 at 03:35 PM.

  2. #2
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    error_sq = (dec - n/d)*(dec - n/d);
    What happens on the first loop? d is set to 0...
    Consider this post signed

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    2,613
    Suggest using abs or fabs instead of square funtion.

    abs - C/C++ Function Reference

    http://www.cplusplus.com/reference/clibrary/cmath/fabs/


    What do you think the value of 3/4 is? It returns an integer not a float.

    Tim S.
    Last edited by stahta01; 10-28-2010 at 03:55 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-12-2010, 01:55 PM
  2. floating point number comparison
    By lehe in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2009, 07:11 PM
  3. Problem aligning floating point numbers
    By esbo in forum C Programming
    Replies: 4
    Last Post: 01-05-2009, 07:09 PM
  4. Floating point exceptions
    By Boris in forum C++ Programming
    Replies: 8
    Last Post: 11-19-2001, 07:32 AM
  5. Floating point faster than fixed-point
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-08-2001, 10:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21