Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. #include <cmath>
  2. #include <iostream>
  3. #include <iomanip>
  4. using namespace std;
  5. double lambertW(double x, double prec=1e-9){
  6.     double w = 0;
  7.     for(int i=0;i<100;i++){
  8.         double wTimesExpW = w*exp(w);
  9.         double wPlusOneTimesExpW = (w+1)*exp(w);
  10.         w -= (wTimesExpW-x)/(wPlusOneTimesExpW-(w+2)*(wTimesExpW-x)/(2*w+2));
  11.         //cout <<w<<endl;
  12.         if (prec > abs((x-wTimesExpW)/wPlusOneTimesExpW)){
  13.             return w;
  14.  
  15.         }
  16.        
  17.            
  18.     /*  if (prec <= abs((x-wTimesExpW)/wPlusOneTimesExpW)){
  19.             cout <<"W(x) ne shoditsa x=" <<x<<endl;}*/
  20.     }
  21. return NAN;
  22. }
  23. double lambertW1(double x, double prec=1e-12){
  24.     double w=1.0;
  25.     for(int i=0;i<100;i++){
  26.         w=-log(-x/w);
  27.     }
  28.     return w;
  29. }
  30. int main()
  31. {
  32. double a,b;
  33. cin>>a>>b;
  34. if(a==0){
  35.     if(b==0)
  36.     cout<<"no solution exists";
  37.     else
  38.     cout<<fixed<<setprecision(9)<<log(b);
  39.     return 0;
  40. }
  41. double x1=(-b/a)-lambertW(-1/(a*pow(M_E,b/a)));
  42. double x2=(-b/a)+lambertW1(-1/(a*pow(M_E,b/a)));
  43. if(x1==0)x1=abs(x1);
  44. if(x2==0)x2=abs(x2);
  45. if(isnan(x1)) {
  46.     if(isnan(x2)) {
  47.         cout<<"no solution exists";
  48.         return 0;
  49.     }else{
  50.         cout<<fixed<<setprecision(9)<<x2;
  51.         return 0;
  52.     }
  53. }else{
  54.     if(isnan(x2)) {
  55.         cout<<fixed<<setprecision(9)<<x1;
  56.         return 0;
  57.     }
  58. }
  59. if(x1<x2)
  60.     cout<<fixed<<setprecision(9)<<x1<<" "<<fixed<<setprecision(9)<<x2;
  61. else
  62.     cout<<fixed<<setprecision(9)<<x2<<" "<<fixed<<setprecision(9)<<x1;
  63. return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement