Fixed Point Math problem

Started by
5 comments, last by NotAYakk 14 years, 11 months ago
Hi, I've implemented a class to deal with fixed point math. The class has some overloaded constructors which can receive float, int, etc and store the fixed point conversion in a var called value. The code follows:

class Fixed32
{
 public:
  // Constructors

  // Overloaded operators
 private:
   int value;
};

The problem is when I try to use it in my code. Whenever I want to assign it to a variable which is not from type Fixed32 I have to do this: GLfixed a = Fixed32(10).value; when I really wish to do is just: GLfixed a = Fixed32(10); Basically I can play with it around since all my variable in code are from Fixed32 type but when it comes to pass it down to OpenGL I'm force to use the ugly Fixed32(10).value since GLfixed is a typedef to int. Does anyone has any idea how can solve it? Thanks in advance.
Advertisement
I don't think its that ugly, personally, and it is expressing what is actually going on (although if value actually *is* private, then you must have some other similar syntax.)

You have a couple options --

You can add an implicit conversion to your class that converts to an int type, which, IIRC, will create the syntax you want.

The second option, if you only want to create fixed-point values from other types, rather than performing other operations on them (add, multiply, etc), is to get rid of the class approach and simply supply several versions of an overloaded function that builds the fixed-point values. Rather than having this functionality be part of a class, make it part of a namespace instead.

throw table_exception("(? ???)? ? ???");

Quote:Original post by Ravyne
I don't think its that ugly, personally, and it is expressing what is actually going on (although if value actually *is* private, then you must have some other similar syntax.)


I agree. Just leave it like that. It is good to be expressive about what you're doing. Which is the reason why I'd recommend against any implicit conversions.
Using a fixed point class or even namespaced functions is a bit overkill anyway. Just do the fixed point conversion in the code. For constant values like the one in your example, work out the fixed point version and just use that rather than wasting time having the application convert it at run time.
If you really must use some kind of function to make things easier to read then just use a preprocessor macro.
Thank you all for the quickly replies.
I actually need the class since I perform many fixed point operation all over the code ( what I mean is I don't use it just for constants, in that case I really could rely on a simple macro ). In order to do that I overload the necessary operators.

More than a design problem these syntax is getting me confused( sometimes using it directly, e.g, Fixed32 angleDegree = angle * Fixed32(0.57...), while sometimes I must access value for instance vertexBuffer = foo.value ), thus causing semantic errors across my code.

More specifically what I want is to guarantee that every time there's a expression like int = Fixed32, it would make the int variable receive the value of 'value' variable in my class.

Quote:You can add an implicit conversion to your class that converts to an int type


How do I do that? If I got it right that's exactly what I want to do.

Like so:

class Foobar{public:	// Compiler assumes you're returning correct type, therefore	// there is no need for the return value type.	operator OtherType () const 	{ 		return OtherType( /* ... */ );	}}
Add this to the public interface of your class:
operator GLfixed() const {	return value;};

This topic is closed to new replies.

Advertisement