First of all, apologies for the large numbers, I couldn't find a working example for a far lower order and the zeros are necessary... my problem is this: I have a large polynomial (from high-order FEM) I want to evaluate in a compiled function:
fc=Compile[{x},-820.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000+447720.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 x-(9.1446810000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(7)) x^2+(9.9250271120000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(9)) x^3-(6.6993933006000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(11)) x^4+(3.0817209182760000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(13)) x^5-(1.0259562557093850000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(15)) x^6+(2.5795471572121680000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(16)) x^7-(5.0559124281358492800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(17)) x^8+(7.9158224884955216000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(18)) x^9-(1.0092673672831790040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(20)) x^10+(1.0643183145895342224000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(21)) x^11-(9.4014784455408856312000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(21)) x^12+(7.0294131146659544873280000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(22)) x^13-(4.4875271223090691593210000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(23)) x^14+(2.4637403808755673815880000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(24)) x^15-(1.1702766809158945062543000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(25)) x^16+(4.8332789236588336574094000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(25)) x^17-(1.7426655674747683575881670000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(26)) x^18+(5.5031544236045316555415800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(26)) x^19-(1.5258746356358019590365290000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(27)) x^20+(3.7214913473684776557868140000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(27)) x^21-(7.9927484619618440561784982500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(27)) x^22+(1.5123670133242584927169054080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(28)) x^23-(2.5206116888737641545281756800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(28)) x^24+(3.6968971436815207599746576640000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(28)) x^25-(4.7633097812819594407365781440000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(28)) x^26+(5.3777341591995557415212453120000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(28)) x^27-(5.3009093854967049452137989504000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(28)) x^28+(4.5402672156089686405057009920000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(28)) x^29-(3.3579059615441330570406746920000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(28)) x^30+(2.1270020166965769510873481920000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(28)) x^31-(1.1416996119033096869807089560000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(28)) x^32+(5.1203497745966616264589371360000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(27)) x^33-(1.8824815347781844214922563000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(27)) x^34+(5.5238840402757535148035320000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(26)) x^35-(1.2436814944480497679528420000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(26)) x^36+(2.0167808018076482723559600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(25)) x^37-(2.0963905703000554410015900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(24)) x^38+(1.0488508169105968435280000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(23)) x^39];
If I want to evaluate this, I get the numerical error message: "CompiledFunction::cfn: Numerical error encountered at instruction 1; proceeding with uncompiled evaluation."
If I use the uncompiled version it gets strange:
f=-820.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000+447720.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 x-(9.1446810000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(7)) x^2+(9.9250271120000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(9)) x^3-(6.6993933006000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(11)) x^4+(3.0817209182760000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(13)) x^5-(1.0259562557093850000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(15)) x^6+(2.5795471572121680000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(16)) x^7-(5.0559124281358492800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(17)) x^8+(7.9158224884955216000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(18)) x^9-(1.0092673672831790040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(20)) x^10+(1.0643183145895342224000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(21)) x^11-(9.4014784455408856312000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(21)) x^12+(7.0294131146659544873280000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(22)) x^13-(4.4875271223090691593210000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(23)) x^14+(2.4637403808755673815880000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(24)) x^15-(1.1702766809158945062543000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(25)) x^16+(4.8332789236588336574094000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(25)) x^17-(1.7426655674747683575881670000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(26)) x^18+(5.5031544236045316555415800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(26)) x^19-(1.5258746356358019590365290000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(27)) x^20+(3.7214913473684776557868140000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(27)) x^21-(7.9927484619618440561784982500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(27)) x^22+(1.5123670133242584927169054080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(28)) x^23-(2.5206116888737641545281756800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(28)) x^24+(3.6968971436815207599746576640000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(28)) x^25-(4.7633097812819594407365781440000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(28)) x^26+(5.3777341591995557415212453120000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(28)) x^27-(5.3009093854967049452137989504000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(28)) x^28+(4.5402672156089686405057009920000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(28)) x^29-(3.3579059615441330570406746920000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(28)) x^30+(2.1270020166965769510873481920000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(28)) x^31-(1.1416996119033096869807089560000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(28)) x^32+(5.1203497745966616264589371360000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(27)) x^33-(1.8824815347781844214922563000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(27)) x^34+(5.5238840402757535148035320000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(26)) x^35-(1.2436814944480497679528420000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(26)) x^36+(2.0167808018076482723559600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(25)) x^37-(2.0963905703000554410015900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(24)) x^38+(1.0488508169105968435280000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*10^(23)) x^39;
f/.x->N[7/10,16]
f/.x->0.7
Weirdly enough both evaluate to the same value if I delete all the zeros in the uncompiled function...
The function is highly oscilliating:
I think what Mathematica does is to first try to evaluate the uncompiled function symbolically and then finds that the numerical error is very high which results in the "No significant digits to display" error...
My questions are the following:
1) I want the compiled function to evaluate no matter what, I prefer a false value to a numerical error message. How can I enforce that?
2) How can I force Mathematica to always evaluate the uncompiled function?
3) Are there some "rules" for compiled functions to prevent this. I found that when I use
N[f,MachinePrecision]
and then copy/paste the result into the compiled function it works. But I would need to do that for thousands of functions...What does compile do if there is a body with numbers containing more digits than MachinePrecision can handly? Simply cut it off?
I really love Mathematica but sometimes it gets really confusing. If I plug the function into MATLAB it always evaluates for all values...its a different value though but at least I always get a value.
I really can not think of any better title than this, but I am trying to understand this and fix it for hours now and would really appreciate some help. Thanks a lot in advance!
€dit: So, the problem was that Compile[] in this case needs a body with MachinePrecision numbers. While something like
f=Compile[{x},
2934893242394239492349.239482934892394239422342342343*x
];
Precision[2934893242394239492349.239482934892394239422342342343]
f[0.333]
works perfectly fine even though the body contains numbers far higher than MachinePrecision in my case it does not. ...sometimes...for higher orders it would only work if the body has MachinePrecision. And also the code has to be copy-pasted so it would always contains the "`" after each number...If it didnt, it would also not work... If copypaste is not possible wrapping the body with Evaluate[N[...,MachinePrecision]] should do the trick. At least I hope so after some more testing... Thanks everyone for the help!
double
(and its complex counterpart). So,N[f,MachinePrecision]
rounds all numbers in the expressionf
to machine precision and this is why it works afterwards. $\endgroup$fc = Compile[{x}, Evaluate[N[f]]];
$\endgroup$