C++ GCC string to long double conversion
andrew k (267) 76 posts |
I’m trying to write some code that will take a string input from a Toolbox input field convert it to a long double, perform a calculation and then convert the result back to a string for output in a display field. But I am hitting issues with precision I think as the result returned isn’t what I expect. to find the issue I have created the test code below to just work out how to go from string > long double > string. I was going to post this question on StackOverflow as I thought it was just a generic C++ question but having run the code below on my Mac it works correctly with the output at the start and end matching but on RISC it is returning something completely different. Any tips on how to do this?
|
David J. Ruck (33) 1636 posts |
What are compiling it with, and what options? |
andrew k (267) 76 posts |
Yes sorry forgot to say that. I am using the latests available GCC 4.7.4 on RISC OS and just compiling with g++ main.c++ and Mac OS I was just using Apple clang version 11.0.3. |
David Pitt (3386) 1248 posts |
I had a quick look, which was not noticeably useful, other than to show it does seem to be a RISC OS thing. gcc --version gcc (GCCSDK GCC 4.7.4 Release 5) 4.7.4 *g++ main.c++ * *a/out Test start 51.1234567 244760051714.4071045 * *gcc --version gcc (GCCSDK GCC 10.2.0) 10.2.0 **g++ main.c++ **a/out Test start 51.1234567 0.0000000 djp@ubuntu1804dev:~/Work/c++$ g++ --version g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0 djp@ubuntu1804dev:~/Work/c++$ g++ main.c++ djp@ubuntu1804dev:~/Work/c++$ ./a.out Test start 51.1234567 51.1234567 <pre> |
andrew k (267) 76 posts |
It’s good to know that it’s not just something I was doing though :) I notice you also have GCC 10.2.0 on RISC OS? If I had C++11 support then I could try converting using std::stold and see if that works on RISC OS |
David Pitt (3386) 1248 posts |
It is on the autobuilder at There is no formal release but it can be built, which requires a Linux, Ubuntu was used here. (I did try Raspberry Pi OS but without success.) |
David Pitt (3386) 1248 posts |
// stold example #include <iostream> // std::cout #include <string> // std::string, std::stold int main () { std::string orbits ("90613.305 365.24"); std::string::size_type sz; // alias of size_t long double pluto = std::stold (orbits,&sz); long double earth = std::stold (orbits.substr(sz)); std::cout << "Pluto takes " << (pluto/earth) << " years to complete an orbit.\n"; return 0; } *g++ --version g++ (GCCSDK GCC 10.2.0 Release 1) 10.2.0 *g++ pluto.c++ *a/out Pluto takes 248.093 years to complete an orbit. * |
Chris Gransden (337) 1207 posts |
If you add the following to the g++ command it will build with gcc 4.7.4 too.
|
David Pitt (3386) 1248 posts |
|
andrew k (267) 76 posts |
@David Yes you are right, using double instead of long double works. @Chris Thanks for the tip on how to use C++11 I will give this a proper go over the next couple of days. It works for the simple example I posted which looks good. |