Menu

#60 strings and numbers

v1.0_(example)
wont-fix
nobody
None
5
2016-08-08
2016-07-17
No

Hi,

Variable Assignment - Automatically change the type of a variable is a good thing. It must remain so.

a=1    #Integer
a="x"  #Now 'a' is a string

But those lines should not work:

print "5" / "5"        # 1.0
print 5 * "5"          # 25.0
print "2e3" - 2e3      # 0.0
print "2"^2            # 4.0

I don´t think that a string shoud be so versatile.
All those lines should generate errors.

Also, numbers should be stored as integers as much as possible. print 5 / 5 should print 1, not 1.0. Stored or (maybe) just printed so.

Respectfully,
Florin Oprea

Discussion

  • Jim Reneau

    Jim Reneau - 2016-08-01
    • status: open --> wont-fix
     
  • Jim Reneau

    Jim Reneau - 2016-08-01

    Actualy, this is the expected behaviour. If a string contains a number and "math" (not + concatenate) is done to it, BASIC-256 tries to convert it. It allows for old "INPUT X" statement to imput a string with a number in it and still work like a number.

    The "/" operator is division and will ALWAYS return a float, the "\" operator is integer division and will always return an integer. This behaviour is like Python and I found it easier for students to understand.

     
  • Florin Oprea

    Florin Oprea - 2016-08-01

    INPUT command is the only place where the user assign a string or a number to a variable.
    Would not it be better to try to figure out what the user entered only there?

    INPUT a

    If the user enters "text" then a will be a string variable.
    If the user enters "15" then we have an integer.
    If user enters "15.1" then a will be a float variable.

    If the progam wants to ensure that the input is expected type then it can use safe alternatives: input float, input integer or input string. Or convert input to desired type with string(), int() or float().
    "Never trust user input", right?

    So, i think changing input command is the key:
    input - guess input type
    input float - convert text into float - if no float, then return 0.0
    input integer - convert text into integer - if no integer, then return 0
    input string - return text

    Just that.

    Think about the following questions:
    1) How many errors can meet a user if you change just the way it works INPUT command? (Remember that it is perfectly compatible with older versions: the INPUT a$ / INPUT a)
    2) How many errors can occur if the variables are automatically converted at every step?

    Please consider this. Not necessarily hold in my opinion, but I really believe in its simplicity.

    Respectfully,
    FLorin Oprea

     
  • Jim Reneau

    Jim Reneau - 2016-08-08

    The INPUT statement (for some time - I just checked the code) behaves as you suggested.

    input ">>",a
    print a + " " + typeof(a)

    • enter the integer 99 and 99 1 is printed
    • enter the float 99.99 and 99.99 2 is printed
    • enter a string hola and hola 3 is printed

    Your input matters and I really appreciate it.
    Jim

     

Log in to post a comment.