Recent

Author Topic: tostring is working properly ?  (Read 561 times)

Paolo

  • Sr. Member
  • ****
  • Posts: 499
tostring is working properly ?
« on: March 17, 2023, 03:51:29 pm »
Hello,

incidentally I see that the code below compile with double variabile but not with real variabile, is this bheaviour the expected one ? (really I do not make use of real type, but I noted this), doesn't real is an alias of double ?

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button5Click(Sender: TObject);
  2. var
  3.   a : real;
  4.   b : double;
  5.   c : string;
  6. begin
  7.   c:=a.tostring;  <--- it does not compile
  8.   c:=b.tostring;  <--- it compiles
  9. end;
  10.  

laz 2.2.2, fpc 3.2.2 win10

mirce.vladimirov

  • Sr. Member
  • ****
  • Posts: 256
Re: tostring is working properly ?
« Reply #1 on: March 17, 2023, 03:57:28 pm »
I dont realy know about this, I am using :

Code: Pascal  [Select][+][-]
  1. c:=floattostr(a) ;
  2. c:=floattostr(b);
  3. c:=inttostr(some_integer_variable);
  4.  

There are also oposite strtoint() and strtofloat()

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: tostring is working properly ?
« Reply #2 on: March 17, 2023, 04:00:04 pm »
incidentally I see that the code below compile with double variabile but not with real variabile, is this bheaviour the expected one ? (really I do not make use of real type, but I noted this), doesn't real is an alias of double ?

Real is a unique alias of Double (type Real = type Double vs. type Real = Double) and thus type helpers for Double are not in scope for Real variables. So yes, this is expected behaviour.

Paolo

  • Sr. Member
  • ****
  • Posts: 499
Re: tostring is working properly ?
« Reply #3 on: March 17, 2023, 04:39:03 pm »
thanks for the explaination. (It is the expected behaviour according to type definition, but is it the desired behaviour ? :))

We can say that "tostring" is less portable than "/floattostr/flrotstrF/etc.." family. Having code with double and a lot of "tostring" if the type is changed to real casues a lot of compiler errors.

(But again it is not a real isssue for me, since I am using only double type since long time ago...)

dsiders

  • Hero Member
  • *****
  • Posts: 1052
Re: tostring is working properly ?
« Reply #4 on: March 17, 2023, 04:54:01 pm »
We can say that "tostring" is less portable than "/floattostr/flrotstrF/etc.." family. Having code with double and a lot of "tostring" if the type is changed to real casues a lot of compiler errors.

But Real.ToString is exactly as portable as the StrtoReal and RealToString routines. Because they don't exist either.
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

Paolo

  • Sr. Member
  • ****
  • Posts: 499
Re: tostring is working properly ?
« Reply #5 on: March 17, 2023, 05:34:15 pm »
but this works

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button5Click(Sender: TObject);
  2. var
  3.   a : real;
  4.   b : double;
  5.   c : string;
  6. begin
  7.   c:=FloatToStr(a); //<--- this compile
  8.   c:=b.tostring;
  9. end;
  10.  

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: tostring is working properly ?
« Reply #6 on: March 19, 2023, 08:18:09 pm »
thanks for the explaination. (It is the expected behaviour according to type definition, but is it the desired behaviour ? :))

Well, feel free to open a bug report to add a type helper for Real as well.

We can say that "tostring" is less portable than "/floattostr/flrotstrF/etc.." family. Having code with double and a lot of "tostring" if the type is changed to real casues a lot of compiler errors.

Real is simply a legacy type from TP times and considering that Delphi tends to ignore legacy types and that type helpers are a concept introduced by Delphi there was simply no type helper added for Real.

but this works

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button5Click(Sender: TObject);
  2. var
  3.   a : real;
  4.   b : double;
  5.   c : string;
  6. begin
  7.   c:=FloatToStr(a); //<--- this compile
  8.   c:=b.tostring;
  9. end;
  10.  

Because the compiler can convert Real to Double, but for declaring overloads and for type helpers an exact type match is necessary.

 

TinyPortal © 2005-2018