Skip to main content
GameDev.net gamedev.net
🔒 Locked

c++ general

Started by nika Jan 20, 2001 at 1:15 PM 5 replies 500+ views
Original Post
nika
nika
hi how can i calculate values with more than 6 places(? i mean 123.456 for example). I''m using math.h and double variables but all values have 6 places. example: double untersummenwerte[100]; double r; int n; int count; untersummenwerte[count]=r/n*sqrt((r*r)-((r*count/n)*(r*count/n)));
Strylinys
Strylinys
You don''t have to do anything special to calculate values to more than 6 digits. When you display them, they will show up with only 6 digits because of the method you are using to display them. Example:

double printme = 123.4567890;

printf("123.4567890 = %.7f", printme);
printf("123.456 = %.3f", printme);

cout << "123.4567890 = " << setprecision(7) << printme;
cout << "123.456 = " << setprecision(3) << printme;

At least, that''s what I understand it to be...

S.
nika
nika
thx
now it works but i had to #include iomanip.h first.

another problem:

1.how can i make arrays with more than 100000 values? If i try to make one my prog always crashs.

2.
cout << setprecision(x); works but only till x=15.
Is this because of setprecision or because of my double values?
How to get more precised values?



Edited by - nika on January 21, 2001 7:46:15 AM
bharris
bharris
Firstly, there is no standard C++ header named "iomanip.h", it's "iomanip" and the contents of the latter are placed in namespace std.

In reply to question 1, if you are unsuccessful in creating an array of 100000 elements statically, try it dynamically, as there is a much greater amount of memory available on the 'free store'.

double *array_of_doubles = new double [100000];

// use array

delete [] array_of_doubles;

In reference to your second question, it appears as if your implementation uses 64 bits internally to store a variable of type double, thus you are limited to 15 digits of precision. This is not a problem with std::setprecision().

You may try a long double, which is allowed to be 'wider' than a double, but there is no guarantee.

If that doesn't work, you may try and see if your compiler offers a non standard extension for a larger data type capable of holding bigger and more precise values.

Ben.

Edited by - bharris on January 21, 2001 8:49:10 AM
Ben_
Ben_
double* pArray; // clear I hope
pArray = new double[100000] // reserves enough space in the memory, for 100000 double values and returns a pointer to the first one
// now you can simply access the array like a normal array, cause pointer <-> arry equality
// e.g. pArray[2] = 1324.23412
delete[] pArray
// if you don''t need the array any longer free the memory you reserved, remeber that are 390 kB, not neccessary at end of prog, but good style of programming

Greetings Ben
kvh
kvh
when you create a normal array the size of the array must be known at compile-time

you can create dynamic arrays of (almost) any size at runtime, the trick is to use pointers like Ben_ and bharris explain, just don''t forget to call delete, you are allocating memory yourself so you have to release it yourself

hope that makes sense

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.