-1

when i declared ans as long double and used "%Lf" in printf, i'm getting always -0.000 as output. But when i declared ans as double and 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

rap
  • 9
  • 4

1 Answers1

1

The issue is in your last prinft("%Lf", ans); function. You specify the format Lf, so when you're trying to pass a double it fails. Using cout instead solves the issue, or simply using f as the printf format. BTW, your code "fails" for double, and "works" for long double, not the other way around.

vsoftco
  • 55,410
  • 12
  • 139
  • 252
  • when i declared ans as long double and used "%Lf" in printf, i'm getting always -0.000. But when i declared ans as double and used "%lf" in printf, i'm getting correct ans – rap May 31 '15 at 07:06
  • @rap possibly your implementation doesn't support %Lf which is why I asked in comments on your question – M.M May 31 '15 at 10:09