Sign in to follow this  

Integer Division

This topic is 1198 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

The majority of my users write scripts to implement custom mathematical functionality.   It is very common for my users to write expressions with integer literals and assume the math will be floating-point.  For example

 

double f(double x)

{

       return x**(2/3) + x**2 + 1;

}

 

instead of

 

double f(double x)

{

       return x**(2.0/3.0) + x**2 + 1;

}

 

Most don’t have any idea what integer division is.  Those with any programming background tend to have experience with Visual Basic or Matlab, both of which always use floating-point division.  I find myself correcting this common mistake frequently, and I only expect it to get worse as my user base grows.

 

I hesitate to ask, because it would be a departure from C++ behavior, but could AngelScript adopt an Engine Behavior option that would treat the / and /= operators as floating-point division?  We could introduce \ and \= operators to perform integer division (like Visual Basic and Python 3).  I suppose even with the engine option disabled the \ and \= operators could still be of some use.

 

a \ b; // int(a) / int(b)

 

Let me know what you think, and let me know if you have any alternative suggestions.  This is a very common mistake that my users make, so I need to do something about it.  Of course, if you agree to the Engine Behavior option, I would provide the implementation.

 

Thank you for your consideration,

Jason

Share this post


Link to post
Share on other sites

Just a quick thought, but you could, instead of using the atomic POD types, use a set of boxing types like Java does. You would end up with a bunch of classes (Integer, Double, Float, ...) and overload their arithmetic operators.

 

I know modifications like this might be a big thing for ongoing projects, but I think it might be a - more or less - elegant approach to deal with your problem.

Share this post


Link to post
Share on other sites

I think the use of an engine property to treat the result of / and /= as float/double would be acceptable, as long as the impact in the code is relatively isolated (which ought to be possible).

 

I don't see the need for an additional operator to force integer division in this case, as the user can explicitly convert the result to integer with int(expr) if desired. Explicit conversions would be more readable than have a backslash or double-slash as integer division in my opinion.

Share this post


Link to post
Share on other sites

I agree that explicit conversions would be more readable than a backslash.  The only issue with int(expr) would be that not all large, 64-bit numbers convert to doubles without loss, so the result of a floating-point division followed by truncation would not necessarily be the same as integer division.  I'm not too concerned about that though, because I can always create some sort of "idivide" function in my application. 

Edited by Jason Goepel

Share this post


Link to post
Share on other sites

This topic is 1198 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