Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

C++ compiler error *not solved!*


  • Please log in to reply

#1
magnus80a

magnus80a

    Member

  • Member
  • PipPip
  • 10 posts
Hi again!

Got another C++ problem. I keep getting this error message, but I don't know what I'm doing wrong?

.\Interval.cpp(34) : error C2662: 'Interval::sign' : cannot convert 'this' pointer from 'const Interval' to 'Interval &'

It's referring to this functions:

bool Interval::hasZero() const {
	if (sign(lo) != sign(hi))  <--- *this line*
		return true;
	else if ((lo == 0.0) || (hi == 0.0))
		return true;
	else
		return false;
}

"lo" and "hi" are (double) attributes of Interval.
The function sign() is not a member function, merely a support function to make the code easier to read.

int sign(double a) {
	if (a < 0.0)
		return -1;
	else
		return 1;   
}

The above mentioned functions are both located in Interval.cpp

Edited by magnus80a, 28 December 2005 - 03:08 PM.

  • 0

Advertisements


#2
khayyam

khayyam

    New Member

  • Member
  • Pip
  • 5 posts
why did you declare your method as const ?
try without.
  • 0

#3
Swandog46

Swandog46

    Malware Expert

  • Member
  • PipPipPipPip
  • 1,026 posts
  • MVP
This is also going to give *huge* problems anyway because of the way C/C++ stores floats (doubles). Remember that floating-point arithmetic is only accurate up to a "precision" or "margin-of-error", so that even if you think you might have a number, say, equalling zero, it might actually internally be implemented as 0.00000000000001928357 (whatever, some garbage after a certain decimal place). So when you run your "sign" function you will get unexpected results. Perhaps there is a built-in function for retrieving floating-point sign that might be better? Moreover, these tests will pretty-much ALWAYS fail:

lo == 0.0

because lo will rarely ever be EXACTLY 0.0 ; rather, even when you THINK lo is zero it will really be 0.00000000000000001 , etc. So you might want to rethink this...
  • 0

#4
khayyam

khayyam

    New Member

  • Member
  • Pip
  • 5 posts
you're right about the doubles and the problems to compare them.

but your sign function returns int, so, in this case, no problem, you'll get exactly 1 or -1
that's why the test sign(lo) != sign(hi) will be ok

the often used solution to compare doubles is to compare their difference, for example
if ((lo == 0.0) becomes if (fabs(l0) < EPSILON) with stg like #define EPSILON .0001

and the value given to EPSILON will depend of the type double or float. read the c++ norm and you'll find how many numbers are guaranteed to be exact and you'll know if you have to declare EPSILON as .0001 or .000001 or stg else.
  • 0

#5
magnus80a

magnus80a

    Member

  • Topic Starter
  • Member
  • PipPip
  • 10 posts

why did you declare your method as const ?
try without.


I thought that as long as a function doesn't change anything within the class it's consdiered a constant function?
Regarding the double comparisons, you're right I've forgotten about floating point stuff. I'll try and se if I can compute my machine eps. If there is one I might as well declare that as machine eps.

*edit*

Tried removing the constant part, but it still complains. It looks like it doesn't like non constant function being used by constant functions either. The error message is the same as before.

Interval Interval::operator *(const Interval& obj) const {
	if ((hi < 0.0) && (obj.lo > 0.0))
		return Interval(lo*obj.hi, hi*obj.lo);
	else if (hasZero() && (obj.lo > 0.0)) *<--- this line*
		return Interval(lo*obj.hi, hi*obj.hi);
	else if ((lo > 0.0) && (obj.lo > 0.0))
		return Interval(lo*obj.lo, hi*obj.hi);
	else if ((hi < 0.0) && (obj.hasZero())) *<--- this line*
		return Interval(lo*obj.hi, lo*obj.lo);
	else if ((lo > 0.0) && (obj.hasZero())) *<--- this line*
		return Interval(hi*obj.lo, hi*obj.hi);
	else if ((hi < 0.0) && (obj.hi < 0.0))
		return Interval(hi*obj.hi, lo*obj.lo);
	else if ((hasZero()) && (obj.hi < 0.0)) *<--- this line*
		return Interval(hi*obj.lo, lo*obj.lo);
	else if ((lo > 0.0) && (obj.hi < 0.0))
		return Interval(hi*obj.lo, lo*obj.hi);
	else {
		return Interval(min(min(lo*obj.lo, lo*obj.hi), min(hi*obj.lo, hi*obj.hi)),  *<--- this line*
						max(max(lo*obj.lo, lo*obj.hi), max(hi*obj.lo, hi*obj.hi)));	*<--- this line*
	}		
}
double min(double a, double b) {
	if (a <= b)
		return a;
	else
		return b;
}

double max(double a, double b) {
	if (a <= b)
		return b;
	else
		return a;
}

Edited by magnus80a, 26 December 2005 - 04:19 AM.

  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP