delphi – 为什么编译器会跳过赋值变量

栏目: 编程语言 · 发布时间: 7年前

内容简介:翻译自:https://stackoverflow.com/questions/10726052/why-compiler-skips-assigning-variable

我有以下程序:

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;

在指定了度变量后,调试器会跳到过程的结尾.这是为什么?

这是一个优化.变量min和sec按值传递.这意味着调用者看不到对它们的修改,并且对此过程是私有的.因此,编译器可以确定分配给它们是没有意义的.永远无法读取分配给变量的值.因此编译器选择节省时间并跳过分配.

我希望你的意思是声明这样的程序:

procedure GetDegree(const num: DWORD; var degree: DWORD; var min, sec: Extended);

正如我在上一个问题中所说,使用Extended并没有多大意义.使用其中一种标准浮点类型(单或双)会更好.甚至使用映射到Double的通用Real.

此外,您已声明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;

翻译自:https://stackoverflow.com/questions/10726052/why-compiler-skips-assigning-variable


以上所述就是小编给大家介绍的《delphi – 为什么编译器会跳过赋值变量》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Algorithms to Live By

Algorithms to Live By

Brian Christian、Tom Griffiths / Henry Holt and Co. / 2016-4-19 / USD 30.00

A fascinating exploration of how insights from computer algorithms can be applied to our everyday lives, helping to solve common decision-making problems and illuminate the workings of the human mind ......一起来看看 《Algorithms to Live By》 这本书的介绍吧!

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具