# 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. ## 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. 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. Originally Posted by bassist11
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. Originally Posted by CommonTater
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

5. grumpy... you're right, of course... but that brings the next question... Does he need floating point math for his task?

6. 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...

7. 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. As someone else said, you may want to have a look at fmod: fmod - C++ Reference

9. Originally Posted by bassist11
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.

10. 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. Originally Posted by nonoob
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.

Popular pages Recent additions