15

So I was porting one of the old BASIC games from the book "BASIC Computer Games", Ref: https://github.com/coding-horror/basic-computer-games to Python, just for fun.

I came across the following expression in the "Fur Trader" game, which the book says works in Microsoft Basic circa 1978 (Ref: Page IX, "BASIC Computer Games"):

LET E1=INT((.15*RND(1)+.95)*10^2+.5)/10^2

It's been a "few" years since I did much BASIC. I am puzzled by the use of 10^2.

Is there any particular reason why the author may have chosen this syntax? Why not just use the constant 100. It seems a weird choice for a book targeted at beginners.

According to the (current) Basic manual, the operator precedence for ^ is the highest. Was this always the case?

This makes my python version look like:

int( ( 0.15 * random.random() + 0.95 ) * 100 + 0.5 ) / 100

Is it correct? My interest is getting same result as the original expression, not in preserving the actual expression.

5
  • 7
    In my experience, these mass-market programming books from the 1970s and 1980s with type-in listings are chock full of nonsensical or downright bad programming practices, and even the occasional bona fide bug. Using 10^2 instead of a saner 100 is pretty much par for the course, and I wouldn't attribute any special meaning or intention to it.
    – Psychonaut
    Oct 10 at 10:31
  • 6
    re precedence of ^ -- yes, it was always thus. DTSS BASIC, 1964, pages 8 and 9. The desire is to have '-X^2' mean '-(X^2)' and not '(-X)^2'. The latter is more often the case. Oct 10 at 13:42
  • 2
    My hazy recollection is that even the very old versions of basic had some kind of automatic conversion back and forth between integers and floating point values, if nothing else when assigned to or retrieved from an integer variable or similar. I wonder if, on at least some systems, 100 would be treated as an integer, and thus use integer division with remainder discarded whereas 10^2 would be treated as floating point and force the expression evaluator to promote the recently demoted integer back to a floating point value... or if the author of the code feared that might be the case...
    – Steve
    Oct 11 at 10:13
  • 1
    The "very old versions of BASIC" (in the 1960s) did not have integer variables. Oct 11 at 19:43
  • 1
    As already answered, 2 is a parameter in the original formula, and one could have generalised it by having "N" there. Generally, in those times, it was more common to write in a "scientific" manner, writing out formulae as they were "supposed to be" mathematically. It may be "bad programming practices" according to @Psychonaut, but it may be "good math" at the same time. Programs, after all, are just applied mathematics. Even games :)
    – Zeus
    Oct 14 at 0:00
20

If f is a floating-point number and n is a non-negative integer, then the expression

INT(f*10^n + 0.5)/10^n

rounds f to n digits of precision after the decimal point, rounding up or down depending on what's closest. For comparison, the expression INT(f*10^n)/10^n rounds f downward.

The Python equivalent is

round(f, n)

so you can translate the code from the game as

round(0.15 * random.random() + 0.95, 2)
21

They likely translated some formula literally where the 10 was actually some variable, but in their use case, it was fixed at 10. For your purposes you can safely use 100.

4
  • 10
    Yes, I agree, More likely, it was the 2 that was changed in the original. Here the 10^2-s along with the INT() and 0.5 is functioning to round to a certain number of decimal places (2). Overall the expression is generating a uniformly distributed value in the range [0.95,1.10] to 2dp. Why they included the rounding, I'm not sure. It will affect the likleihood of the end values so the precision effect of rounding rather than flooring are probably lost in the mix. Oct 10 at 12:24
  • 3
    @DanSheppard I suspect the reason for the rounding is so that the result will have two decimal places of precision.
    – Davislor
    Oct 10 at 19:27
  • 2
    I would rather guess it was the 2 that was originally a variable (rounding precision) that was hardcoded into the program. Oct 11 at 14:21
  • @user3840170: I agree. It's more likely that the author wanted to be able to easily change the number of decimal places, than to change the number base used.
    – dan04
    Oct 11 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.