GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 21-Apr-2004, 12:53
hellhammer hellhammer is offline
New Member
 
Join Date: Mar 2004
Posts: 25
hellhammer is on a distinguished road

Help with IF-ELSE conditional loops


Hello 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:
for ( i = 0; i < 100; i++ )					
	{
		if ((fmod(Intermediate[i],1) == 0.1)
		{
		  printf("%f", Intermediate[i];
		}
	}

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  
Old 21-Apr-2004, 12:58
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
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  
Old 21-Apr-2004, 13:15
hellhammer hellhammer is offline
New Member
 
Join Date: Mar 2004
Posts: 25
hellhammer is on a distinguished road
Quote:
Originally Posted by aaroncohn
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?

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:
for ( i = 0; i < 100; i++ )					
{
	Check = fmod(Intermediate[i],1);
 
        if (Check == 0.1)
	{
	   printf("%f \n", Intermediate[i]);
        }
}

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  
Old 21-Apr-2004, 13:19
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Quote:
Originally Posted by hellhammer
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?
Well, the addition of the Check variable did absolutely nothing. The code does the exact same thing, except now there's another variable. You could use a switch-statement. But hey, I've got to go to class right now. I'll be back in about an hour, so I'll help you out some more then.
__________________
-Aaron
  #5  
Old 21-Apr-2004, 13:36
hellhammer hellhammer is offline
New Member
 
Join Date: Mar 2004
Posts: 25
hellhammer is on a distinguished road
Quote:
Originally Posted by aaroncohn
Well, the addition of the Check variable did absolutely nothing. The code does the exact same thing, except now there's another variable. You could use a switch-statement. But hey, I've got to go to class right now. I'll be back in about an hour, so I'll help you out some more then.

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  
Old 21-Apr-2004, 14:11
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
there are several ways you could accomplish this, i'll post one of them:
CPP / C++ / C Code:
int main ()
{
  
	double Intermediate[22] = {0};
	Intermediate[0] = 16.0;

	for(int i=1;i<22;i++)
		Intermediate[i] = Intermediate[i-1]+0.1;

	for (i = 0; i < 22; i++ )         
		if(static_cast<int>(fmod(Intermediate[i],1)*10) == 1)
			cout<<Intermediate[i]<<" ";  //16.1 17.1 18.1

	return 0;
}

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:
double a = 16.1;
double b = fmod(a,1);
cout<<b; //this will display 0.1
if(b==0.1) //this won't work because b might store it as 0.1000 even
       //though when u display b, it will show 0.1 because of set precision
cout<<a;
so one of the solutions is to multiply the result of fmod by 10 which will always convert 0.1 to 1.0 and then cast it as integer which will turn it into 1 and then compare it to 1. Since integers are always stored without decimals you won't have a problem.
  #7  
Old 21-Apr-2004, 14:20
hellhammer hellhammer is offline
New Member
 
Join Date: Mar 2004
Posts: 25
hellhammer is on a distinguished road
Quote:
Originally Posted by machinated
there are several ways you could accomplish this, i'll post one of them:
CPP / C++ / C Code:
int main ()
{
  
	double Intermediate[22] = {0};
	Intermediate[0] = 16.0;

	for(int i=1;i<22;i++)
		Intermediate[i] = Intermediate[i-1]+0.1;

	for (i = 0; i < 22; i++ )         
		if(static_cast<int>(fmod(Intermediate[i],1)*10) == 1)
			cout<<Intermediate[i]<<" ";  //16.1 17.1 18.1

	return 0;
}

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:
double a = 16.1;
double b = fmod(a,1);
cout<<b; //this will display 0.1
if(b==0.1) //this won't work because b might store it as 0.1000 even
       //though when u display b, it will show 0.1 because of set precision
cout<<a;
so one of the solutions is to multiply the result of fmod by 10 which will always convert 0.1 to 1.0 and then cast it as integer which will turn it into 1 and then compare it to 1. Since integers are always stored without decimals you won't have a problem.

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  
Old 21-Apr-2004, 14:24
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
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  
Old 21-Apr-2004, 14:25
hellhammer hellhammer is offline
New Member
 
Join Date: Mar 2004
Posts: 25
hellhammer is on a distinguished road
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  
Old 21-Apr-2004, 14:28
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
ur welcome

code on...
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 02:46.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.