1
$\begingroup$

So, I have two lists, and a function. If I apply my function to both lists in a "listable" manner, I have one result, but if I apply it to lists' elements one-by-one, in a "map" manner, I get slightly different result.

How do I fix this?

a = 71.00428009033203125;
b = 71.5652618408203125;
tstFunc[x_, y_] := 0.04686746379620325 + 0.998597351069296 x - 0.00002667617826545349 x^2 - 1.1147778875484568*^-6 x^3 - 9.545047757044727*^-10 x^4 + 1.9551823847676841*^-10 x^5 - 0.00033229168991462903 y - 4.383544784477607*^-6 x y - 5.960020622382784*^-8 x^2 y - 5.884558939835153*^-10 x^3 y + 2.6085442056704577*^-12 x^4 y + 2.297317388713515*^-6 y^2 - 9.645541773068444*^-8 x y^2 - 9.511441168001075*^-10 x^2 y^2 + 3.4034398709508465*^-12 x^3 y^2 + 1.1686028576733287*^-7 y^3 + 1.1067252598785332*^-9 x y^3 + 9.186247574157861*^-12 x^2 y^3 + 1.4473334726028848*^-10 y^4 + 9.081366085213091*^-12 x y^4 - 6.388659545763774*^-12 y^5;
alist = ConstantArray[a, 1000];
blist = ConstantArray[b, 1000];
tstFunc[alist[[1]], blist[[1]]] - tstFunc[alist, blist][[1]]

Delivers a difference:

minute difference

Note that if lists are not that long, there's no dirrefence in the same calculation:

alist = ConstantArray[a, 100];
blist = ConstantArray[b, 100];
tstFunc[alist[[1]], blist[[1]]] - tstFunc[alist, blist][[1]]

zero difference

I don't understand how this works. I need consistancy in my calculations. Please help!

$\endgroup$
1
  • $\begingroup$ Strange. NumberForm[tstFunc[alist[[1]], blist[[1]]], 16] is 70.72441187221068 and NumberForm[tstFunc[alist, blist][[1]], 16] is 70.72441187221067 $\endgroup$
    – MelaGo
    Oct 7, 2023 at 19:31

1 Answer 1

2
$\begingroup$
Clear["Global`*"]

Machine precision calculations do not track nor try to control precision. Precision is lost by most calculations and very intensive calculations can lose a lot of precision. You get what you get. Using exact numbers gives exact results:

a = 71.00428009033203125 // Rationalize[#, 0] &;
b = 71.5652618408203125 // Rationalize[#, 0] &;

tstFunc[x_, y_] := 
  Evaluate[0.04686746379620325 + 0.998597351069296 x - 
     0.00002667617826545349 x^2 - 1.1147778875484568*^-6 x^3 - 
     9.545047757044727*^-10 x^4 + 1.9551823847676841*^-10 x^5 - 
     0.00033229168991462903 y - 4.383544784477607*^-6 x y - 
     5.960020622382784*^-8 x^2 y - 5.884558939835153*^-10 x^3 y + 
     2.6085442056704577*^-12 x^4 y + 2.297317388713515*^-6 y^2 - 
     9.645541773068444*^-8 x y^2 - 9.511441168001075*^-10 x^2 y^2 + 
     3.4034398709508465*^-12 x^3 y^2 + 1.1686028576733287*^-7 y^3 + 
     1.1067252598785332*^-9 x y^3 + 9.186247574157861*^-12 x^2 y^3 + 
     1.4473334726028848*^-10 y^4 + 9.081366085213091*^-12 x y^4 - 
     6.388659545763774*^-12 y^5 // Rationalize[#, 0] &];
alist = ConstantArray[a, 1000];
blist = ConstantArray[b, 1000];
tstFunc[alist[[1]], blist[[1]]] - tstFunc[alist, blist][[1]]

(* 0 *)

Alternatively, using arbitrary precision will use inexact values but track and try to control precision.

a = 71.00428009033203125 // SetPrecision[#, 20] &;
b = 71.5652618408203125 // SetPrecision[#, 20] &;

tstFunc2[x_, y_] := Evaluate[tstFunc[x, y] // SetPrecision[#, 20] &];
alist = ConstantArray[a, 1000];
blist = ConstantArray[b, 1000];
tstFunc2[alist[[1]], blist[[1]]] - tstFunc2[alist, blist][[1]]

(* 0.*10^-18 *)
$\endgroup$
1
  • $\begingroup$ Thank you Bob! OMG, both methods are so much slower than just "calc the list" ((( oh well. $\endgroup$
    – Anton
    Oct 8, 2023 at 13:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.