Sign in to follow this  

floating point comparisons

This topic is 4249 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 Andreas, More questions relating to floating point calculations I'm afraid. I just can't seem to escape that decimal point.... ;) Is there any feature in angelscript for defining an epsilon for floating point comparisons on particular types? At the moment, I've exposed each of my own utility comparison functions for each typedef that sets an epsilon appropriate for the type... e.g. CompareFlows(flow1, flow2) && ComparePrices(price1, price2) However, this makes my scripts much less terse than the comparison operators, as you can imagine. i.e. if (x < y and y > 20.0) .... becomes.. if (ComparePrices(x, y) < 0 and ComparePrices(y, 20.0) > 0) ... The only solution I've come up with at this stage is to wrap the double typedefs with their own classes and overload the equality operators to hide the floating point comparison complexity from the script users. However, I've been told that enforcing the type safety using these floating point wrappers with overloaded operators is next to impossible because of implicit casting in c++. If angelscript could register an epsilon for a particular double or float typedef, it would pretty cool. Your thoughts? cheers Pratty

Share this post


Link to post
Share on other sites
I can't see of any good way of doing what you want.

I think what you really need is a function to round your floating point values to the wanted precision, something like:

if( round(x, 0.01) < round(y, 0.01) )

That should be easy enough for the script writer to understand, and doesn't cause any confusion as to what is going on, I think.

Regards,
Andreas

Share this post


Link to post
Share on other sites
I had a brainwave on solving this problem without making scripts more complicated for the users but got an angelscript not supported return code (-7 i think).

Here's what I did:

- Created an object type that is a double typedef for each of my types. e.g. MW, cumecs, metres. (using RegisterObjectType with asOBJ_FLOAT)

- Created the operators <, <=, >, >=, ==, !=, for the type, passing pointers to the helper functions that do the **** if (abs(val1 - val2)) <= epsilon **** checking (using RegisterObjectBehaviour)

Unfortunately, it appears that angelscript doesn't yet support the RegisterObjectBehaviour calls for those operators for asOBJ_FLOAT object type.

Share this post


Link to post
Share on other sites
You should be able to register the behaviours for any registered type, independently of the asOBJ_XXX flag.

You're probably trying to register the comparison operators as object behaviours, which is not yet supported. Instead you need to register them as global behaviours (RegisterGlobalBehaviour).

Regards,
Andreas

Share this post


Link to post
Share on other sites

This topic is 4249 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this