我有以下程序:
procedure GetDegree(const num : DWORD ; var degree : DWORD ; min ,sec : Extended);
begin
degree := num div (500*60*60);
min := num div (500*60) - degree *60;
sec := num/500 - min *60 - degree *60*60;
end;
Run Code Online (Sandbox Code Playgroud)
在指定了度变量之后,调试器会跳到过程的结尾.这是为什么?
Dav*_*nan 17
这是一个优化.变量min和sec值按值传递.这意味着调用者看不到对它们的修改,并且对此过程是私有的.因此,编译器可以确定分配给它们是没有意义的.永远不会读取分配给变量的值.因此编译器选择节省时间并跳过分配.我希望你的意思是声明这样的程序:
procedure GetDegree(const num: DWORD; var degree: DWORD; var min, sec: Extended);
Run Code Online (Sandbox Code Playgroud)
正如我在上一个问题中所说,使用时并没有多大意义Extended.使用其中一种标准浮点类型会更好,Single或者Double.甚至使用Real映射到的通用Double.
此外,您已声明min为浮点类型,但计算计算整数.在这方面,我对你上一个问题的回答非常准确.
我建议你创建一个记录来保存这些值.传递三个独立的变量会使您的函数接口非常混乱并破坏封装.这三个值仅在考虑整体时才有意义.
type
TGlobalCoordinate = record
Degrees: Integer;
Minutes: Integer;
Seconds: Real;
end;
function LongLatToGlobalCoordinate(const LongLat: DWORD): TGlobalCoordinate;
begin
Result.Degrees := LongLat div (500*60*60);
Result.Minutes := LongLat div (500*60) - Result.Degrees*60;
Result.Seconds := LongLat/500 - Result.Minutes*60 - Result.Degrees*60*60;
end;
function GlobalCoordinateToLongLat(const Coord: TGlobalCoordinate): DWORD;
begin
Result := Round(500*(Coord.Seconds + Coord.Minutes*60 + Coord.Degrees*60*60));
end;
Run Code Online (Sandbox Code Playgroud)