c++ - wrong ans when using long double and correct when using double -
when declared ans long double , used "%lf" in printf, i'm getting -0.000 output. when declared ans double , used "%lf" in printf, i'm getting correct ans
#include <iostream> #include <stdio.h> #include <algorithm> using namespace std; #define ll long long int int main() { ll i,j,k,n,l,a[100000],ma; cin>>n>>l; for(i=0;i<n;i++) { cin>>a[i]; } sort(a,a+n); ma=0; for(i=1;i<n;i++) { if(a[i]-a[i-1]>ma)ma=a[i]-a[i-1]; } ll ans1,ans2; ans1=a[0]; ans2=l-a[n-1]; long double ans=max(ans1,ans2); long double rap=ma/2.000; if(rap>ans)ans=rap; printf("%lf",ans); return 0; } thanks in advance
the issue in last prinft("%lf", ans); function. specify format lf, when you're trying pass double fails. using cout instead solves issue, or using f printf format. btw, code "fails" double, , "works" long double, not other way around.
Comments
Post a Comment