Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Template for numbers only (https://www.delphipraxis.net/183800-template-numbers-only.html)

WojTec 5. Feb 2015 12:19

Delphi-Version: XE6

Template for numbers only
 
I have function:

Delphi-Quellcode:
function SimilarValue(const AValue: Float32; AValues: array of Float32; out AResult: Float32): Int32;
var
  I: Int32;
  Distance: Float80;
  SmallestDistance: Float80;
begin
  Result := -1;
  AResult := 0;

  SmallestDistance := 16777216;

  for I := Low(AValues) to High(AValues) do
  begin
    Distance := Sqr(AValue - AValues[I]);
    if Distance < SmallestDistance then
    begin
      Result := I;
      AResult := AValues[I];

      SmallestDistance := Distance
    end;
  end;
end;
I want to make it working for any numeric type, but won't to overloading. Is it possible to do it with templates?

Delphi-Quellcode:
type
  TSimilarValue<T> = class
    class function Get(const AValue: T; AValues: array of T; out AResult: T): Int32;
  end;

class function TSimilarValue<T>.Get(const AValue: T; AValues: array of T; out AResult: T): Int32;

himitsu 5. Feb 2015 13:02

AW: Template for numbers only
 
Natürlich wäre es so auch möglich,


oder einfach nur so
Delphi-Quellcode:
function SimilarValue(const AValue: Double; AValues: array of const; out Index: Ineger): Double;  
oder
function SimilarValue(const AValue: Double; AValues: array of Variant; out Index: Ineger): Double;
Funktioniert für alle Integer-Typen (beim Result natürlich mit Round) und allen normalen Float-Typen (Single und Double).

Uwe Raabe 5. Feb 2015 13:12

AW: Template for numbers only
 
Zitat:

Zitat von himitsu (Beitrag 1288903)
(beim Result natürlich mit Round)

Ist dir vielleicht entgangen, daß der Result den Index im Array zurückgibt?

himitsu 5. Feb 2015 13:17

AW: Template for numbers only
 
Nein, ist mir nicht :zwinker:, aber bei VAR/OUT muß der Typ genau stimmen muß und da nur der Index-Typ vordefiniert ist, da hab ich das einfach mal gedreht. :stupid:

Der schöne Günther 5. Feb 2015 13:19

AW: Template for numbers only
 
Generics (or "templates") won't help you here because there is no way of constraining a type to "numbers only".

As others said, System.Math.SameValue is probably your best bet for checking equality of floats

WojTec 5. Feb 2015 13:55

Re: Template for numbers only
 
How to change code if parameter will ba array of const?
Can do it better? With SameValue? How?

Der schöne Günther 5. Feb 2015 13:58

AW: Template for numbers only
 
Delphi-Quellcode:
array of const
is just a legacy way of writing "array of TVarRec".

I still don't quite get what you plan to do "better" than System.Math.SameValue(..)

WojTec 5. Feb 2015 14:16

Re: AW: Template for numbers only
 
Zitat:

Zitat von Der schöne Günther (Beitrag 1288914)
I still don't quite get what you plan to do "better" than System.Math.SameValue(..)

Not better than, but better with. How to do with SameValue?

himitsu 5. Feb 2015 17:52

AW: Re: AW: Template for numbers only
 
Zitat:

Zitat von Der schöne Günther (Beitrag 1288914)
I still don't quite get what you plan to do "better" than System.Math.SameValue(..)

Er möchte nicht zwei Werte vergleichen (Delphi-Referenz durchsuchenSameValue), sondern in einer Liste von Werten den Ähnlichsten heraussuchen.

Also eher wie Delphi-Referenz durchsuchenMaxValue, nur eben nicht das Größte, sondern das Ähnlichste ... fast wie eine unscharfe Suche ala Delphi-Referenz durchsuchenMatchText, zusammengemanscht mit einem Delphi-Referenz durchsuchenIndexText.


Delphi-Quellcode:
array of const
-> siehe Delphi-Referenz durchsuchenFormat = Man kann alles möglichen Typen übergeben (Integer, Float usw.) und entscheidet dann intern, wie man welchen Typen behandelt, also ähnlich einem
Delphi-Quellcode:
array of variant
.




Delphi und C# kennen sowas wie "Number" nicht als Einschränkung.
http://docwiki.embarcadero.com/RADSt...ierte_Typen%29
https://msdn.microsoft.com/de-de/library/d5x73970.aspx
Java kann so etwas, aber dort ist doch letztendlich alles irgendwie eine Klasse.
http://www.java-forum.org/java-basic...chraenken.html

Ohne "nummerische" Typeinschränkung funktioieren viele Dinge einfach nicht.
Warum es soeine Einschränkung nicht gibt, welche alle nummerischen Typen und Records mit Operatoren erlauft, weiß ich nicht.
Delphi-Quellcode:
type
  TValue<T> = class
    class function Similar(const AValue: T; const AValues: array of T; out AResult: T): Integer;
  end;

class function TValue<T>.Similar(const AValue: T; const AValues: array of T; out AResult: T): Integer;
var
  i: Integer;
  Distance, Smallest: T;
begin
  Result  := -1;
  AResult := Default(T);
  Smallest := Max(T); // gibt es nicht
  for i := Low(AValues) to High(AValues) do begin
    Distance := Abs(AValue - AValues[i]); // geht nicht
    if Distance < Distance then begin // geht nicht
      Result  := I;
      AResult := AValues[I];
      Smallest := Distance
    end;
  end;
end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 00:28 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz