4

I'm working in a XE6 project, but this may apply to other version of builder as well.

I'm looking at a function name, which I think may be misleading. I'm curious if StrToFloat() returns a float or if it returns a double. I found an alternative, which is .ToDouble() but we have a bunch of references in our code already that uses StrToFloat(). I wish to verify that I'm getting the proper precision that doubles offer.

I've done a couple tests like:

UnicodeString temp = "1234567890.12345678901234567890";
double a = StrToFloat(temp);
double b = temp.ToDouble();

These seem to give the same values from the tests I've done, but I wish to verify the StrToFloat() is the same as .ToDouble()

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
James Oravec
  • 19,579
  • 27
  • 94
  • 160

1 Answers1

2

I found enough references to answer my own question...

StrToFloat() returns an extended and an extended is a long double.

.ToDouble() returns a double.

So short answer is they are not the same, and vary as shown above.

References:

James Oravec
  • 19,579
  • 27
  • 94
  • 160
  • 2
    `.ToDouble()` calls `StrToFloat()` internally and returns the `Extended` as a `double` upon exit. Do pay attention to the `Extended` documentation, though. `Extended` is a `long double` on 32bit platforms, but is a `double` on Win64. The difference is that `double` is 8 bytes (64 bits) whereas `long double` is 10 bytes (80 bits). That is a 16bit [loss of precision on Win64](http://docwiki.embarcadero.com/RADStudio/XE6/en/Delphi_Considerations_for_Multi-Device_Applications#The_Extended_Data_Type_Is_2_Bytes_Smaller_on_64-bit_Windows_Systems). – Remy Lebeau Aug 12 '14 at 19:39
  • 1
    @RemyLebeau: That is very strange – Mooing Duck Aug 12 '14 at 19:43
  • 1
    @MooingDuck: It is due to a hardware limitation of 64bit CPUs. x86 natively supports 10-byte floating-point operations (hense why `Extended` existed in the first place), whereas x64 does not. Embarcadero does provide a `TExtended80Rec` type to handle 10-byte floating-point operations in x64, but of course the performance will not be the same as the CPU doing them natively. – Remy Lebeau Aug 12 '14 at 19:48