floating point comparisons

Started by
3 comments, last by pratty 17 years, 11 months ago
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
Advertisement
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

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

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.
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

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

indeed I am, thanks Andreas!

This topic is closed to new replies.

Advertisement