Quick Question About Variables
Author |
Message |
rar
|
Posted: Thu Sep 24, 2009 1:12 pm Post subject: Quick Question About Variables |
|
|
I'm sure this has been asked a million times before, but...
What is the difference between a double and a float variable?
Also, what declaration would you make to limit a value to 2 decimal places? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
jernst
|
Posted: Thu Sep 24, 2009 1:23 pm Post subject: Re: Quick Question About Variables |
|
|
A double uses twice as many bytes to store a decimal number. So if on your system a float is represented with 4 bytes, double would be with 8 bytes. So it can store bigger numbers, or more precise rational numbers.
You can restrict the output of printf to display a certain number of decimal points. See this page: http://www.cppreference.com/wiki/c/io/printf |
|
|
|
|
|
wtd
|
Posted: Sun Sep 27, 2009 1:37 am Post subject: RE:Quick Question About Variables |
|
|
It's worth noting that the numbers of bytes used to represent the types is implementation-specific, if memory serves.
While 32/64 bit values are pretty standard, some architectures have had 80-bit doubles. |
|
|
|
|
|
OneOffDriveByPoster
|
Posted: Mon Sep 28, 2009 5:21 pm Post subject: Re: Quick Question About Variables |
|
|
If you want to store two decimal places and the values fit, you may want to use "int" and just store your numbers 100 times the magnitude in relation to the input and output values. There is also "long long" in C99. You have to look out for division though. |
|
|
|
|
|
|
|