Hi Guillermo,

This is unfortunately an example of why one should (almost) never do exact 
equality checks with floating-point numbers, which are unfortunately rarely 
precisely the quantity we think of them as. PDL’s “power” operation uses the 
“pow” libm function (or rather, since we use “tgmath” to simplify the code, 
really the “pow” family of functions), and is entirely subject to its whims:

pdl> p $x=pdl(-3)
-3
pdl> p $x**2-9
0
pdl> p $z=-3+0*i
-3
pdl> p $z->info
PDL: CDouble D []
pdl> p $x->info
PDL: Double D []
pdl> p $z**2-9
1.77635683940025e-15-2.20436423846524e-15i

The “info” calls show the types, which are as expected. If you want a complex 
-3, you can just do cdouble(-3). I infer that “pow”, with complex numbers, may 
convert to a polar format, and a tiny instability is introduced. But the real 
solution here is to not use ==, but instead to do something like:

If (abs($quantity1-$quantity2) < $tolerance) ...

Best regards,
Ed

From: Guillermo P. Ortiz<mailto:gor...@exa.unne.edu.ar>
Sent: 13 April 2023 15:05
To: pdl-de...@lists.sourceforge.net<mailto:pdl-de...@lists.sourceforge.net>; 
pdl-general@lists.sourceforge.net<mailto:pdl-general@lists.sourceforge.net>
Subject: [Pdl-devel] unexpected results

Dear pdl folks,

doing $x=pdl(-3)
with PDL v2.081 we obtain $x**2-9==0
but, defining $z=-3+0*i
$z**2-9 =! 0
while
$z*$z-9 ==0

May be integer power are computed differently for real
but not for intrinsic complex? (logarithmically for any power)

Regards


--


Dr. Guillermo P. Ortiz
Electromagnetismo Aplicado
Dto. Física, Facultad de Ciencias Exactas
Universidad Nacional del Nordeste
Avda Libertad 5460, W3404AAS Corrientes,
Argentina.
(+54) 379-4424678 interno 4613
gortiz at unne edu ar

_______________________________________________
pdl-general mailing list
pdl-general@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/pdl-general

Reply via email to