Mega Search
23.2 Million


Sign Up

Make a donation  
Float numbers: Double or Extended  
News Group: embarcadero.public.delphi.language.delphi.general

Hi,

I created my own helper for "Double" because for all float values in my application I use this type.

Then I noticed, that the following call is not working:

var
  a, b: Double;
begin
  (a + b).MyHelperFunc;
end;

I found Rudy's article http://rvelthuis.de/articles/articles-floats.html where he says, that for
calculations values are always converted to Extended.

I always thought, Double is the default float type, e.g. because of this declaration:

function TField.GetAsFloat: Double;


So I'm wondering if I should change all Double to Extended in order to avoid conversion and to avoid
the need for two identical helpers.

My applications are mainly Win32. Maybe at some point I try to switch them to Win64.

cu Christian

Vote for best question.
Score: 0  # Vote:  0
Date Posted: 19-Jan-2015, at 12:35 AM EST
From: Christian Kaufmann
 
Re: Float numbers: Double or Extended  
News Group: embarcadero.public.delphi.language.delphi.general
Am 19.01.2015 um 12:50 schrieb Rudy Velthuis (TeamB):
> Michael Rabatscher wrote:
>
>> AFAIK the standard flags on the FPU are indeed defined that way. So
>> the double values are loaded into the FPU - there converted to
>> 10Bytes and all following operations are executed as "extended". When
>> the data is written back it's again converted to double.
>
> The intermediate value (a+b) is very likely stored in an Extended too.
> So Extended is not only used FPU-internally, it is also used by the
> Delphi compiler for all not explicitly typed floating point results
> like (a+b) in the example code.

yeah right - that's what I meant... it's converted back when it's stored
to memory.

kind regards
   Mike

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 19-Jan-2015, at 4:15 AM EST
From: Michael Rabatscher
 
Re: Float numbers: Double or Extended  
News Group: embarcadero.public.delphi.language.delphi.general
Michael Rabatscher wrote:

> AFAIK the standard flags on the FPU are indeed defined that way. So
> the double values are loaded into the FPU - there converted to
> 10Bytes and all following operations are executed as "extended". When
> the data is written back it's again converted to double.

The intermediate value (a+b) is very likely stored in an Extended too.
So Extended is not only used FPU-internally, it is also used by the
Delphi compiler for all not explicitly typed floating point results
like (a+b) in the example code.

-- 
Rudy Velthuis        http://www.rvelthuis.de

"The graveyards are full of indispensable men."
 -- Charles de Gaulle (1890-1970)

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 19-Jan-2015, at 3:50 AM EST
From: Rudy Velthuis (TeamB)
 
Re: Float numbers: Double or Extended  
News Group: embarcadero.public.delphi.language.delphi.general
Christian Kaufmann wrote:

> Hi,
> 
> I created my own helper for "Double" because for all float values in
> my application I use this type.
> 
> Then I noticed, that the following call is not working:
> 
> var
>   a, b: Double;
> begin
>   (a + b).MyHelperFunc;
> end;
> 
> I found Rudy's article
> http://rvelthuis.de/articles/articles-floats.html where he says, that
> for calculations values are always converted to Extended.
> 
> I always thought, Double is the default float type, e.g. because of
> this declaration:
> 
> function TField.GetAsFloat: Double;

As I wrote in the article, the default is Extended, except on Win64 and
perhaps the mobile compilers. Extended is not available on every
platform, Double generally is.
-- 
Rudy Velthuis        http://www.rvelthuis.de

"The significant problems we face cannot be solved at the same 
 level of thinking we were at when we created them."
 -- Albert Einstein (1879-1955)

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 19-Jan-2015, at 3:46 AM EST
From: Rudy Velthuis (TeamB)
 
Re: Float numbers: Double or Extended  
News Group: embarcadero.public.delphi.language.delphi.general
> I created my own helper for "Double" because for all float values in my application I use this type.
>
> Then I noticed, that the following call is not working:
>
> var
>    a, b: Double;
> begin
>    (a + b).MyHelperFunc;
> end;
>
> I found Rudy's article http://rvelthuis.de/articles/articles-floats.html where he says, that for
> calculations values are always converted to Extended.

AFAIK the standard flags on the FPU are indeed defined that way. So the 
double values are loaded into the FPU - there converted to 10Bytes and 
all following operations are executed as "extended". When the data is 
written back it's again converted to double.
You can force the FPU to use a lower (aka double) precission by using 
the follwoing code:

var fpuCtrlWord : word;

// set only double precission for compatibility
      fpuCtrlWord := Get8087CW;
      // set bits 8 and 9 to "10" -> double precission calculation 
instead of extended!
      fpuCtrlWord := fpuCtrlWord or $0300;
      fpuCtrlWord := fpuCtrlWord and $FEFF;
      Set8087CW(fpuCtrlWord);

I needed that in my math library so the SSE optimized code and the FPU 
code produce (at least to some extend) the very same results.

Note that if you need to port that code to win64 and you don't use the
code above some math algorithms tend to have different outcomes even if
you use a pure pascal definition of that (e.g. some iterative 
optimization routines).

kind regards
   Mike



>
> I always thought, Double is the default float type, e.g. because of this declaration:
>
> function TField.GetAsFloat: Double;
>
>
> So I'm wondering if I should change all Double to Extended in order to avoid conversion and to avoid
> the need for two identical helpers.
>
> My applications are mainly Win32. Maybe at some point I try to switch them to Win64.
>
> cu Christian
>

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 19-Jan-2015, at 3:20 AM EST
From: Michael Rabatscher
 
Re: Float numbers: Double or Extended  
News Group: embarcadero.public.delphi.language.delphi.general
Hi.

If in Win32 than all intermediates are extended.

In Win64 extended is an alias for double: http://docwiki.embarcadero.com/Libraries/XE7/en/System.Extended
With one exception: http://docwiki.embarcadero.com/RADStudio/XE7/en/Extended_type_compatibility_(Delphi).

So, for Win32 you actually need two helpers.

For Win64 there is some compiler switch to suppress conversion of single to double for intermediates. But for Win32 I don't know if such a switch exists.


> {quote:title=Christian Kaufmann wrote:}{quote}
> Hi,
> 
> I created my own helper for "Double" because for all float values in my application I use this type.
> 
> Then I noticed, that the following call is not working:
> 
> var
>   a, b: Double;
> begin
>   (a + b).MyHelperFunc;
> end;
> 
> I found Rudy's article http://rvelthuis.de/articles/articles-floats.html where he says, that for
> calculations values are always converted to Extended.
> 
> I always thought, Double is the default float type, e.g. because of this declaration:
> 
> function TField.GetAsFloat: Double;
> 
> 
> So I'm wondering if I should change all Double to Extended in order to avoid conversion and to avoid
> the need for two identical helpers.
> 
> My applications are mainly Win32. Maybe at some point I try to switch them to Win64.
> 
> cu Christian

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 19-Jan-2015, at 3:13 AM EST
From: Maxim Shiryaev