A small code example:
Code: Select all
Dim Number as integer
Input "Enter a number "; Number
If Err = 6 then print "number too big"
sleep
end
Code: Select all
Dim Number as integer
Input "Enter a number "; Number
If Err = 6 then print "number too big"
sleep
end
Code: Select all
#define NumOverflow 1e3000
#define NumUnderflow -1e3000
Dim Number as single 'could use double
input "Number is = "; Number
'now test for overflow and underflow
If Number = NumOverflow then
print "overflow"
elseIf Number = NumUnderflow then
print "underflow"
else
'no error
end if
Code: Select all
Dim Number as single 'could use double
Input "Number is = "; Number
'now test for overflow and underflow
If Str(Number) = "1.#INF" Then
print "overflow"
ElseIf Str(Number) = "-1.#INF" Then
print "underflow"
Else
'no error
End If
Sure, it's faster, but uses false bordersStillLearning wrote:I believe my version uses less code and runs faster.
And when you INPUT the number as SINGLE, the overflow occurs in the INPUT statement before your test.FB docs wrote:Single is a 32-bit, floating point data type used to store decimal numbers. They can hold positive values in the range 1.401298e-45 to 3.402823e+38, or negative values in the range -1.401298e-45 to -3.402823e+38, or zero (0). They contain at most 24 bits of precision, or about 6 decimal digits.
StillLearning wrote:I believe my version uses less code and runs faster.
Code: Select all
#define MinNumber -123
#define MaxNumber 123
Dim Number as single 'could use double
Do
input "Number is = "; Number
If Number>=MinNumber and Number<=MaxNumber and Err()=0 then Exit do
print "Your number must be in the range ";MinNumber;" ...";MaxNumber
Loop
Print "Finally you got it, your number is";Number
Sleep
If the user inputs the wrong number of values, or if it expects a number for a value and gets a string that is not a valid number, then QBASIC issues the message "Redo from start", and does not continue further until it receives a valid input.
Not really!jj2007 wrote:Now this is a much, much better way to handle the problem, right?
Code: Select all
CONST AS DOUBLE _
MinN = 1.401298e-45 _
, MaxN = 3.402823e+38
VAR number = 0.0 ' must use double!!
DO
INPUT "Type number"; number
SELECT CASE ABS(number)
CASE 0 : EXIT DO
CASE IS > MaxN : ?"Overflow";
CASE IS < MinN : ?"Underflow";
CASE ELSE : EXIT DO
END SELECT : ?", valid range is [+-] " & MinN & " ... " & MaxN
LOOP
PRINT "Your SINGLE number is " & CSNG(number)
#IFDEF __FB_PCOS__
SLEEP
#ENDIF
Code: Select all
#define MinNumber -123
#define MaxNumber 123
Dim Number as double 'could use double
Do
input "Number is = "; Number
If Number>=MinNumber and Number<=MaxNumber and Err()=0 then Exit do
print "Your number must be in the range ";MinNumber;" ...";MaxNumber
Loop
Print "Finally you got it, your number is";Number
Sleep
Code: Select all
Number is = ? -124
Your number must be in the range -123 ... 123
Number is = ? 124
Your number must be in the range -123 ... 123
Number is = ? 111
Finally you got it, your number is 111
The task as defined by OP was not to check if the input was a valid single value. Besides, 1e-40 is a valid single value.TJF wrote:Hi mere mortal from the Eternal City. The task is checking if the input is a valid SINGLE value. Just try 1e-40 in your code.
The first task by the OP was checking for an INTEGER value. In the second post the OP changed to checking for a SINGLE value.jj2007 wrote:The task as defined by OP was not to check if the input was a valid single value. Besides, 1e-40 is a valid single value.
Yep, that's not a valid single, strictly speaking. But a reasonable language would simply return zero instead of making a fuss about it (and that's what FB does btw).TJF wrote:Test 1e-48.
jj2007 wrote:But a reasonable language would simply return zero instead of making a fuss about it (and that's what FB does btw).
However, the real problem here is that Err() is not being set for serious errors, such as an overflow or a blabla text returning zero.
TJF wrote:
- An overflow returns inf -> no problem.
- The numerical value of blabla is zero -> no problem.
- When values like 1e-48 and 9e-48 should get sorted, the user should get informed that this is not possible. This is the serious issue.
Code: Select all
CONST AS DOUBLE _
MinN = 1.401298e-45 _
, MaxN = 3.402823e+38
' VAR number = 0.0 ' must use double!!
Dim number as single ' we are talking about singles, aren't we?
DO
INPUT "Type number"; number
print "Err 1=";Err()
SELECT CASE ABS(number)
CASE 0 : EXIT DO
CASE IS > MaxN : ?"Overflow";
CASE IS < MinN : ?"Underflow";
CASE ELSE : EXIT DO
END SELECT : ?", valid range is [+-] " & MinN & " ... " & MaxN & " - your number is";number
LOOP
print "Err 2=";Err()
number=number/1000000
print "Err 3=";Err()
PRINT "Your SINGLE number is ";number
#IFDEF __FB_PCOS__
SLEEP
#ENDIF
Code: Select all
Type number? 1e50
Err 1= 0
Overflow, valid range is [+-] 1.401298e-045 ... 3.402823e+038 - your number is 1.#INF
Type number? 1e-50
Err 1= 0
Err 2= 0
Err 3= 0
Your SINGLE number is 0
Users browsing this forum: No registered users and 5 guests