Truncated Char in C++

Hello,
I'm working with a float value like: -44.397217  and trying to process it with cout.  My float seems to get truncate to -44.3972 somewhere along the way.

Here is the code:

float y=-44.397217;
    std::ostringstream ss;
     ss << y;
  std::string str = ss.str();
    cout << str << endl;

  char digits[10];
  std::strcpy( digits, str.c_str() );
  std::cout << digits << std::endl;

Open in new window


Do you see the issue?

thanks!
christamccAsked:
Who is Participating?
 
phoffricCommented:
You have 8 decimal digits. You need to set the precision. This works:
     float y=-44.397217;  
    std::ostringstream ss;
    ss.precision(8);
    ss << (double)y;
    std::string str = ss.str();  
    std::cout << str << std::endl;

See ios_base::precision:
   http://www.cplusplus.com/reference/iostream/ios_base/precision/

>> decimal number package
This is only needed if you cannot afford any rounding of your decimal digits. Most applications such as graphics and scientific algorithms can handle some small roundoff or truncation errors.
0
 
phoffricCommented:
float is likely 64 bits on your platform. Change to double to get more bits. IIRC, float can only hold about 6-1/2 decimal digits.
0
 
christamccAuthor Commented:
I think the float is fine, when it is traced we can see that it gets lost at the str.

I found this info:
"It is impossible to accurately represent a base 10 decimal number using base 2 values, except for a very small number of values (such as 0.25). To get what you need, you have to switch from the float/double built-in types to some kind of decimal number package."

Does that sound like the issue? Any idea on a decimal number package?

Thanks
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
phoffricCommented:
>> ss << (double)y;
Change that to:
   ss << y;
0
 
phoffricCommented:
Now consider:
   float y=-44.397217448;  
   std::ostringstream ss;
   ss.precision(11);

In this case, you do not get the desired result because float does not have enough bits to handle these 11 decimal digits. But if you change the float to a double, then you will get the correct result.
0
 
sarabandeCommented:
a 64-bit floating point type would have a precision of about 15 decimals. so the float is only 32 bit what is 6 to 7 decimals of precision. if your trace shows 8 digits then the last digit is random and the one before not precise. you could verify that by myultiplying or dividing by 10 and look at the trace again.

so you should use double as phoffric told you.

for outputting you could include <iomanip> and do like

std::cout << std::fixed  <<  std::setprecision(6) << dbl;

Open in new window


note, when using fixed or scientific, the argument to setprecision defines the number of digits after the decimalpoint. when you neither specify fixed or scientific the argument specifies the maximum digits of both sides of the decimal point.

see http://www.cplusplus.com/reference/iostream/manipulators/setprecision/ for more information.

Sara


Sara

0
 
phoffricCommented:
In my first post, I meant to say double, not float, as in:

double is likely 64 bits on your platform. Change to double to get more bits. IIRC, float can only hold about 6-1/2 decimal digits.

I was surprised to see that for your specific example using
    float y=-44.397217;    // 8 decimal digits
that the results were good despite having more than 7 digits. You could try other 8 digit numbers to see if they all work using float.

But, as you have seen, in my example of:
    float y=-44.397217448;   // 11 decimal digits
you do lose the precision using float, and here, using double is necessary to retain all the decimal digits.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.