开发者

Frac function losing precision

开发者 https://www.devze.com 2022-12-23 15:56 出处:网络
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.

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.


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.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号