Using Double Precision

A very basic question about getting it to function properly

Page 1 of 1

9 Replies - 21225 Views - Last Post: 05 September 2009 - 11:56 AM Rate Topic: -----

#1 Okaya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-August 09

Using Double Precision

Post icon  Posted 04 September 2009 - 09:06 AM

Hello to all,

Here's a very basic question about using double precision which I seem to not find the answer to. Any help or references where I could look into this further would be appreciated.

I'm currently using the CodeBlocks IDE, which in turn includes GCC as it's compiler. I've been getting this same issue occur in the Dev-C++ IDE (which also uses GCC).

Essentially, the issue is that I do not seem to get any higher precision by defining variables as anything more precise than float variables. However, as far as I know, there is no need for any special libraries to be included for the use of double precision (or even long double, I think. Is it so?). If this is indeed the case, then I suppose that I should be able to use double precision directly.

I am able to use the type double without any compiler errors. It is simply that my program does not actually perform any better with double than it does with float.

For example, here's a sample program:
#include <stdio.h>


int main()
{


float b=3.1415926535897932384626433832795;

double c=3.1415926535897932384626433832795;

printf("Assigned value for Pi:3.1415926535897932384626433832795\n\n");
printf("Pi as float:%f\n\n",b);
printf("Pi as double:%lf\n\n",c);


return 0;
}



The output I get from this program is:

Quote

Assigned value for Pi:3.1415926535897932384626433832795

Pi as float:3.141593

Pi as double:3.141593



I suppose that 'Pi as double' should definitely retain more decimals than 'Pi as float', were this working properly. Am I using this wrong?


Thank you very much for any feedback, which is much appreciated.

Okaya

Is This A Good Question/Topic? 0
  • +

Replies To: Using Double Precision

#2 debjit625  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 51
  • View blog
  • Posts: 446
  • Joined: 06-September 08

Re: Using Double Precision

Posted 04 September 2009 - 09:11 AM

Your code will be this
#include <stdio.h>


int main()
{
float b=3.1415926535897932384626433832795;
double c=3.1415926535897932384626433832795;

printf("Assigned value for Pi:3.1415926535897932384626433832795\n\n");
printf("Pi as float:%f\n\n",b);

printf("Pi as double:%d\n\n",c); //Here "%d" for double

return 0;
}


for using float or double you dont need a special libraries its in the language ,our processor handle floating points in a special processor call co-processor known as FPU it resides inside you main processor (CPU).it use a format to understand floating numbers this format is standard by IEEE search google about "floating point numbers".
Good luck

This post has been edited by debjit625: 04 September 2009 - 09:18 AM

Was This Post Helpful? 0
  • +
  • -

#3 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Using Double Precision

Posted 04 September 2009 - 09:59 AM

you need to specify the number of digits to print after the decimal point, e.g.
#include <stdio.h>
#include <float.h>

int main()
{
float b=3.1415926535897932384626433832795;
double c=3.1415926535897932384626433832795;

printf("Assigned value for Pi:3.1415926535897932384626433832795\n\n");
printf("Pi as float:%.20f\n\n",b);
printf("Pi as double:%.20lf\n\n",c);
printf("precision float %d double %d\n", FLT_DIG, DBL_DIG);
return 0;
}



prints
Assigned value for Pi:3.1415926535897932384626433832795

Pi as float:3.14159274101257320000

Pi as double:3.14159265358979310000

precision float 6 double 15


you can see that float has a precision of 6 or 7 significant digits and double 15 or 16 significant digits

the header file <float.h> contains definitions of implementation dependent constants for floating point types

This post has been edited by horace: 04 September 2009 - 10:12 AM

Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Using Double Precision

Posted 04 September 2009 - 10:04 AM

If your just trying to display more digits then you can just use setPrecision();
#include <iostream>
#include <iomanip>

int main()
{
	double pi = 3.1415926535897932384626433832795;
	std::cout << std::setprecision (9) << pi<< std::endl;
	std::cin.get();
	return 0;
}


although i think you can only display up to 12 digits with this method.
Was This Post Helpful? 0
  • +
  • -

#5 Okaya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-August 09

Re: Using Double Precision

Posted 04 September 2009 - 02:45 PM

Thank you very much debjit625, horace and ImaSexy for the replies provided. They are quite appreciated.

debjit625, thank you for the additional information on the FPU. However, you provide a code where you have changed the "%lf" in the printf() function for printing the double variable 'c' into "%d".
double c=3.1415926535897932384626433832795;

/*-----part of code is missing here-----*/

printf("Pi as double:%d\n\n",c); //Here "%d" for double

When I compile this, it returns an innacurate result:

Quote

Assigned value for Pi:3.1415926535897932384626433832795

Pi as float:3.141593

Pi as double:1413754136
, as it takes 'c' for an integer. Did you mean some other correction should take place, or did I misunderstand your suggestion? Thank you very much for your feedback on this.


horace, I did use the %.20f and %.20lf operators successfully, thank you. However, in all descriptions of using double precision that I can find, these are not necessarilly used, and the double value is addressed by means of the bare %lf operator instead. Why does this not work, for my system? For instance, doesn't %lf work for your (and anyone else who may be desirous to try this) system? Is it necessary to address the number of significant digits?

ImaSexy, thank you for this code, much appreciated. Again, I'd like to ask, shouldn't double precision work simply by declaring a variable as 'double' and using %lf? Does this not work for you either, for instance?



Also, what about long double? Would that have any library inclusion requirements? (I have been unable to use it so far, although I know -- I am told that is -- that it should work with the use of the operator %Lf, when it does).



Many thanks for any feedback. It is appreciated.

Okaya
Was This Post Helpful? 0
  • +
  • -

#6 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Using Double Precision

Posted 04 September 2009 - 10:55 PM

View PostOkaya, on 4 Sep, 2009 - 08:45 PM, said:

horace, I did use the %.20f and %.20lf operators successfully, thank you. However, in all descriptions of using double precision that I can find, these are not necessarilly used, and the double value is addressed by means of the bare %lf operator instead. Why does this not work, for my system? For instance, doesn't %lf work for your (and anyone else who may be desirous to try this) system? Is it necessary to address the number of significant digits?

Also, what about long double? Would that have any library inclusion requirements? (I have been unable to use it so far, although I know -- I am told that is -- that it should work with the use of the operator %Lf, when it does).

with printf one should %f to print float and double (float is promoted to double when printf is called) and %Lf to print long double. if you specify %f the precision (digits after the decimal point) defaults to 6 so %.20f specifices a precision of 20.
here is your example incluing long double
#include <stdio.h>
#include <float.h>

int main()
{
float b=3.1415926535897932384626433832795;
double c=3.1415926535897932384626433832795;
long double d=3.1415926535897932384626433832795L;

printf("Assigned value for Pi:3.1415926535897932384626433832795\n\n");
printf("Pi as float:\t\t%.20f\n",b);
printf("Pi as double:\t\t%.20f\n",c);
printf("Pi as long double:\t%.20Lf\n",d);
printf("precision float %d double %d long double %d \n", FLT_DIG, DBL_DIG, LDBL_DIG);
return 0;
}


when run prints
Assigned value for Pi:3.1415926535897932384626433832795

Pi as float:		 3.14159274101257324000
Pi as double:		  3.14159265358979312000
Pi as long double:	3.14159265358979324000
precision float 6 double 15 long double 19 


as you can see long double has a few more digits of precision than double

This post has been edited by horace: 04 September 2009 - 10:56 PM

Was This Post Helpful? 0
  • +
  • -

#7 debjit625  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 51
  • View blog
  • Posts: 446
  • Joined: 06-September 08

Re: Using Double Precision

Posted 05 September 2009 - 04:04 AM

Sorry i thought you were interested in the numbers after decimal point
so it will be
printf("Pi as double:%0.15f\n\n",c); //Here "%0.15f" 


so it will give you "3.141592653589793"
Good luck

This post has been edited by debjit625: 05 September 2009 - 04:09 AM

Was This Post Helpful? 0
  • +
  • -

#8 Okaya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-August 09

Re: Using Double Precision

Posted 05 September 2009 - 11:21 AM

horace, debjit625, thank you for the further helpful feedback.

horace, I see, so is the apparent 'problem' I had simply the result of the internal workings of printf()? From what I gather, this means that the issue was simply one of displaying the digits in question, not of storing them in variables, or using them in calculations. In other words, this would mean that double precision calculations are processed without problem in my system at the moment, it's just that printf() would not display the double precision digits in full without my addressing it's level of displayed precision specifically, by the %.20lf operator.


Thank you also for the demonstration of the relevant long double systax, much appreciated.

Okaya
Was This Post Helpful? 0
  • +
  • -

#9 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Using Double Precision

Posted 05 September 2009 - 11:38 AM

View PostOkaya, on 5 Sep, 2009 - 05:21 PM, said:

horace, debjit625, thank you for the further helpful feedback.

horace, I see, so is the apparent 'problem' I had simply the result of the internal workings of printf()? From what I gather, this means that the issue was simply one of displaying the digits in question, not of storing them in variables, or using them in calculations. In other words, this would mean that double precision calculations are processed without problem in my system at the moment, it's just that printf() would not display the double precision digits in full without my addressing it's level of displayed precision specifically, by the %.20lf operator.


Thank you also for the demonstration of the relevant long double systax, much appreciated.

Okaya

yes, calculations are carried out in double to about 15 significant digits - printf() is just printing six digits after the decimal point by default. Of course, there is no point doing calculations using double if ones original data is only accurate to three or four significant digits. Also as calculations are carried out rounding errors can build up to the point where the results are total rubbish. e.g. try calculating the sine() of large angles using the taylor series
http://en.wikipedia....i/Taylor_series

This post has been edited by horace: 05 September 2009 - 11:41 AM

Was This Post Helpful? 0
  • +
  • -

#10 debjit625  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 51
  • View blog
  • Posts: 446
  • Joined: 06-September 08

Re: Using Double Precision

Posted 05 September 2009 - 11:56 AM

OK i didnt said why i used 15 "%0.15f" sorry for that its .IEEE(format) double precision uses 64 bits to represent numbers and is usually
accurate to about 15 significant decimal digits.

A bit of more interesting info inside the co-processor the floating points are stored in 80 bits (extended precision)rather than 32bits(single precision) or 64 bits (double precision) so in the time of calculation it gets more accurate with (extended precision).

Try to research on IEEE(Institute of Electrical and Electronic Engineers) for floating point numbers in computers

Good luck
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1