Jump to content
  • Advertisement
Sign in to follow this  
felipedrl

Fixed Point Math problem

This topic is 3414 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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( /* ... */ );
}
}




Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!