Automatic set ERR for error

New to FreeBASIC? Post your questions here.
StillLearning
Posts: 33
Joined: Aug 27, 2019 22:22

Automatic set ERR for error

Postby StillLearning » Jul 18, 2020 22:40

I read you have to use the -e compiler option to have support for error handling. Why doesn't Freebasic automatically set the value for ERR when a error occurs but not use it? This way a programmer can have a error occur and not have to have all the error handling support that, in some cases, uses a lot of extra code.

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
fxm
Posts: 9834
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Automatic set ERR for error

Postby fxm » Jul 19, 2020 5:34

Not exact.

The default FreeBASIC behavior (-e, -ex or -exx switch not used) is to set the ERR variable and continue.
(but your example cannot set ERR)

See the Error Handling documentation page.
StillLearning
Posts: 33
Joined: Aug 27, 2019 22:22

Re: Automatic set ERR for error

Postby StillLearning » Jul 21, 2020 2:46

Here is my solution to detect overflow and underflow of a floating point number:
This solution requires no error handler or Err.

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



I found no problems so far. Am I missing something?
Still working on solution for non-floating point numbers.
fxm
Posts: 9834
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Automatic set ERR for error

Postby fxm » Jul 21, 2020 18:36

I would prefer this coding:

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
StillLearning
Posts: 33
Joined: Aug 27, 2019 22:22

Re: Automatic set ERR for error

Postby StillLearning » Jul 21, 2020 20:28

I believe my version uses less code and runs faster.
TJF
Posts: 3599
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: Automatic set ERR for error

Postby TJF » Jul 22, 2020 7:59

StillLearning wrote:I believe my version uses less code and runs faster.
Sure, it's faster, but uses false borders
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.
And when you INPUT the number as SINGLE, the overflow occurs in the INPUT statement before your test.

Regards
jj2007
Posts: 1645
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Automatic set ERR for error

Postby jj2007 » Jul 22, 2020 8:45

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

Now this is a much, much better way to handle the problem, right?

Except that Err() will not be set if you type "FB is a great language" instead of a valid number.

Once upon a time, there was a language called "QBasic" which, according to the FB manual, treated the problem as follows:
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.
TJF
Posts: 3599
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: Automatic set ERR for error

Postby TJF » Jul 22, 2020 9:43

jj2007 wrote:Now this is a much, much better way to handle the problem, right?
Not really!

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
jj2007
Posts: 1645
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Automatic set ERR for error

Postby jj2007 » Jul 22, 2020 10:58

Explain to us mere mortals, great guru from the Austrian Alpes, why your solution is better than mine...

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
TJF
Posts: 3599
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: Automatic set ERR for error

Postby TJF » Jul 22, 2020 11:48

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.
jj2007
Posts: 1645
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Automatic set ERR for error

Postby jj2007 » Jul 22, 2020 12:48

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 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.

The problem is another one: FreeBasic does not check if the input is valid for any number. It does not even set Err() for "asdsadasd", it just returns zero.
TJF
Posts: 3599
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: Automatic set ERR for error

Postby TJF » Jul 22, 2020 13:51

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.
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.

My mistake, 1e-40 is a valid SINGLE. Test 1e-48.
jj2007
Posts: 1645
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Automatic set ERR for error

Postby jj2007 » Jul 22, 2020 19:02

TJF wrote:Test 1e-48.
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).

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
Posts: 3599
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: Automatic set ERR for error

Postby TJF » Jul 22, 2020 23:42

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.
  • 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.
Anyhow, INPUT is meant to be a quick and dirty solution for testing. For further tasks it needs a masked GUI entry, or in a terminal program a statement like INPUT USING in Omikron BASIC (nemored transformed that to FB).
jj2007
Posts: 1645
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Automatic set ERR for error

Postby jj2007 » Jul 23, 2020 0:53

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

Re bla bla is no problem: sure, there are no typos in this world, but people need to sort values like 1e-48 and 9e-48 correctly all the time.

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 5 guests