Best search algorithm to find condition within a range

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Apr 8 00:36:58 CEST 2015


On Wed, 8 Apr 2015 12:36 am, jonas.thornvall at gmail.com wrote:

> Bullshit declare two integers in any language one 7 and one 4 and then
> write x=7+4; if you find a programming language where that does not yield
> 11 tell me.

In Forth, you can set the base to any arbitrary integer (within reason).

steve at orac:/home/steve$ gforth
Gforth 0.7.0, Copyright (C) 1995-2008 Free Software Foundation, Inc.
Gforth comes with ABSOLUTELY NO WARRANTY; for details type `license'
Type `bye' to exit
7 4 + . 11  ok
hex  ok
7 4 + . B  ok
7 4 +   ok
2 base !  ok
. 1011  ok


The dot . prints the value on the top of the stack. In Python terms, the
closest equivalent would be:

print (7 + 4)  # prints 11 by default
# set the base to hex
print (7 + 4)  # prints B
x = 7 + 4
# set the base to 2
print (x)  # prints 1011


except that Python doesn't allow you to change the base used by ints, it is
always decimal.

In Forth, however, setting the base doesn't just change the *display* of
integers, it also changes how you enter them:

3
:7: Undefined word
>>>3<<<
Backtrace:
$B7252EDC throw
$B725F638 no.extensions
$B7253054 interpreter-notfound1
  ok


So in base 2 mode, it doesn't recognise 3 as a number. If I want to enter
three, I have to enter it in binary:

11  ok
decimal  ok
. 3  ok



> Integers are internally assumed to be base 10 otherwise you could not
> calculate without giving the base.


That is *absolutely not* the case in Forth. It's not even the case in
Python: integers are actually stored in either binary (base 2) or some very
large base, I think equivalent to base 256, depending on the version of
Python and the size of the int. 


> All operations on integers addition, subtraction, multiplication and
> division assume base 10.

That's just ridiculous.

In Python, like most other languages, the only thing which assumes base 10
is entry and printing of integers.




-- 
Steven




More information about the Python-list mailing list