5

I like converting old BASIC games - I ran across one that had this odd formula. Currently I am writing in Pascal, but I can write it in any language. After rummaging through the code, I could not find if this var in use, but still would like to know what kind of math shortcut BASIC was using back in the day.

d1 = 1-(( 0.23 + random / 10 ) * (-(d <= 50 )))

d1 is a dummy var, d = depth of sub

I broke it down into steps and found that part (-(d <= 50)) causes my compile to fail.

Can someone shed some light on to it?

Klyxmaster
  • 53
  • 2

1 Answers1

4

-(d <= 50) should, AFAIK (boolean -> int conversion), return -1 if d <= 50 and 0 if d > 50. To sum up, if d > 50, the right part of multiplication will be equal to 0, so d1 will be equal to 1. You should write it using else or ternary construct (C-like pseudocode below):

d1 = (d > 50) ? 1 : 1.23 + random / 10;

Step by step explanation:

d1 = 1-(( 0.23 + random / 10 ) * (-(d <= 50 )))

then

if ( d <= 50 )
  d1 = 1-(( 0.23 + random / 10 ) * (-TRUE)))
else
  d1 = 1-(( 0.23 + random / 10 ) * (-FALSE)))

then

if ( d <= 50 )
  d1 = 1-(( 0.23 + random / 10 ) * (-1)))
else
  d1 = 1-(( 0.23 + random / 10 ) * (-0)))

then

if ( d <= 50 )
  d1 = 1 - (( 0.23 + random / 10 ) * -1))
else
  d1 = 1 - (( 0.23 + random / 10 ) * 0))

then

if ( d <= 50 )
  d1 = 1 - (-( 0.23 + random / 10 ))
else
  d1 = 1 - (0)

then

if ( d <= 50 )
  d1 = 1 + ( 0.23 + random / 10 );
else
  d1 = 1;

then, finally

d1 = (d > 50) ? 1 : 1.23 + random / 10;
  • TYVM!! That makes sense. – Klyxmaster Jul 05 '14 at 23:11
  • done thanks again - never too old to keep learning new ways to program heheh (I have a softspot for pascal) – Klyxmaster Jul 05 '14 at 23:40
  • @user3808564 as a side note, I'd strongly discourage using this kind of "Kronecker delta" in code, as it not only obfuscates the code (as you already noticed), but also decreases its speed (unnecessary evaluation of constant expression in one case and additional unnecessary floating point multiplication in the other). Sad thing is, I've seen this "hack" numerous times in "serious" code (particle potential calculators in LAMMPS project for example). –  Jul 05 '14 at 23:45
  • this is old late 70's early 80's programing that i'm converting :-) I agree wholeheartedly. due to memory restrictions back in the day, "trick" programing like this was almost required. Try writing a game with 1k or 2k of ram :-) – Klyxmaster Jul 06 '14 at 23:30
  • @Klyxmaster honestly, since it could be done in a single "if" without increasing complexity nor memory footprint., I'd just say it was a premature-op kludge, not a thing done out of necessity... BASIC apps were usually written by beginners, not professionals. I'm coding for microcontrollers for some time and I'm using C++ for that - I cross-compile to assembly for double-checking the code for efficiency & optimalization possibilities, and there's seldom a thing I could've written better than it already is, by the C++ compiler itself (and I'm using GNU, not Intel - the "lesser" one, mind me)... –  Jul 07 '14 at 11:56