7 Replies - 1506 Views - Last Post: 04 December 2009 - 09:50 AM Rate Topic: -----

#1 sarastrong   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-December 09

FLOATING NUMBER

Posted 04 December 2009 - 07:27 AM

how can we find if a floating no is even , i used modulus , but i keep getting an error that says "error C2296: '%' : illegal, left operand has type 'float'".
I must find first if the floating no is even then extract the integer part and fraction part.

heres my program!!!

#include <iostream>
#include <iomanip>
using namespace std;

bool is_positive(float);
void extract_int_float(float &,float &,float &);
int main()
{float x;
float i,f;

bool found;
cout<<"Enter a floating no with exactly 3 no after decimal pt";
cin >>x;
found=is_positive(x);
if (found==true)
cout<< " It is a positive no"<<endl;
extract_int_float( x,i,f);
cout<<"the int part is "<<i<<endl;
cout<<"the fraction part is  "<<f<<endl;
return 0;
}



bool is_positive(float x)
{  if(x%2==0)                              ------------------------>this part
		return true;
	else return false;
}

void extract_int_float(float& x, float & i,float & f)
{ 
	i=x/1;
         f=(x-i)*1000;
}


*** MOD EDIT: Added code tags. Please :code: ***

This post has been edited by JackOfAllTrades: 04 December 2009 - 07:33 AM


Is This A Good Question/Topic? 0
  • +

Replies To: FLOATING NUMBER

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: FLOATING NUMBER

Posted 04 December 2009 - 07:35 AM

Uh...isn't a number defined to be positive if it's greater than 0?

EDIT: Also, given that -- as you've now discovered -- there is no modulus operation for a float variable, perhaps the order in which you're doing this:

Quote

I must find first if the floating no is even then extract the integer part and fraction part.

needs to be reconsidered?

This post has been edited by JackOfAllTrades: 04 December 2009 - 07:39 AM

Was This Post Helpful? 0
  • +
  • -

#3 sarastrong   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-December 09

Re: FLOATING NUMBER

Posted 04 December 2009 - 07:49 AM

wow..!!!!...
sorry i've been working on several programmin exercises that i can't even think/read properly.......
and yea u r right!!!!....i gota find if it is positive ,not even.!!

thank you ....ummm for making me read the question again...

but since there's no modulus operation for float variable,
what should i do if i wanted to find out a float variable is even!!!!is there a way??
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: FLOATING NUMBER

Posted 04 December 2009 - 08:31 AM

There is actually a Mod function for floating point numbers. Strange as that may seem. The function is in cmath and is fmod() (not to be confused with modf())

fmod with determine the remainder of a division just like % but using floating point numbers.

If you want to tell if a floating point number is even or not you have to kind of define what you mean by "even" -- if you mean "properly divisible by 2" then fmod can help, but there will be a margin of error (we are talking about floating point values here).

so is 2.24 an even number? What about 2.1? or 3.2? Generally the concept of even and odd is reserved for integers.
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: FLOATING NUMBER

Posted 04 December 2009 - 08:34 AM

Heh...shows you how often I use floating point numbers :P
Was This Post Helpful? 0
  • +
  • -

#6 sarastrong   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-December 09

Re: FLOATING NUMBER

Posted 04 December 2009 - 09:01 AM

thnx =D

@nickdmax: well i think i got the fmod() explanation...ill try it out!!! ..thnx again!!

@jackofalltraders: yup!!!! u spotted it right away!!!! really thnx!!
programming uses much of the glucose in my body =/ =/ !!!n i lose concentration!!!!
Was This Post Helpful? 0
  • +
  • -

#7 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3213
  • View blog
  • Posts: 19,241
  • Joined: 14-September 07

Re: FLOATING NUMBER

Posted 04 December 2009 - 09:11 AM

Quote

so is 2.24 an even number? What about 2.1? or 3.2? Generally the concept of even and odd is reserved for integers.



Is there an actual ruling on that? I was under the impression that it only applies to integers. I wouldn't be surprised if there was some weird qualifier for it.
Was This Post Helpful? 0
  • +
  • -

#8 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: FLOATING NUMBER

Posted 04 December 2009 - 09:50 AM

I have only ever seen even/odd parity defined for integers. I do not believe that real numbers or complex numbers or quaternions have a concept of even or odd...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1