Jump to content

David Heffernan

Members
  • Content Count

    3709
  • Joined

  • Last visited

  • Days Won

    185

Posts posted by David Heffernan


  1. On 6/7/2025 at 9:22 AM, dummzeuch said:

    OK, I bite: What's the problem with SameValue?

    The principle of a function that compares real values for equality, up to a specified tolerance, is a valid thing to do in many cases. But there are lots of caveats. In practise, most developers (and far from just in the Delphi space) that I see recommending it are completely unaware of these caveats. Some of these caveats and issues:

    1. How do you choose a tolerance? Does your tolerance account for the scale of the values, and indeed should it? Some use cases demand absolute tolerances, some demand relative tolerances.
    2. If you are accounting for scale, how do you choose the scale? Is it based on the pair of values being compared, or should it be based from the total pool of values. For instance, you might have two series that you wish to compare. Shouldn't the scale be based on the series rather than individual samples? Or maybe it is individual samples.
    3. It's easy to mistake this as an equality, but the resulting relationship implied by equality to tolerance is not transitive, so is not a mathematical equality. That is a R b and b R c does not imply a R c.

    Looking more specifically at Delphi's SameValue, the tolerance used when the Epsilon parameter is zero (or omitted) is very odd. I definitely think puppies are dying left, right and centre when that code path is chosen.

     

    One of the common misconceptions with floating point is that it is not exact. I think of it as exact, subject to the rules of the domain, but the key point is that not all values are representable. So if you have floating point values a and b, then they represent some precise real value. But when you do a * b, say, then the true value may not be representable. And so the result is the closest representable value. This is well defined, and reproducible. A lot of people think that there's just some random errors and fuzz in it all. That FuzzFactor constant in the RTL source seems to be a classic example of that sort of thinking. This famous question on SO is a useful resource: https://stackoverflow.com/questions/588004/is-floating-point-math-broken

     

    One of my pet bug bears in Delphi is its inability to convert correctly between floating point and textual representations. In every other mainstream language (and most non-mainstream languages) this is possible. But in Delphi the code used to perform these conversions is home grown and broken. There are good algorithms for doing this, and it's a subject of active research, but Embarcadero don't seem to care about this. In my codebase I use correct algorithms. Which means that for all values I can convert from float to text and back and always get the same value. The inability to do this often leads to users calling SameValue.

     

    My own codebase does call comparison function that compare for equality to tolerance. But there is a lot of care taken in how the tolerance is chosen and applied.

     

    I guess that's the crux of what I am saying. So many people just say, this is hard, slap a tolerance onto the comparison that is good enough for the two values I have to hand, and surely that's fine for all other values! I'm a bit of a pedant in this area, I admit. But it's kind of my job to be. Sorry!

    • Like 1
    • Thanks 1

  2. 23 minutes ago, Paul H said:

    Thanks David - I was hoping to find something with H-M-S support including setting / getting in decimals the way you can with TTime. It is probably no more than a couple of hours work to do something basic with a record helper, I just didn't want to duplicate something that already exists.

    I mean you didn't say any of this in your original post. Clearly you knew what your requirements were, but we can't read your kind! 


  3. It depends a bit on what your goals are. But if you want to test equality then you need to put the expected value into a single. That's hard to do in a compiler because delphi literal syntax doesn't allow to specify the type of a floating point literal. You could declare a typed constant of single type and compare against that. 


  4. 19 hours ago, jesu said:

    Thanks for all the suggestions. I have the feeling that VCL should somehow protect events that can recreate controls. Something like 

     

    
    if Creating then ForceQueue incompatible operations

     

    Before reaching this conclusion, does there exist a clear diagnosis of the problem. That hasn't been shown here yet, so I'd want to understand the problem before trying to solve it. 


  5. 1 hour ago, Typer2 said:

    It’s generally not advisable to change the font of a form or visual control directly from within an event handler like OnClick, especially for controls that are part of the form itself. Internally, setting Font.Name or Font.Size can lead to a call to CreateWnd, which may cause issues if it's done while Windows messages are still being processed for the same control—resulting in access violations like the one you're seeing in COMCTL32.dll.

    This is incorrect. Changing font properties in an event handler is fine.


  6. 2 hours ago, DelphiUdIT said:

    It is not mine, I'm not a chemical tech. I only see the use of the "absolute" directive and ask if there is any issue (in assignement for example). By the way I have tested some uses case and i don't found any issue, it's like a normal array of records.

    Like I wrote, I had know that there are other methods, I was only curious about "absolute" used in array of records.

     

    Thank you all for your interest.

    It has nothing to do with assignment. It's pretty rare for absolute to be useful. Pretend it doesn't exists, and you'll get a long way. 

    • Thanks 1

  7. On 11/24/2023 at 6:43 AM, robertjohns said:

    but on my system TOSVersion.ToString returns Windows 10

    We've no idea what your system is. There are many versions of Delphi. We don't know what your app manifest is. These are both possibly relevant. 

    • Like 1

  8. Isn't this a well documented file system for which there are multiple open source projects that you can use to support the documentation? 

     

    More generally, I'd advise you to put more effort into asking questions. Time spent describing what your problem is, what you already know, where you are blocked, etc. is effort you should spend if you want to get something in return. 


  9. 43 minutes ago, alogrep said:

    Thanks to all.

    No, the division by zero was an exampte (and a test I actually didi).

    It is all exceptions (that are not handled)

    The more critical one is when I set a table to an index name that does not esxist. 

    The app just exit, no wrnings.

    Doesn't sound like it's not that exceptions aren't handled. App exiting is entirely different. How about the details we asked for. 

×