![]() |
|
#1
|
|||
|
|||
Help with IF-ELSE conditional loopsHello Board,
This really should be a breeze, and I'm almost ashamed of posting this. However, this HAS been bugging me for a day now and I can't seem to solve it, and I need help. THE PROBLEM: I have an array of numbers that goes from 16.0 to 12640.0 in increments of 0.1. i.e [16.0, 16.1, 16.2 .. 12640.0]. This array is called Intermediate , and is of type double . Now, depending on the fractional part of a particular array element, I need to multiply a certain set of array elements with the elements of another array. In other words, if the element in consideration is 16.0, I multiply a certain set of numbers with "Bank0", which is one of 10 filter coefficient banks. If the element is 16.1, I multiply a different set with "Bank1", etc. I've used the fmod function to determine the fractional part, as follows: CPP / C++ / C Code:
This should print out 16.1, 17.1, 18.1 etc... When I tried running the program, after coding the steps as they are meant to be performed, I got the wrong results. I then proceeded to attempt to locate the problem, and I see that the conditional statements for the fractional parts are not working at all, except for [i]if ((fmod(Intermediate,1) == 0) Does the number in the condition HAVE to be an integer? What am I doing wrong? Thanks a lot! |
|
#2
|
||||
|
||||
|
An if statement only understands true or false... zero is false and anything else is true. The if statement will only evaluate to true if the return value of fmod() is equal to 0.1. If that's what you wanted, then in that case the if statment is just fine the way it is. However, I did happen to notice that it is missing a right-parenthesis.
((fmod(Intermediate[i],1) == 0.1) .^where's my brother? __________________
-Aaron |
|
#3
|
|||
|
|||
|
Quote:
Hi Aaron, The missing brother was a typo I need to check what the fractional part of each array element is. How would you suggest I go about it? I declared another *double* variable called Check, and tried to run this code: CPP / C++ / C Code:
This doesn't work either? If the "if" statement checks only true-false conditions, how would you suggest I write a program that checks for the fractional part and executes different statements accordingly? Thanks! |
|
#4
|
||||
|
||||
|
Quote:
__________________
-Aaron |
|
#5
|
|||
|
|||
|
Quote:
Switch accepts only integer arguments. I need to check for the fractional part. Thanks for your help, and it'd be great if you could help me after your class! |
|
#6
|
|||
|
|||
|
there are several ways you could accomplish this, i'll post one of them:
CPP / C++ / C Code:
the problem is that floating point variables are not necessarily returned exactly as 0.1 by fmod. If you try storing the returned value of fmod in a variable, it shows 0.1, however if you try to compare that variable with 0.1 it doesnt work. for e.g. CPP / C++ / C Code:
|
|
#7
|
|||
|
|||
|
Quote:
Thanks Machinated! Will this work for 0.2...0.9 as well, if I try to compare to 2..9. Also, is static_cast<int> C++ specific? I have to write my code in C. Thank you very much for your help! |
|
#8
|
|||
|
|||
|
yes it will work for anything, as long as u apply the right logic to it. and yes static_cast<int> is c++ specific, for c type casts you would jus use parenthesis:
int(fmod(a,1)) or (int)fmod(a,1) |
|
#9
|
|||
|
|||
|
I think it works! Thanks, Machinated. The compiler didn't accept static_cast, but I just removed that and used an (int) cast.
It seems to be working for all the fractional parts. |
|
#10
|
|||
|
|||
|
ur welcome
code on... ![]() |
Recent GIDBlog
Python ebook by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The