# messy mathematics

Please take a moment to consider if this thread is worth bumping.

## Recommended Posts

Me again! I wonder, is there a more elegant way of scripting the algebraic equation ... x = y / (K - z)?

The best i can do is that given ...

integer y; // defined as a positive integer elsewhere in the script
integer z; // defined as a positive integer elsewhere in the script
integer K; // defined as a constant positive integer elsewhere in the script such that K >= y + z

... then ...

float x = (float)y / (float)(K - z);

... typecasting the RHS of the equation being (apparently) necessary to avoid z = 0 regardless of the RHS values.

Is it just me thinking that looks too ugly to be as good as it gets? Is there a better way?

##### Share on other sites

Sadly, no. Integer division is going to give you an integer result. (Rounded towards 0.)

One question would be: Do those integers need to be integers?

##### Share on other sites

Thank you Wolfie. I thought what you said probably was the case but felt I had to ask.

As to whether or not the offending variables need to be integers, I suppose the honest answer is no, except insofar as they represent quantities that are naturally integers and need to serve as divisors in only four instances — to yield the temporary percentage-type logical control variable 'x'.

I just hate ugly looking equations, I suppose, but one pays one's money and make one's choice.

Thanks again though. At least now when the project's mathematician complains about me butchering her pretty algebra, I can blame SL 