0

This might be a commonly known thing, but I wonder if there is any best practice of dealing with data spill-over in Delphi.

Say I have a variable Dt that is assigned a value of 0.01 in code. When Delphi runs through it, the value of Dt in debug mode is 0.0099999997765, slightly off the actual value of 0.01. Although the values are not significantly different, using Dt in an iteration of 10000 steps will lead to it.

Any suggestion on how to force Dt to 0.01? Also, I am using Delphi 2003, so there are constrains on what I can do.

Dt:= 0.01    //Debug value = 0.0099999997765
.
for I=0 to 10000
   temp := temp + Dt;  

The issue is more prominent as temp starts to spill, as well resulting in an answer that far off what is expected. Any suggestions?

Ken White
  • 123,280
  • 14
  • 225
  • 444
SamAct
  • 529
  • 4
  • 23
  • 3
    This is how floating-point numbers work. It is impossible to "force" `Dt` to be exactly 0.01. But you are clearly using single-precision variables. You get much better approximations if you instead use double-precision variables. – Andreas Rejbrand Oct 22 '20 at 16:33
  • 1
    Or don't use floating points at all if you only need a fixed position - i.e. when only down to 0.0001 is needed then just use `Integer` or `Int64` and operate on that, and only when outputting it divide it by 10000. – AmigoJack Oct 22 '20 at 16:54
  • A [document](https://www.phys.uconn.edu/~rozman/Courses/P2200_15F/downloads/floating-point-guide-2015-10-15.pdf) usually referred to. – Tom Brunberg Oct 22 '20 at 17:05
  • What is the actual data type of `Dt`? If it is a `TDateTime` then don't use floating-point values directly at all. `TDateTime` uses a `Double` internally, but is not treated as a normal floating-point number. Use the `TDateTime`-related functions in the `SysUtils` and `DateUtils` units to manipulate `TDateTime` values without losing precision. – Remy Lebeau Oct 22 '20 at 17:07
  • 1
    @RemyLebeau: I'd guess `Dt` is a time difference, like `Δt`. – Andreas Rejbrand Oct 22 '20 at 17:10
  • Some brilliant minds commented on a similar question here: https://stackoverflow.com/q/23667061/822072 – James L. Oct 23 '20 at 20:14
  • 1
    0.01 cannot be represented as a binary floating point number. Here is its nearest representation as an Extended, Double, and Single value: http://pages.cs.wisc.edu/~rkennedy/exact-float?number=0.01 – James L. Oct 23 '20 at 20:20

0 Answers0