Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2016-02

Possible bug in Numeric test

6 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

steveg

Not sure if this is considered a bug or not :)

I have found this function returns TRUE if passed 'E1/E2/etc' I am guessing it is seeing the 'E' as exponent

function IsNum( const sSrc :string ) :boolean;
var
   Code :integer;
   Num :real = 0;
begin
   Num := Num;
   Val(sSrc, Num, Code);
   Exit( Code = 0 );
end;

Thanks - SteveG

Vojtěch Čihák
to steveg

I tried your function in FPC 3.0.0 in mode ObjFPC and it returns False for string 'E1/E2/etc'.  
V.
______________________________________________________________

Show quoted text 5 lines
Od: steveg Komu: "FPC-Pascal users discussions" Datum: 24.02.2016 00:57 Předmět: [fpc-pascal] Possible bug in Numeric test

Not sure if this is considered a bug or not :)

I have found this function returns TRUE if passed 'E1/E2/etc' I am guessing it is seeing the 'E' as exponent

function IsNum( const sSrc :string ) :boolean;
var
   Code :integer;
   Num :real = 0;
begin
   Num := Num;
   Val(sSrc, Num, Code);
   Exit( Code = 0 );
end;

Thanks - SteveG

Vojtěch Čihák
to Nitorami

Oh, I didn't catch it. So there must be some checking for E in the string - at least on the first place - because strings 'E' and '1E' returns FALSE.  ______________________________________________________________

Show quoted text 5 lines
Od: Nitorami Komu: Datum: 24.02.2016 22:22 Předmět: Re: [fpc-pascal] Possible bug in Numeric test

Steve probably means "when passed 'E1' or 'E2' etc." Indeed IsNum ('E1') interprets it as zero and delivers TRUE.

Bart
to steveg: Not sure if this is considered a bug or not :) I have found this function returns TRUE if…
steveg wrote
Not sure if this is considered a bug or not :)

Fully compatible with Delphi 7.

{$apptype console}
{$ifdef fpc}
{$mode objfpc}
{$h+}
{$codepage utf8}
{$endif}
{$R+}
uses
  classes,sysutils;
function IsNum( const sSrc :string ) :boolean;
var
  Code :integer;
  Num :real;
begin
  Val(sSrc, Num, Code);
  Result := ( Code = 0 );
end;
var
  R: Real;
  Code: Integer;
begin
  Val('E1',R,Code);
  writeln('E1 : Code = ',Code,#32, IsNum('E1'));
  Val('E',R,Code);
  writeln('E  : Code = ',Code,#32, IsNum('E'));
  Val('1E',R,Code);
  writeln('1E : Code = ',Code,#32, IsNum('1E'));
  Val('1E1',R,Code);
  writeln('1E1: Code = ',Code,#32, IsNum('1E1'));
  Val('E1/E2/etc',r,Code);
  writeln('etc: Code = ',Code,#32, IsNum('E1/E2/etc'));
end.

C:\Users\Bart\LazarusProjecten\ConsoleProjecten>dcc32 test.pas Borland Delphi Version 15.0
Copyright (c) 1983,2002 Borland Software Corporation test.pas(19) Hint: Value assigned to 'Num' never used test.pas(36) Hint: Value assigned to 'R' never used test.pas(34) Hint: Value assigned to 'R' never used test.pas(32) Hint: Value assigned to 'R' never used test.pas(30) Hint: Value assigned to 'R' never used test.pas(28) Hint: Value assigned to 'R' never used test.pas(39)
40 lines, 0.20 seconds, 71676 bytes code, 4097 bytes data.

C:\Users\Bart\LazarusProjecten\ConsoleProjecten>test E1 : Code = 0 TRUE
E : Code = 2 FALSE
1E : Code = 3 FALSE
1E1: Code = 0 TRUE
etc: Code = 3 FALSE

C:\Users\Bart\LazarusProjecten\ConsoleProjecten>fpc test.pas Free Pascal Compiler version 3.0.0 [2015/11/16] for i386 Copyright (c) 1993-2015 by Florian Klaempfl and others Target OS: Win32 for i386
Compiling test.pas
test.pas(17,3) Note: Local variable "Num" is assigned but never used test.pas(25,3) Note: Local variable "R" is assigned but never used Linking test.exe
38 lines compiled, 2.6 sec, 133856 bytes code, 5828 bytes data 2 note(s) issued

C:\Users\Bart\LazarusProjecten\ConsoleProjecten>test E1 : Code = 0 TRUE
E : Code = 2 FALSE
1E : Code = 3 FALSE
1E1: Code = 0 TRUE
etc: Code = 3 FALSE

Bart

steveg
to Vojtěch Čihák: I tried your function in FPC 3.0.0 in mode ObjFPC and it returns False for…

Drat, that was a bad assumption of mine - sorry

I meant it fails for any of E1 or E2 entries
so any entry beginning with E and then a following number

eg: IsNum('E1');

Sorry for the poor example

Vojtěch Čihák wrote 39 lines
I tried your function in FPC 3.0.0 in mode ObjFPC and it returns False for string 'E1/E2/etc'. V. ______________________________________________________________ > Od: steveg > Komu: "FPC-Pascal users discussions" > Datum: 24.02.2016 00:57 > Předmět: [fpc-pascal] Possible bug in Numeric test > Not sure if this is considered a bug or not :) I have found this function returns TRUE if passed 'E1/E2/etc' I am guessing it is seeing the 'E' as exponent function IsNum( const sSrc :string ) :boolean; var Code :integer; Num :real = 0; begin Num := Num; Val(sSrc, Num, Code); Exit( Code = 0 ); end; Thanks - SteveG _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal