1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| type {Grande partie Gemeni}
TFixed2 = record
private
RawValue: SmallInt; // Stocke la valeur * 100
public
// Conversion depuis et vers Double
class operator Implicit(a: Double): TFixed2; overload;
class operator Implicit(a: TFixed2): Double; overload;
// Opérateurs arithmétiques
class operator Add(a, b: TFixed2): TFixed2;
class operator Subtract(a, b: TFixed2): TFixed2;
class operator Multiply(a, b: TFixed2): TFixed2;
class operator Divide(a, b: TFixed2): TFixed2;
function ToString: string;
end;
{ Implémentation des opérateurs }
class operator TFixed2.Implicit(a: Double): TFixed2;
begin
Result.RawValue := Round(a * 100);
end;
class operator TFixed2.Implicit(a: TFixed2): Double;
begin
Result := a.RawValue / 100.0;
end;
class operator TFixed2.Add(a, b: TFixed2): TFixed2;
begin
Result.RawValue := a.RawValue + b.RawValue;
end;
class operator TFixed2.Subtract(a, b: TFixed2): TFixed2;
begin
Result.RawValue := a.RawValue - b.RawValue;
end;
class operator TFixed2.Multiply(a, b: TFixed2): TFixed2;
begin
// On multiplie, puis on divise par 100 pour garder l'échelle
Result.RawValue := Round((a.RawValue * b.RawValue) / 100);
end;
class operator TFixed2.Divide(a, b: TFixed2): TFixed2;
begin
// On multiplie le numérateur par 100 avant de diviser
Result.RawValue := Round((a.RawValue * 100) / b.RawValue);
end;
function TFixed2.ToString: string;
begin
Result := FloatToStrF(Self.RawValue / 100, ffFixed, 8, 2);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
f1, res: TFixed2;
D: Double;
begin
D := 15;
f1 := 10;
res := f1+ D ; // 10.00 * 15
showmessage(res.ToString);// affiche 25
res := res / f1; // 25.00 / 10.00
showmessage(res.ToString);// affiche 2.5
end; |
Partager