Frac 函数失去精度
我有一个 TDateTime 变量,在运行时分配的值是 40510.416667。当我使用 Frac 函数将时间提取到 TTime 类型变量时,它将其设置为 0.41666666666。为什么它改变了值的精度?是否有解决方法来保留原始值的精度,即。将其设置为 0.416667。
I have a TDateTime variable which is assigned a value at runtime of 40510.416667. When I extract the time to a TTime type variable using the Frac function, it sets it to 0.41666666666. Why has it changed the precision of the value and is there a workround to retain the precision from the original value ie. to set it to 0.416667.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
TDateTime 是一个浮点数。有些数字无法精确表示为浮点数。 0.416667 / 0.41666666666 似乎是另一个。
您可以四舍五入到 5 或 6 位数字进行显示。这可以让你的准确度达到 1 秒左右。
TDateTime is a floating point number. Some numbers can't be represented exactly as a floating point number. 0.416667 / 0.41666666666 would seem to be another one.
You can round to 5 or 6 digits for display. That gets you accuracy to around 1 second.
每个计算机科学家应该了解的浮点数知识帮助,SO自己的浮点精度也应该有帮助 - 这将为您提供一些详细信息杰夫的回答。
What Every Computer Scientist Should Know About Floating-Point Numbers should help, as should SO's own Precision of Floating Point - that will give you some detailed information to go with Jeff's answer.
精度损失的原因之一是 TDateTime 是 double,而 Frac 的参数和返回值是 Extended 类型。
将浮点从一种类型转换为另一种类型时,一些精度可能会丢失。 (对它们进行算术运算时也是如此)。
要正确比较浮点值,您应该使用 Math 单元中的 CompareValue 函数。
One of the reason for the loss of precision is that TDateTime is a double, and Frac's parameter and return value is of type Extended.
When converting floating points from one type to another, some precision can be lost. (Same goes when doing arithmetic on them).
To compare float value correctly, you should use the CompareValue function from the unit Math.
感谢您对此的所有帮助,非常感谢。为了解决由于精度变化而出现的问题,我使用 CompareTime 函数而不是 >= 或 <= 运算符来比较时间。
Thanks for all your help on this, much appreciated. To get round my problen that was arising due to the change in precision I used the CompareTime function instead of the >= or <= operators for comparing the times.