9 Replies - 429 Views - Last Post: 27 October 2013 - 09:17 AM Rate Topic: -----

#1 alexe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 23-May 13

Wrong output-what can I change?

Posted 25 October 2013 - 12:35 PM

#include <stdio.h>
#include <math.h>

int main()
{
    double c;
    c=0.0131798;
    printf("c=%lf\n",c);
}



When I run the program,the output is c=0.013180.What can I change in order to get the exact result?

This post has been edited by alexe: 25 October 2013 - 12:35 PM

Is This A Good Question/Topic? 0
  • +

Replies To: Wrong output-what can I change?

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1628
  • View blog
  • Posts: 3,089
  • Joined: 30-May 10

Re: Wrong output-what can I change?

Posted 25 October 2013 - 12:39 PM

It's a float, there's no such thing as 'exact'.
http://docs.oracle.c...g_goldberg.html

Also, field width is important.
printf("c=%.7f\n",c);
Was This Post Helpful? 0
  • +
  • -

#3 alexe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 23-May 13

Re: Wrong output-what can I change?

Posted 25 October 2013 - 01:24 PM

But...if I have to count this:
m=pow(f,2)-4*u*v;

where f=-3.5668,u=1.2,v=0.0131798

what can I do,so that the output has 12 numbers instead of 7???
The output is m=2.968634 but I want it to be m=2.96863359528 :helpsmilie:
Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1311
  • View blog
  • Posts: 4,503
  • Joined: 19-February 09

Re: Wrong output-what can I change?

Posted 25 October 2013 - 07:27 PM

Hi, you can also use an argument to specify the precision.

int main()
{
  double c;
  c = 2.96863359528;
  printf("c=%.*lf\n", 12, c);

  return 0;
}


Was This Post Helpful? 0
  • +
  • -

#5 alexe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 23-May 13

Re: Wrong output-what can I change?

Posted 26 October 2013 - 06:32 AM

View Post#define, on 25 October 2013 - 07:27 PM, said:

Hi, you can also use an argument to specify the precision.

int main()
{
  double c;
  c = 2.96863359528;
  printf("c=%.*lf\n", 12, c);

  return 0;
}



But then the output is 2.968634 (with blankets)..
I want the number c, to has a precision of 12 digits!!!How can I do that???
Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1311
  • View blog
  • Posts: 4,503
  • Joined: 19-February 09

Re: Wrong output-what can I change?

Posted 26 October 2013 - 08:15 AM

You can check the characteristics of floating-point types on your machine/compiler using float.h.
It is the type that has a specific precision eg long double, with printf you are only specifying the number of digits to print.

#include <stdio.h>
#include <float.h>

int main()
{
  double c;
  double n;
  c = 2.96863359528;
  n = 0.12345678901234567890;

  printf("c=%.*lf\n", 12, c);
  printf("n=%.20lf\n", n);
  printf("DBL_DIG=%d \n", DBL_DIG );

  return 0;
}



I got this output:
c=2.968633595280
n=0.12345678901234567740
DBL_DIG=15

Was This Post Helpful? 0
  • +
  • -

#7 alexe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 23-May 13

Re: Wrong output-what can I change?

Posted 26 October 2013 - 09:01 AM

I get the same result..But after I print it,I have also to use it,to make some calculations and then the number has just a precision of 6 numbers,instead of 12,so I don't get the right result :helpsmilie:
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon


Reputation: 3992
  • View blog
  • Posts: 12,321
  • Joined: 25-December 09

Re: Wrong output-what can I change?

Posted 26 October 2013 - 09:32 AM

Quote

have also to use it,to make some calculations and then the number has just a precision of 6 numbers,instead of 12

A double has a precision of at least 15 digits when used in calculations. When displaying a double with printf() if you want the double to print more than the default display precision you need to specify the precision in your printf() call every time.

You really need to read that link provided by Salem_c in post #2.



Jim
Was This Post Helpful? 0
  • +
  • -

#9 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1311
  • View blog
  • Posts: 4,503
  • Joined: 19-February 09

Re: Wrong output-what can I change?

Posted 26 October 2013 - 11:07 AM

You need to give us an example of code that doesn't work as you expect. It could be a number of things.
Was This Post Helpful? 0
  • +
  • -

#10 alexe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 23-May 13

Re: Wrong output-what can I change?

Posted 27 October 2013 - 09:17 AM

I got it now...Thank you very much, guys!!!! :bigsmile:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1