2

I'm writing a library for procedural image generation (Clisk) which allows users to define their own mathematical functions to generate images.

It's clearly possible for them to define a function which could result in a divide by zero for some pixels, e.g. (pseudocode)

red = 1.0 / (xposition - 0.5)

This would result in a divide by zero whenever xposition = 0.5 (the middle of the image)

Ideally I don't want image generation to crash... but at the same time I don't want to create a clunky hack to ignore divide by zeros that will cause problems later.

What would be a good, robust, systematic approach to handling these cases?

Aziz Shaikh
  • 16,245
  • 11
  • 62
  • 79
mikera
  • 105,238
  • 25
  • 256
  • 415
  • 4
    Perhaps you could explain a bit more: if the user buggers up their function, why do you particularly care? Why not just make your code catch Exception and re-throw a UserDefinedCodeBuggeredUpException? – Neil Coffey May 23 '12 at 01:59
  • There's a newer question on a similar topic that's been getting some good attention: [What is the fastest integer division supporting division by zero no matter what the result is?](http://stackoverflow.com/questions/16777456) – hippietrail Jun 02 '13 at 05:10

3 Answers3

8

Ideally I don't want image generation to crash... but at the same time I don't want to create a clunky hack to ignore divide by zeros that will cause problems later.

(I'm assuming you mean the snippet to be an example of some user-supplied code ...)

Clearly, if the user-supplied code could throw exceptions, then you can't stop that happening. (And the advice to check before division is obviously irrelevant ... to you.)

So what could you do apart from "crash"? Generate an empty image? Ignore the user's function? You'd be producing garbage ... and that's not what the user needs.

You certainly can't reach in and fix his / her java code. (And if that snippet is meant to be code written in some custom language, then you can't reach in and correct that either. You / your library doesn't know what the user-supplied code should be doing ...)

No. I reckon that the best answer is to wrap any unexpected (unchecked) exceptions coming out of the user-supplied code in an exception of your own that tells the user clearly that the error occurred in his code. It is then up to the application code calling your library code whether to deal with the exception or "crash".


If you are asking for a "good, robust, systematic approach" for users to write their functions, I think you are barking up the wrong tree. And it is not really your concern ...

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

I'm not a graphics programmer really, but you could do

 private static final double MIN_X = 0.0000001

 red = 1.0 / Math.max(xpos - 0.5, MIN_X);

Obviously, you will probably have to drop an absolute value in there if you allow negatives

jeff
  • 4,325
  • 16
  • 27
0

You could always just supply a parameter asking them what to do on divide-by-zero. It's their code, after all - they should know what's best for their case.

Then the question becomes, what's a reasonable default for that parameter? I'd say "return 0.0" or "throw an exception" are both reasonable for this application. Just make sure you document it.

BlueRaja - Danny Pflughoeft
  • 84,206
  • 33
  • 197
  • 283