Thread: Calculating a floating point, but not by if's & elseif's ?

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    2

    Question Calculating a floating point, but not by if's & elseif's ?

    This is the code, first of all, all variables are floats and swingTolerance has a value of 40:

    Code:
         
                    swing = AngleSubtract( destination, *angle );
                    scale = fabs( swing );
    
    		if ( scale < swingTolerance * 0.01 ) {
    			scale = 0.01;
    		} else if ( scale < swingTolerance * 0.02 ) {
    			scale = 0.02;
    		} else if ( scale < swingTolerance * 0.03 ) {
    			scale = 0.03;
    		} else if ( scale < swingTolerance * 0.04 ) {
    			scale = 0.04;
    		} else if ( scale < swingTolerance * 0.05 ) {
    			scale = 0.05;
    
                    (else if's continue into higher numbers 1.20 ...1.21 ...1.22 ect)
    
    		} else {
    			scale = 0.1;
                    }
    AngleSubtract() is a function for finding difference between angles, and fabs() finds absolute value.

    I'm fairly new to coding and wanted to know if there was a way to calculate the scale variable with floating point precision rather than this sequence of if's and elseif's (where the floating point precision is lost). Here is one attempt...

    Code:
    	swing = AngleSubtract( destination, *angle );
    	scale = fabs( swing );
    	
    	scale = swingTolerance *= .5 + scale;
    ...where I tried to set up the code to find the scale value on its own with floating point precision for each step/code pass/cycle. This didn't work at all. The problem is that I'm new to programming and I don't know how to arrange the different variables so scale can be calculated with one line or statement, rather than through a series of if's and elseif's. I know there must some simple, logical way to get this working. The trick must have something to do with the code being able to read the scale variable before it is modified each step/code pass/cycle.

    I can provide more information if necesary. This project uses no external libraries either, so I know the solution needs to be self contained in this code and can't rely on any premade functions from C#, .Net, or OS specific libraries. Any help would be greatly appreciated.
    Last edited by Locrian; 08-12-2010 at 02:34 PM. Reason: code correction

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    What about this:
    Code:
    swing = AngleSubtract( destination, *angle );
    scale = fabs( swing ) / swingTolerance;
    I'm not sure what range of values you'll be using, but this worked for a few data points that I tried.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    2

    Thumbs up

    Thank you pianorain! This worked perfectly. I knew it had to be something simple that I was overlooking. Again, thanks!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Decimal places on Floating point number
    By manutdfan in forum C Programming
    Replies: 1
    Last Post: 10-29-2006, 12:56 PM
  2. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  3. Floating point exceptions
    By Boris in forum C++ Programming
    Replies: 8
    Last Post: 11-19-2001, 08:32 AM
  4. Floating point faster than fixed-point
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-08-2001, 11:34 PM
  5. Replies: 2
    Last Post: 09-10-2001, 12:00 PM