Need a way to pick out multiples of 10 from a group of numbers

This is a discussion on Need a way to pick out multiples of 10 from a group of numbers within the C Programming forums, part of the General Programming Boards category; So I have a program where I can input a number from 0 to 40 as one variable, and a ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    15

    Need a way to pick out multiples of 10 from a group of numbers

    So I have a program where I can input a number from 0 to 40 as one variable, and a number from 0 to 8000 as another variable.

    Depending on the numbers, there are certain formulas I need to use. If the number for the first variable is multiple of ten(0,10,20, etc), I use one set of equations, and any other number uses a different set of equations. If the number for the second variable is a multiple of a thousand (0,1000,2000, etc.) I use one set of equations and likewise if it's not.

    I'm kind of stuck on a way to pick out those numbers and use the correct formulas.

    I've tried an "if" function followed by an "else if" for the other variable.

    Code:
    if(at = 0.0 || at = 10.0 || at = 20.0 || at = 30.0 || at = 40.0)
    {
            printf("\nEnter the performance values that correspond to the temperature and pressure altitude given. \n");
    	printf("Low alt: ");
    	scanf("%lf", &sdt);
    	printf("High alt: ");
    	scanf("%lf", &ldt);
    						
    	//Calculate distance for even temp
    	nwd = sdt + (paas - la)/(ha - la) * (ldt - sdt);
    }
    else if(paas = 0.0 || paas = 1000.0 || "etc etc" || paas = 8000.0)
    {
    	printf("\nEnter the performance values that correspond to the temperature and pressure altitude given. \n");
    	printf("Low temp: ");
    	scanf("%lf", &sda);
    	printf("High temp: ");
    	scanf("%lf", &lda);
    						
    	//Calculate distance for even alt
    	nwd = sda + (at - lt)/(ht - lt) * (lda - sda);	
    }
    Using this method, I get an "Lvalue required as left operand of assignment" error by the if statements. Both "at" and "paas" are doubles.

    Help is appreciated. I'm a beginner when it comes to programming. This isn't part of a class or anything, I'm just trying to develop a simple little program to do some calculations for me that I do on a regular basis as a pilot.

    Thanks!

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    15
    Alright. Sorry guys. I just realized I forgot to put a double equal sign. I got it working.

    If anyone has a more efficient way to do this though, I would appreciate it.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by bassist11 View Post
    Alright. Sorry guys. I just realized I forgot to put a double equal sign. I got it working.

    If anyone has a more efficient way to do this though, I would appreciate it.
    You can simplify your tests with the % modulus operator...
    Code:
    if ( at % 10 == 0)
    ...
    
    if (paas % 1000 == 0)
    ...
    look it up in your C documentation for more details.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,657
    Quote Originally Posted by CommonTater View Post
    You can simplify your tests with the % modulus operator....
    That doesn't work for floating point variables or values.

    With some care, fmod() in <math.h> might be used.

    Generally, however, if you ever have code that is checking equality of floating point values, one needs to be very careful. One characteristic of any computed floating point value (and "computed" does include "being read from an input stream") is its precision - and a counter-part of precision is, loosely speaking, imprecision. It is not guaranteed than an input of "10.0" will compare equal to the floating point value 10.0
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    grumpy... you're right, of course... but that brings the next question... Does he need floating point math for his task?

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Comparison between exact values and values calculated during floating point calculations is a world of disappointment.

    I remember learning that even something as simple as 0.1f + 0.9f == 1.0f does not work as expected!
    Due to rounding error it'll equal something like e.g. 0.999999624872... or 1.00000035442...
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    15
    Floating point math is preferred with the calculations I'm doing.

    Those two floating variables that I'm checking for equality are fresh off the input line. There is no calculating being done with them between input and checking those conditions. So I shouldn't have to worry about precision at that point?

  8. #8
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    265
    As someone else said, you may want to have a look at fmod: fmod - C++ Reference

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,657
    Quote Originally Posted by bassist11 View Post
    Floating point math is preferred with the calculations I'm doing.

    Those two floating variables that I'm checking for equality are fresh off the input line. There is no calculating being done with them between input and checking those conditions. So I shouldn't have to worry about precision at that point?
    Wrong.

    You may get lucky with some values, but there is no general guarantee with floating point that a value you input will compare as equal to the value you you expect. For example, there is no guarantee that an input of "0.1" will, when converted to floating point, compare as equal to 0.1.

    That is one of many reasons reason why floating point comparisons are generally considered to be very bad medicine - if you habitually rely on such things, you WILL encounter a case where the value does not compare equal to what you expect.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    If the inputted values are integers, their floating point representation is exact by definition.
    The 'if' statements shown by the OP are also comparing these to integers (in effect).
    There will never be a problem.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by nonoob View Post
    If the inputted values are integers, their floating point representation is exact by definition.
    The 'if' statements shown by the OP are also comparing these to integers (in effect).
    There will never be a problem.
    That's only mostly true.
    Unfortunately since there are only 23 bits in an IEEE754 float's significant, integers outside the range of -16777216 to +16777216 cannot all be represented exactly.
    For double's you're fine there, however there's also the possiblity that the target system isn't using IEEE754 for its floating point.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sum letters in written numbers from 1 to 2008
    By Desolation in forum C++ Programming
    Replies: 12
    Last Post: 04-26-2010, 03:18 PM
  2. Heaps...
    By Nutshell in forum C Programming
    Replies: 14
    Last Post: 04-23-2002, 09:54 AM
  3. Formatting Output
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 03-26-2002, 01:33 AM
  4. Homework help
    By Jigsaw in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 05:56 PM
  5. Replies: 7
    Last Post: 01-02-2002, 09:16 PM

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