Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
added 80 characters in body
Source Link

I have a system that I can't change now, is use float to store informations.

I had a problem with rounding error. Example:

std::string floatToStr(float d) { std::stringstream ss; ss << std::fixed << std::setprecision(15) << d; return ss.str(); } float val723 = 0.575f; std::cout << floatToStr(val723) << std::endl; 

The result is

0.574999988079071 

I can correct this with a string process.

std::string doubleToStr(double d, int precision) { std::stringstream ss; ss << std::fixed << std::setprecision(precision) << d; return ss.str(); } double val945 = (double)0.575f; std::cout << doubleToStr(atof(doubleToStr(val945, 4).c_str()), 15) << std::endl; 

The result is:

0.575000000000000 

But this is a costy solution. Is there a any better solution which can be use in realtime process? (Actually I don't need to use it in my real time code but I prepare myself if I will have to use in real time code.)

Edit1: I understand I can use 6 or 7 digits after the decimal point for float type.

I have a system that I can't change now, is use float to store informations.

I had a problem with rounding error. Example:

std::string floatToStr(float d) { std::stringstream ss; ss << std::fixed << std::setprecision(15) << d; return ss.str(); } float val723 = 0.575f; std::cout << floatToStr(val723) << std::endl; 

The result is

0.574999988079071 

I can correct this with a string process.

std::string doubleToStr(double d, int precision) { std::stringstream ss; ss << std::fixed << std::setprecision(precision) << d; return ss.str(); } double val945 = (double)0.575f; std::cout << doubleToStr(atof(doubleToStr(val945, 4).c_str()), 15) << std::endl; 

The result is:

0.575000000000000 

But this is a costy solution. Is there a any better solution which can be use in realtime process? (Actually I don't need to use it in my real time code but I prepare myself if I will have to use in real time code.)

I have a system that I can't change now, is use float to store informations.

I had a problem with rounding error. Example:

std::string floatToStr(float d) { std::stringstream ss; ss << std::fixed << std::setprecision(15) << d; return ss.str(); } float val723 = 0.575f; std::cout << floatToStr(val723) << std::endl; 

The result is

0.574999988079071 

I can correct this with a string process.

std::string doubleToStr(double d, int precision) { std::stringstream ss; ss << std::fixed << std::setprecision(precision) << d; return ss.str(); } double val945 = (double)0.575f; std::cout << doubleToStr(atof(doubleToStr(val945, 4).c_str()), 15) << std::endl; 

The result is:

0.575000000000000 

But this is a costy solution. Is there a any better solution which can be use in realtime process? (Actually I don't need to use it in my real time code but I prepare myself if I will have to use in real time code.)

Edit1: I understand I can use 6 or 7 digits after the decimal point for float type.

Source Link

float rounding error float to double conversion

I have a system that I can't change now, is use float to store informations.

I had a problem with rounding error. Example:

std::string floatToStr(float d) { std::stringstream ss; ss << std::fixed << std::setprecision(15) << d; return ss.str(); } float val723 = 0.575f; std::cout << floatToStr(val723) << std::endl; 

The result is

0.574999988079071 

I can correct this with a string process.

std::string doubleToStr(double d, int precision) { std::stringstream ss; ss << std::fixed << std::setprecision(precision) << d; return ss.str(); } double val945 = (double)0.575f; std::cout << doubleToStr(atof(doubleToStr(val945, 4).c_str()), 15) << std::endl; 

The result is:

0.575000000000000 

But this is a costy solution. Is there a any better solution which can be use in realtime process? (Actually I don't need to use it in my real time code but I prepare myself if I will have to use in real time code.)