FRIHOST • FORUMS • SEARCH • FAQ • TOS • BLOGS • COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Help me with this c++ trouble





polis
I wnat to print on console this number:

1.414213562373095048801688724209698078569671875376948073176679737990732478462

But I got this instead:

1.41421

I've used float, double, long float and long double.

Ώany ideas?

Please help.
Indi
Realistically speaking, you're not going to get that kind of precision on any current desktop platform... and probably not on most supercomputers... using any of the standard types (float, double or long double - there is no long float, but some platforms support a long long double). For that kind of precision, you would have to write your own number class, or see if you can dig one up on Google.

However, to answer your direct question, assuming you're using the IOStreams library, you would do this:
Code:
cout.precision(4);
cout << 1.414213562373095048801688;
// Will print "1.1414"

cout.precision(2);
cout << 1.414213562373095048801688;
// Will print "1.14"

Or you can use manipulators:
Code:
cout << setprecision(4) << 1.414213562373095048801688;


But you won't get the kind of precision you're hoping for from any built-in type. From a 32-bit float, you can expect a maximum precision of 6±. From a 64-bit float, you can expect 15±. You're asking for a precision of around 80. Not likely. But you can always make your own number class if you really need that kind of precision.
polis
Yeah I reach the same conclusion.

So with double I printed on console 16 digits. Guess that is enough for me.

Thanks anyway.
qebab
There is the alternative of using a string for saving these numbers, though. Obviously, any arithmetic would be hard...
godsenddeath
qebab wrote:
There is the alternative of using a string for saving these numbers, though. Obviously, any arithmetic would be hard...



thats true, couldn't you use a combination of doubles and strings and just transfer back and forth using a stringstream?
godsenddeath
then again, is it really neccisary to have that much precision?
Indi
godsenddeath wrote:
qebab wrote:
There is the alternative of using a string for saving these numbers, though. Obviously, any arithmetic would be hard...



thats true, couldn't you use a combination of doubles and strings and just transfer back and forth using a stringstream?

What would be the point? If you store the numbers in a string but do the math in doubles you will only have as much precision as a double. You might as well just keep them as doubles and forget about strings altogether.
Liu
godsenddeath wrote:
then again, is it really neccisary to have that much precision?

Maybe he's a rocket scientist.
Mannix
you also need to use a variable from the float/double family. ...But be careful. ...Even a double has a limit on its precision. ....The fraction part only goes out so far...
coreymanshack
Like most of the others said, create your own class.
Related topics
HELP ME in C++
Need help in Borland C++ 3.0
Frihost DC (Distributed Computing) Project
C++ assignment help
The Whole "GOD" Thing
Help...phpMyAdmin trouble
C&C: Generals Help
Yabb problem
[help]c++ builder
help! domain trouble!
+BBCode [php]...[/php]
Help: C++ : windows.h
Kindle Global
Colored Crack !?
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.