3

Example:

import graph;
import gsl;
size(12cm,false);
defaultpen(linewidth(.7bp));
real f(real x){return x^2;}
real g(real x){return 2^x;}

draw(graph(f,-1.5,5),red);
draw(graph(g,-1.5,5),blue);

draw(Label("$x$",EndPoint,align=S),(-2,0)--(5,0),Arrow(TeXHead));
draw(Label("$y$",EndPoint,align=W),(0,-3)--(0,g(5)),Arrow(TeXHead));

real margin=.1;
for (int i: new int[]{2,4}){
draw(Label("$"+(string) i+"$",Relative(0)),(i,0)-(0,margin)--(i,0)+(0,margin));
draw(Label("$"+(string) g(i)+"$",Relative(0)),(0,g(i))-(margin,0)--(0,g(i))+(margin,0));
draw((i,0)--(i,g(i))--(0,g(i)),linetype(new real[]{5,5}));
}

pair z[]=intersectionpoints(graph(f,-1.5,5),graph(g,-1.5,5));
arrow(Label(format("$z[0].x=%.50f$",z[0].x)),z[0],dir=dir(-90));
real Lam=-2*Wm1(log(2)/2)/log(2);
arrow(Label(format("$Lambert W=%.50f$",Lam)),z[0],dir=dir(90),.5cm);
dot(z);
label("$0$",0,dir(-135));

enter image description here

Support:

https://www.wolframalpha.com/input/?i=x%5E2%3D2%5Ex https://stackoverflow.com/questions/8345581/c-printf-a-float-value

Question:

  1. Why Asymptote get two different results and what is the best approximation?

  2. Although %.50f but only 42 decimal digits after the comma as my output!! Do you have a exact output?

4

I would trust the gsl result here. You obtain the Asymptote result by intersecting two paths that represent the graphs of functions. However, these paths are only approximately the graphs of functions; they are hoped to be accurate up to human vision, but not for 50 digits. If you want to make the path-intersection result more accurate, you can try something like

pair z[]=intersectionpoints(graph(f,-1.5,5, n=200, Hermite),graph(g,-1.5,5, n=200, Hermite));

and keep increasing n for more accurate results (at the cost of significantly more computation time). But the intersectionpoints function uses "machine precision" at best; assuming that means 64-bit floats, you're never, ever going to get more than 16 significant digits in the base-10 representation via the Asymptote method.

Concerning the second question: the real type is a 64-bit float under the hood, which means in your decimal printout, everything after the first 16 digits or so is complete garbage. I'm not sure why you get 40 digits; I would guess this is an exact representation of the base-2 float that is the "true" result, but that can't be true because if it were the final digit would be 5, not 4.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy