Context adaptive numeric input for functions

When reusing functions coded long ago, I often ask myself "does my function take in numbers in this unit or that unit?" Different units can be used to express the quantities of the same type of object. And some pairs of numbers and units are easier to remember than other pairs for example:
  • half second vs 22050 samples (audio)
  • 10 seconds vs 300 frames  (video)
  • 220 instead of 1048576 (sizes that could be FFT'ed easier)
Most of the time the number will only make useful sense when it's associated with a particular unit. For example you would immediately know that 22050 seconds would be ridiculous in an audio application's context. Hence, I started to write "smart" functions that guess the proper unit based on the magnitude of the input and the normally used magnitudes and reasonable thresholds. For example:

function fftSomething(data, size)

if(size < 20)

       size = 2size

end if 

bla bla bla...

end function ffTSomething

Of course, I could have done a unit conversion before using the function. It would also be more difficult to use unusual numeric values. Things could go wrong if someone doesn't know that the function is doing this. But as for my personal programming, wherein I only deliver the results and not the code, where saving in thinking time is preferred over strictness and efficiency, where I know what typical values are used, this style works for me.

1 comments:

Hdpcgames said...

Gta san andreas Hdpcgames was published in October 2004 for the PlayStation 2, then in June 2005 for Microsoft Windows and Xbox. The game is set in an open-world setting where players may travel and engage in their leisure time, and it tells the narrative of former mobster Carl “CJ” Johnson, who was pulled home by his mother’s death, merely to participate. A lengthy voyage took him across the imaginary state of San Andreas, which is based mostly on California and Nevada.

Post a Comment