-
Notifications
You must be signed in to change notification settings - Fork 99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Not getting the shortest decimal representation of 0.3 #199
Comments
You want Line 498 in 6f85836
d2fixed_buffered() implements Ulf's Ryu Printf algorithm which is given a precision for fixed notation (the precision is the number of digits after the decimal point). Passing a precision of 4 is like calling printf("%.4f", num); in C.
|
Thanks Stephan! calling char buff[64];
d2s_buffered(0.3, buff);
std::cout << buff << "\n"; prints: which is: What should I call to get the shortest fixed representation i.e. "0.3" ? |
This repo doesn't generate shortest fixed notation. However, if you're using C++, you can use C++17 If you call
#include <charconv>
#include <cstdio>
#include <iterator>
#include <system_error>
using namespace std;
void print_shortest(const double val) {
char buf[24]; // worst case: "-1.2345678901234567e-100"
const auto result = to_chars(buf, end(buf), val);
if (result.ec == errc{}) {
printf("%.*s\n", static_cast<int>(result.ptr - buf), buf);
} else {
printf("FAIL!\n");
}
}
int main() {
print_shortest(0.00003);
print_shortest(0.0003);
print_shortest(0.003);
print_shortest(0.03);
print_shortest(0.3);
print_shortest(3);
print_shortest(30);
print_shortest(300);
print_shortest(3000);
print_shortest(30000);
print_shortest(300000);
print_shortest(3000000);
}
If you always want fixed notation (but otherwise want shortest round-trip), passing MSVC's STL supports both the MSVC and Clang compilers on Windows; if you're targeting another platform, you'll need to ask your Standard Library implementers (I am unsure of their exact status). |
hello folks, beg your pardon for this silly? question, test.c:9:52: error: call of overloaded ‘to_chars(char [24], char*, const double&)’ is ambiguous acc. https://stackoverflow.com/questions/63963961/what-is-the-correct-way-to-call-stdto-chars it might be less a fail of me but a shortcoming in gcc, g++, libc++ or libstdc++? I'm not! an experienced coder, 'script kiddie' at best, but would like to - try to - use ryu in another project, gnumeric, and for that I need a 'startpoint', a template, a working call to the ryu conversion functions from another piece of code. I had hope to find that with above path but 'm stuck again. I kindly ask for any help ... :-) Best Regards, b. |
@newbie-02 Both Clang's libc++ and GCC's libstdc++ now support floating-point |
thank you for that hint, am I right that is support for g++ / clang++ / C++? If yes are there any means to make it available for 'pure C'? Got something up and running, see recipe below to evtl. save time for others who search for similar. And an additional question, 'long double to string' should be supported by generic128.h or similar, can someone provide a working example how to achieve that? e.g. 'long double x = 77.4L;', now convert into a SRT-string. would be very nice, I waste too much time in searching / trial and error. ( I need real long doubles, for the moment 80-bit x87 figures, not! the substitution partly used by Microsoft ( and valgrind? ): long double = double. A basic recipe to get converting up and running ( debian based linux, gcc available ):
in your program to get sthg. like '7.74E1' in btxt. |
howto script to get 'long' and 'generic' stuff to work: #207 (comment) |
hello @ShaiWavesAudio, |
hello@StephanTLavavej, |
I ran the following code:
I expected 0.3 to be printed, instead I got 0.3000, surly 0.3000 is not the shortest decimal representation of this floating point number.
What am I missing?
What is the correct way to get the shortest representation?
Thnaks,
Shai
The text was updated successfully, but these errors were encountered: