![]() |
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, ![]() |
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
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 ![]() ![]() ![]()
Delphi-Quellcode:
-> siehe
array of const
![]()
Delphi-Quellcode:
.
array of variant
Delphi und C# kennen sowas wie "Number" nicht als Einschränkung. ![]() ![]() Java kann so etwas, aber dort ist doch letztendlich alles irgendwie eine Klasse. ![]() 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 14:21 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz