You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

Determines if a value is a number upon which arithmetic can be done.

Syntax

Result = SRP_Num(Expression, Default)

Parameters

Title FieldDescription
ExpressionThe expression to evaluate.
DefaultThe default value to return if the expression is not a number. (Optional)

Remarks

SRP_Num has two modes: to determine if an expression is a number or to default an expression to a number. Mode 1 occurs when Default is omitted, and mode 2 occurs when Default is set.

Mode 1

In Mode 1, SRP_Num returns 1 if the expression is a number or 0 if not. Unlike the built-in Num() function, SRP_Num only returns 1 if Expression is safe for arithmetic.

// A is not assigned, so IsNum will be 0. Using Num() would have broken to the debugger.
IsNum = SRP_Num(A)

// B is "", which cannot safely be used to calculate C.
// Num() would have returned 1 and calculating C would have caused a debugger error.
B = ""
If SRP_Num(B) then
	C = B + 1
end

Mode 2

In Mode 2, SRP_Num returns Expression if Expression is a number or Default if it is not. This is particularly useful for quickly defaulting numeric function parameters.

Compile Function Add(Num1, Num2)


    // The usual way of defaulting parameters, which will still break if Num1 is ""
    If Unassigned(Num1) then Num1 = 0
    If Unassigned(Num2) then Num2 = 0


    // To be even safer, we need to check for both "" and unassigned... but this will break too
    // because BASIC+ evaluates all expressions in an condition, even if the first expression
    // makes the condition true. Thus, these statements will break to the debugger if Num1 or Num2
    // is unassigned.
    If Unassigned(Num1) OR Num1 EQ "" then Num1 = 0
    If Unassigned(Num2) OR Num2 EQ "" then Num2 = 0


	// This is even safer, but the arithmetic will break to the debugger if Num1 or Num2 contains
    // non-digits or is too big to be a number.
    If Unassigned(Num1) then
        Num1 = 0
    end else
        If Num1 EQ "" then Num1 = 0
    end
    If Unassigned(Num2) then
        Num2 = 0
    end else
        If Num2 EQ "" then Num2 = 0
    end


    // SRP_Num covers all the bases
    Num1 = SRP_Num(Num1, 0)
    Num2 = SRP_Num(Num2, 0)

Return Num1 + Num2
  • No labels