Delphi-Version: XE6
Template for numbers only
I have function:
Delphi-Quellcode:
I want to make it working for any numeric type, but won't to overloading. Is it possible to do it with templates?
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;
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; |
AW: Template for numbers only
Natürlich wäre es so auch möglich,
oder einfach nur so
Delphi-Quellcode:
Funktioniert für alle Integer-Typen (beim Result natürlich mit Round) und allen normalen Float-Typen (Single und Double).
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; |
AW: Template for numbers only
Zitat:
|
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:
|
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 |
Re: Template for numbers only
How to change code if parameter will ba array of const?
Can do it better? With SameValue? How? |
AW: Template for numbers only
Delphi-Quellcode:
is just a legacy way of writing "array of TVarRec".
array of const
I still don't quite get what you plan to do "better" than System.Math.SameValue(..) |
Re: AW: Template for numbers only
Zitat:
|
AW: Re: AW: Template for numbers only
Zitat:
Also eher wie MaxValue, nur eben nicht das Größte, sondern das Ähnlichste ... fast wie eine unscharfe Suche ala MatchText, zusammengemanscht mit einem IndexText.
Delphi-Quellcode:
-> siehe Format = Man kann alles möglichen Typen übergeben (Integer, Float usw.) und entscheidet dann intern, wie man welchen Typen behandelt, also ähnlich einem
array of const
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