お知らせ

電子会議

ライブラリ

パレット

Delphi FAQ検索

Delphi FAQ一覧

サンプル蔵





FDelphi FAQ
16番会議室「玉石混淆みんなで作るSample蔵」に寄せられたサンプル

"誤差の発生しない四則演算2"






◆サンプルコード 2◆
function SubLongFixed(const x, y: string): string;
var
  x1, y1: TLongFixed;
begin
  x1:=StrToLongFixed(GetAbsStr(x));
  y1:=StrToLongFixed(GetAbsStr(y));

  if isPositive(x) then begin
    if isPositive(y) then begin
      if isAbsGTE(x1, y1) then begin
        result:=LongFixedToStr(SubAbsLongFixed(x1, y1));
      end
      else begin
        result:='-'+LongFixedToStr(SubAbsLongFixed(y1, x1));
      end;
    end
    else begin
        result:=LongFixedToStr(AddAbsLongFixed(x1, y1));
    end;
  end
  else begin
    if isPositive(y) then begin
      result:='-'+LongFixedToStr(AddAbsLongFixed(x1, y1));
    end
    else begin
      if isAbsGTE(x1, y1) then begin
        result:='-'+LongFixedToStr(SubAbsLongFixed(x1, y1));
      end
      else begin
        result:=LongFixedToStr(SubAbsLongFixed(y1, x1));
      end;
    end;
  end;

end;

function AddLongFixed(const x, y: string): string;
begin
  if isPositive(x) then begin
    if isPositive(y) then begin
      result:=LongFixedToStr(AddAbsLongFixed(StrToLongFixed(x),
                                             StrToLongFixed(y)));
    end
    else begin
      result:=SubLongFixed(x, GetAbsStr(y));
    end;
  end
  else begin
    if isPositive(y) then begin
      result:=SubLongFixed(y, GetAbsStr(x));
    end
    else begin
      result:='-'+LongFixedToStr(AddAbsLongFixed(
                                   StrToLongFixed(
                                     GetAbsStr(x)),
                                     StrToLongFixed(GetAbsStr(y))));
    end;
  end;

end;

function MulLongFixed(const x, y: string): string;
begin
  if isPositive(x) then begin
    if isPositive(y) then begin
      result:=LongFixedToStr(MulAbsLongFixed(StrToLongFixed(x),
                                             StrToLongFixed(y)));
    end
    else begin
      result:='-'+LongFixedToStr(MulAbsLongFixed(StrToLongFixed(x),
                                   StrToLongFixed(GetAbsStr(y))));
    end;
  end
  else begin
    if isPositive(y) then begin
      result:='-'+LongFixedToStr(MulAbsLongFixed(StrToLongFixed(
                                   GetAbsStr(x)),StrToLongFixed(y)));
    end
    else begin
      result:=LongFixedToStr(MulAbsLongFixed(StrToLongFixed(
                                 GetAbsStr(x)),
                                 StrToLongFixed(GetAbsStr(y))));
    end;
  end;

end;

function QuoLongFixed(const x, y: string): string;
begin
  if isPositive(x) then begin
    if isPositive(y) then begin
      result:=LongFixedToStr(QuoAbsLongFixed(
                               StrToLongFixed(x),
                               StrToLongFixed(y)));
    end
    else begin
      result:='-'+LongFixedToStr(QuoAbsLongFixed(
                                   StrToLongFixed(x),
                                   StrToLongFixed(GetAbsStr(y))));
    end;
  end
  else begin
    if isPositive(y) then begin
      result:='-'+LongFixedToStr(QuoAbsLongFixed(
                                   StrToLongFixed(GetAbsStr(x)),
                                   StrToLongFixed(y)));
    end
    else begin
      result:=LongFixedToStr(QuoAbsLongFixed(
                               StrToLongFixed(GetAbsStr(x)),
                               StrToLongFixed(GetAbsStr(y))));
    end;
  end;

end;

 // 97/12/19 (金) 21:47:36 07コナン


Original document by 07コナン      氏 ID:(QZM05143)


ここにあるドキュメントは NIFTY SERVEの Delphi Users' Forum の16番会議室「玉石混淆みんなで作るSample蔵」に投稿されたサンプルです。これらのサンプルはボーランド株式会社がサポートする公式のものではありません。また、必ずしも動作が検証されているものではありません。これらのサンプルを使用したことに起因するいかなる損害も投稿者、およびフォーラムスタッフはその責めを負いません。使用者のリスクの範疇でご使用下さい。

Copyright 1996-2002 Delphi Users' Forum