-
Content Count
3709 -
Joined
-
Last visited
-
Days Won
185
Posts posted by David Heffernan
-
-
You can see your project, we cannot.
-
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:
- 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.
- 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.
- 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!
-
1
-
1
-
1 hour ago, Kas Ob. said:And yes should renamed to be Delta there
I don't think that the parameter to SameValue should be named either epsilon or delta, it should be tol or tolerance. Or am I misunderstanding?
-
8 minutes ago, Paul H said:I did say originally I wanted a counter with H-M-S support, which (to me at least) implies decimal input/output. Sorry if that wasn't clear to you.
Yes, my fault. Sorry.
-
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!
-
var Counter: UInt64;
-
My advice is to understand a problem before looking for a solution. At the moment it's clear that the problem still eludes you. Concentrate on that first.
-
Every time someone calls SameValue a puppy dies
-
3
-
1
-
-
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.
-
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.
-
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.
-
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.
-
1
-
-
1 hour ago, DelphiUdIT said:I always use Assigned(...) hoping that one day or another it will solve all the situations (either nil or an invalid pointer) 🙂
It will never find invalid pointers, but that's not why Assigned exists
-
2 hours ago, Stefan Glienke said:where mass would be a number instead of a string
I think the use of string here is to allow encoding of indeterminate masses for elements with no stable isotopes, or something along those lines. Look at the items with mass surrounded by parens.
-
What are you trying to achieve here? As Stefan says, why aren't you declaring these as records to begin with? And why would you want to make anything here a variable. Seems like this area of Physics is pretty much fixed!
-
1
-
1
-
-
7 hours ago, pmcgee said:@Stefan Glienke do you distinguish between the terms 'anonymous method' and 'anonymous function'?
I have mostly heard the word 'method' being used for something like "procedure of object / function of object".It's the official name for them, as you can see from the documentation
https://docwiki.embarcadero.com/RADStudio/en/Anonymous_Methods_in_Delphi
-
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.
-
1
-
-
Whether or not you want to use asm is not relevant. It's what you do with it that matters.
I mean, exactly what you said above would apply equally if you wanted to write the code in pascal.
-
3 minutes ago, dummzeuch said:Maybe if you want to use ASM to access the parameter?
Why would [ref] be needed to use asm?
-
Is there any known use case other than the one demonstrated by FreeAndNil, namely to allow modification of a const param?
-
Is there any known use case other than the one demonstrated by FreeAndNil, namely to allow modification of a const param?
-
McAfee, like so many AV products, the cure is worse than the disease.
-
2
-
-
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.
-
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.
What .PAS file does a form use?
in General Help
Posted
It could also be TForm and everything assembled at runtime. @Anders Melander already ended all debate in this thread.