0

I have a double value that I need to access to inside a backgroundThread. I would like to use somethink like AtomiccmpExchange but seam to not work with double. is their any other equivalent that I can use with double ? I would like to avoid to use Tmonitor.enter / Tmonitor.exit as I need something the most fast as possible. I m under android/ios so under firemonkey

noa
  • 59
  • 8

2 Answers2

2

You could type cast the double values into UInt64 values:

PUInt64(@dOld)^ := AtomicCmpExchange(PUInt64(@d)^,PUInt64(@dNew)^,PUInt64(@dCom‌​p)^); 

Note that you need to align the variables properly, according to platforms specifications.


As @David pointed out, comparing doublevalues is not the same as comparing UInt64 values. There are some specific double values that will behave out of the ordinary:

  • A NaN is normally (as specified in IEEE-754) detected by comparing a value by itself.

    IsNaN := d <> d; 
    

    footnote: Delphi default exception handler is triggered in the event of comparing a NaN, but other compilers may behave differently. In Delphi there is an IsNaN() function to use instead.

  • Likewise the value zero could be both positive and negative, for a special meaning. Comparing double 0 with double -0 will return true, but comparing the memory footprint will return false.

LU RD
  • 34,438
  • 5
  • 88
  • 296
  • What is `d <> d`? Could you elaborate that, please? It's some kind of compiler _magic_ or so? I'm aware of `IsNan` function, but that does internally something else than you present. – Victoria Oct 13 '17 at 22:17
  • @Victoria, [IEEE 754](https://en.wikipedia.org/wiki/NaN) specifies that a floating point variable with a value of NaN gives false when compared for equality. (Details in the link). This is often used in systems where a sensor can give NaN's as a result of certain conditions. Then you have to test for NaN before proceeding with calculations. – LU RD Oct 13 '17 at 22:37
  • Thank you, but why is `IsNan` function in Delphi implemented in a different way? – Victoria Oct 13 '17 at 22:52
  • @Victoria, I'm aware that the Delphi's math unit has another way of testing (at bit level). I don't know why at the moment, but many industri c compilers operate as described. A quiet NaN is not supposed to signal an exception when loaded into the FPU, but that is what will happen in Delphi. More on the subject [NaN](http://www.wow.com/wiki/NaN). Feel free to ask a question about this subject here at SO. A similar question about C++ is here :https://stackoverflow.com/a/570694/576719 – LU RD Oct 13 '17 at 23:11
  • Well, so long I'm not a high reputation user asking how is something implemented in Delphi in a certain way, it could be a bad received question (which I don't care) which might be finally deleted by _haters_ (which is wasting of helper's time). I know assembler very well just for a subset of a few microcontrollers, not much about assembler of _PC_ processors. You seem to be keen about it, hence I asked :) – Victoria Oct 13 '17 at 23:27
  • Don't be afraid of asking. The subject has been touched, but I have never seen a straight Delphi question. My guess is that the exception mask is set to trigger all NaN's for convenience, and since there is a function supplied to test for NaN's, all was fine. Finally the SO link I was looking for: [What is the rationale for all comparisons returning false for IEEE754 NaN values?](https://stackoverflow.com/q/1565164/576719). – LU RD Oct 14 '17 at 08:38
0

Maybe use of System.SyncObjs.TInterlocked class methods will be better?