As easy as pi?

Hi,

The following piece of code was to demonstrate the use os the unary scope resolution operator.

However the result of the program was:

Local float vlaue of PI = 3.141592741012573242
Global double value of PI = 3.141592653589790007

I was surprised that the difference between the float versionand double version was so big? Can somebody please explain what causes these differences?

//Fig. 3.24: fig03_24.cpp
//Using the unary scope resolution operator
#include <iostream>

using std::cout;
using std::endl;

#include <iomanip>

using std::setprecision;

//define global constant PI
const double PI = 3.14159265358979;

int main()
{
//define global contant PI
const float PI = static_cast< float >(::PI);

//display values of local and global PI constants
cout << setprecision (20)
<< " Local float value of PI = " << PI
<< "\nGlobal double value of PI = " << ::PI << endl;

return 0; //indicates successful termination
} //end main
[1148 byte] By [Bazman] at [2007-11-20 8:15:17]
# 1 Re: As easy as pi?
1. Use [code][ /code] tags!
2. float data type is accurate up to 6 digits, while double is up to 15. That's exactly what you see in your output.

Cheers
Hobson at 2007-11-9 1:19:46 >
# 2 Re: As easy as pi?
Take a look here: float ( http://msdn2.microsoft.com/en-us/library/hd7199ke(vs.71).aspx)

Float has 6-7 significant digits while double has 15-16 significant digits.

Laitinen
laitinen at 2007-11-9 1:20:47 >
# 3 Re: As easy as pi?
Hi,

I was using the following program to learn about the unary scope resolution operator.

However the output of the problem was as follows:

Local float value of PI = 3.1415927410125732
Global double value of PI = 3.14159265358979

I was very surprised that the float value and hte double values were so different can anyone explain why this is so?

Kind Regards

Baz

//Fig. 3.24: fig03_24.cpp
//Using the unary scope resolution operator
#include <iostream>

using std::cout;
using std::endl;

#include <iomanip>

using std::setprecision;

//define global constant PI
const double PI = 3.14159265358979;

int main()
{
//define global contant PI
const float PI = static_cast< float >(::PI);

//display values of local and global PI constants
cout << setprecision (20)
<< " Local float value of PI = " << PI
<< "\nGlobal double value of PI = " << ::PI << endl;

return 0; //indicates successful termination
} //end main
Bazman at 2007-11-9 1:21:46 >
# 4 Re: As easy as pi?
double
8 bytes IEEE 754. Covers a range from 4.94065645841246544e-324d to 1.79769313486231570e+308d (positive or negative).

float
4 bytes, IEEE 754. Covers a range from 1.40129846432481707e-45 to 3.40282346638528860e+38 (positive or negative).

Like all numeric types floats may be cast into other numeric types (byte, short, long, int, double). When lossy casts to integer types are done (e.g. float to short) the fractional part is truncated and the conversion is done modulo the length of the smaller type.

i think it will be friutful for you.

Ashish Kasama
ashukasama at 2007-11-9 1:22:46 >
# 5 Re: As easy as pi?
Hi thanks ashukasama,

I thought it must be something to do with casting from a higher order varaible to a lower order one.

However from your response:

float
4 bytes, IEEE 754. Covers a range from 1.40129846432481707e-45 to 3.40282346638528860e+38 (positive or negative).

surely float still covers up to 45 decimal places which should be more than enough to cover the 16 decimal places used in the original double variable.

I just thought that float should be accurate enough in this instance?

Or have I missed the point here??

Baz
Bazman at 2007-11-9 1:23:42 >
# 6 Re: As easy as pi?
Bazman, what was wrong with the answers you were given to this question yesterday: As easy as pie? ( http://www.dev-archive.com/forum/showthread.php?t=424943)

Please dont double post in the future!

Laitinen
laitinen at 2007-11-9 1:24:44 >
# 7 Re: As easy as pi?
[ Merged and moved threads ]

@to Bazman
Please don't post this kind of problems in Visual C++ forum!
Please don't open multiple threads for the same problem!
Please use CODE tags (http://www.dev-archive.com/forum/misc.php?do=bbcode#code)!

Thank you!
Ovidiu
ovidiucucu at 2007-11-9 1:25:48 >
# 8 Re: As easy as pi?
go thru this link

http://en.wikipedia.org/wiki/Floating_point

it will definitely be fruitful for you

**********Ashish Kasama*************
ashukasama at 2007-11-9 1:26:49 >
# 9 Re: As easy as pi?
Hi,

I apologise that I posted this twice.

But I had no replies in my mailbox and I couldn't even find the thread when I looked last night so I thought somehow I hadn't posted it correctly.

Also I will endeavour to post to correct forum it future. Which forum should this have gone in?

In any case thanks to all of you its clear now.

Baz
Bazman at 2007-11-9 1:27:47 >
# 10 Re: As easy as pi?
Which forum should this have gone in?
You can find a brief description of each forum in the forums index page (http://www.dev-archive.com/forum/index.php?).
Example:

Visual C++ Programming - Ask questions about Windows programming with Visual C++...
C++ (Non Visual C++ Issues) - Ask or answer C and C++ questions not related to Visual C++. This includes Console programming, Linux programming, or general ANSI C++.
etc...

Choose the appropriate one. That way you have the chance to find faster and better answers and also can help others which are searching for the same problem.
ovidiucucu at 2007-11-9 1:28:52 >
# 11 Re: As easy as pi?
ah OK apologies again!

will teach me to read the whole list next time!
Bazman at 2007-11-9 1:29:53 >
# 12 Re: As easy as pi?
float
4 bytes, IEEE 754. Covers a range from 1.40129846432481707e-45 to 3.40282346638528860e+38 (positive or negative).

surely float still covers up to 45 decimal places which should be more than enough to cover the 16 decimal places used in the original double variable.

I just thought that float should be accurate enough in this instance?

Or have I missed the point here??Range is not directly related to number of significant digits to represent the number. The 10 to the power something part is not part of total significant digits. It is not that a number that can be expressed in 6/7 significant digits can only go to as low as upto just 6/7 decimal places (without a 10^something), i.e. 10^-6/10^-7 but it can go upto as low as 10^-45 or something, but the significant digits can only be specified correctly upto 6/7 decimal places.
exterminator at 2007-11-9 1:30:50 >
# 13 Re: As easy as pi?
To be sure to represent exactly any binary FP number with a decimal representation, the same number of significand decimal digits than significand binary digits is required, in the worst case.
This is due to the fact that 10==2*5.
2 appears only once in the prime number development of 10.

Notation: x**n is x at the power of n.
Proof:
1/(2**n) is represented 0.0...01 in binary, where the number of zeroes is equal to n.
It's easy to demonstrate, with a recurrency argument, that the decimal representation of this number contains n significand digits and is terminated by 5.

1/(2**1) -> 0.5
1/(2**2) -> 0.25
1/(2**3) -> 0.125

1/(2**n) -> 0.0...0A...B5
1/(2**(n+1)) -> 0.0...0C...D25

32 bits float numbers TYPICALLY have a binary representation with a 24 bits mantissa, a 1 bit sign, and a 7 bits signed exponent.
Their precision is 24 binary digits which is the same precision as 24*log10(2)==7.224... decimal digits.

But, to represent exactly this FP number, sometimes, 24 significant decimal digits would be necessary!

Typically, double is 64 bits, with 53 bits for the mantissa, 1 bit for the sign and 10 bits for the exponent.
SuperKoko at 2007-11-9 1:31:58 >