The Wiert Corner – irregular stream of stuff

• Email Subscription

Join 1,728 other followers

Archive for August 13th, 2019

QualityCentral 56524: tanh function from Delphi 7 till Delphi XE was buggy; XE2 fixed it

Posted by jpluimers on 2019/08/13

In case you maintain code in older versions of Delphi, be aware that the function tanh was broken starting in Delphi 7 and only got fixed in Delphi XE2: QualityCentral QualityCentral 56524: tanh function from Delphi 7 till Delphi XE was buggy; XE2 fixed it.

For big inputs, it would just fail, instead of returning 1.

The reason is that in the buggy versions, tanh got replaced from an old working version into a simple sinh/cosh, which mathematically is correct, but if your numeric data type has limited accuracy, you need to account for the boundaries where the result fits, but intermediates do not.

the [WayBack] Math.Tanh Function implements the hyperbolic tangent, for which you can find the definition at Hyperbolic function – Wikipedia: Definitions.

By now it is implemented for all floating point types the same way (only the parameter type changes in each implementation)

function Tanh(const X: Extended): Extended; overload;
const
MaxTanhDomain = 23;
C1of3 = 1/3;
CBorder = 1.8145860519450699870567321328132e-5; // 2 ^(-63 / 4)
CLn2Div2 = 0.34657359027997265470861606072909; // Ln2 / 2
var
y, z: Extended;
begin
FClearExcept;
case TExtendedRec(X).SpecialType of
fsPositive,
fsNegative:
begin
z := X;
if X < 0 then z := -z;
if (z > MaxTanhDomain) then
Result := 1.0
else if (z < CBorder) then
Result := z  - z * z * z * c1of3
else if (z < CLn2Div2) then
begin
y := ExpMinus1(2*z);
Result := y / (2 + y);
end
else
begin
y := Exp(2*z);
Result := 1 - (2/(y + 1));
end;
if X < 0 then Result := -Result;
end;
else
Result := X;
end;
FCheckExcept;
end;

–jeroen

Powershell: what kind of data type is [string[]] and when would you use it?

Posted by jpluimers on 2019/08/13

[WayBackIn Powershell, what kind of data type is [string[]] and when would you use it? (thanks cignul9 and arco444!): basically it forces an array of string.

It defines an array of strings. Consider the following ways of initialising an array:

[PS] > [string[]]\$s1 = "foo","bar","one","two",3,4
[PS] > \$s2 = "foo","bar","one","two",3,4

[PS] > \$s1.gettype()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String[]                                 System.Array

[PS] > \$s2.gettype()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array

By default, a powershell array is an array of objects that it will cast to a particular type if necessary. Look at how it’s decided what types the 5th element of each of these are:

[PS] > \$s1.gettype()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object

[PS] > \$s2.gettype()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Int32                                    System.ValueType

[PS] > \$s1
3
[PS] > \$s2
3

The use of [string[]] when creating \$s1 has meant that a raw 3 passed to the array has been converted to a String type in contrast to an Int32 when stored in an Object array.

–jeroen

(53) Introducing the “Lab in a Box” Concept – Patrick Titiano & Kevin Hilman, BayLibre – YouTube

Posted by jpluimers on 2019/08/13