I would like to know how to change the last x digits, of a series of double numbers, to zeros. E.g. I would like to change 1.0034958675848392 to 1.0034950000000000?

I have been searching C++.com for hours and I haven't come up with anything!

Thanks,

Corsican

I have been searching C++.com for hours and I haven't come up with anything!

Thanks,

Corsican

#include <cmath>

Use**modf**() to split the fractional part out, multiply it by the correct power of ten (six in your example), use **floor**(), divide by that same power of ten, and add the whole and fractional parts back together.

If you know your number is small enough that the multiplication won't loose digits, you can skip the**modf**() part.

Have fun.

Use

If you know your number is small enough that the multiplication won't loose digits, you can skip the

Have fun.

What I've done doesn't quite work, I'm afraid! (Although I do thank you for your suggestion.)

For example, I get 0.1638324999999999, from the following code:

dblx = 0.16383257184178683;

modf(dblx, &dblIntpart);

dblFractpart = modf (dblx, &dblIntpart);

dblFractpart = dblFractpart * 10000000;

dblFractpart = floor(dblFractpart);

dblFractpart = (dblFractpart * 0.0000001);

dblx = dblIntpart + dblFractpart;

Furthermore, adding 0.0000000000000001 to the output doesn't change it!

Please help if you can...

Corsican

For example, I get 0.1638324999999999, from the following code:

dblx = 0.16383257184178683;

modf(dblx, &dblIntpart);

dblFractpart = modf (dblx, &dblIntpart);

dblFractpart = dblFractpart * 10000000;

dblFractpart = floor(dblFractpart);

dblFractpart = (dblFractpart * 0.0000001);

dblx = dblIntpart + dblFractpart;

Furthermore, adding 0.0000000000000001 to the output doesn't change it!

Please help if you can...

Corsican

Maybe it would help to understand how floating points are stored?

http://en.wikipedia.org/wiki/IEEE_floating-point_standard

It might not really be easily possible to cut your numbers to decimal points because they're stored in binary, but you might be able to do something close using bitwise operations, and bitshifting. I do this on integers all the time, but I can't say for certain if it would be very useful to floating point numbers.

http://en.wikipedia.org/wiki/IEEE_floating-point_standard

It might not really be easily possible to cut your numbers to decimal points because they're stored in binary, but you might be able to do something close using bitwise operations, and bitshifting. I do this on integers all the time, but I can't say for certain if it would be very useful to floating point numbers.

I'm probably wrong but #include <iostream>, using std::setprecision; using std::fixed;

then just cout << fixed << setprecision(x = # of places past decimal point ) << data;

hope that might help :P i don't know very much about C++ to help you out.

then just cout << fixed << setprecision(x = # of places past decimal point ) << data;

hope that might help :P i don't know very much about C++ to help you out.

xplore and xabnu:

Thanks for your suggestions. I've found a way around it. That is, outputting truncated values and then reading them back in! I don't think it matters that I'm not rounding the last digit because the precision is still very high. It's not an elegant solution, but it does the job.

xplore: Thanks for your suggestion, but setting the precision of the output wasn't the problem. But again, thanks for responding.

xabnu: Yes, I'd also thought of using bitwise operations and bitshifting. I'd read a bit about that a couple months ago. Now that I have a back up solution, I'm not feeling so anxious! So, I'll likely read, again, how to do that, and the Wikipedia entry that you suggested. Thank you for your response.

Thanks for your suggestions. I've found a way around it. That is, outputting truncated values and then reading them back in! I don't think it matters that I'm not rounding the last digit because the precision is still very high. It's not an elegant solution, but it does the job.

xplore: Thanks for your suggestion, but setting the precision of the output wasn't the problem. But again, thanks for responding.

xabnu: Yes, I'd also thought of using bitwise operations and bitshifting. I'd read a bit about that a couple months ago. Now that I have a back up solution, I'm not feeling so anxious! So, I'll likely read, again, how to do that, and the Wikipedia entry that you suggested. Thank you for your response.

xplore:

Today when I set the precision of my output I found that in addition to what you suggested I do, I also had to put this in:

#include <iomanip>

PS I also don't know much about C++

Today when I set the precision of my output I found that in addition to what you suggested I do, I also had to put this in:

#include <iomanip>

PS I also don't know much about C++

Also, I had to change this:

cout << fixed << setprecision(x = # of places past decimal point ) << data;

To this:

cout << setprecision(x = # of places past decimal point) << data;

-The solution that I found to my initial query involved me truncating the outputs. I simply printed out the contents of the matrices, without setting the precision. The output numbers, by default, had six significant figures. I read them back in and I had what I wanted!

cout << fixed << setprecision(x = # of places past decimal point ) << data;

To this:

cout << setprecision(x = # of places past decimal point) << data;

-The solution that I found to my initial query involved me truncating the outputs. I simply printed out the contents of the matrices, without setting the precision. The output numbers, by default, had six significant figures. I read them back in and I had what I wanted!

what do you think of:

float x = 1.0034958675848392;

x = floor(x * 100000.0f) / 100000.0f; // If you need only the first six digits

if you problem is the output of such numbers and not the real representation:

yes, setprecision is a way ,sprintf() is C style but a way too.

float x = 1.0034958675848392;

x = floor(x * 100000.0f) / 100000.0f; // If you need only the first six digits

if you problem is the output of such numbers and not the real representation:

yes, setprecision is a way ,sprintf() is C style but a way too.

Topic archived. No new replies allowed.