views:

183

answers:

4

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.

+2  A: 

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.

Jeff Cuscutis
+2  A: 

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.

Ken White
+1  A: 

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.

Ken Bourassa
+1  A: 

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.

PeteDaMeat