-1

I have double

i=0.9500000000000000000000000001;

How can i convert it to string?

Like this

"0.9500000000000000000000000001"

  • 1
    https://en.cppreference.com/w/cpp/string/basic_string/to_string – Jesper Juhl Apr 05 '20 at 16:41
  • 1
    Btw; your double won't keep that much precision. – Jesper Juhl Apr 05 '20 at 16:42
  • nothing up not work, std::to_string(i) gives me 0.95, i need 0.9500000000000000000000000001 – Shoorty FTW Apr 05 '20 at 16:53
  • 2
    @ShoortyFTW Because a `double` does not have that much precision. Decide a number that will fit into `double` and then use `to_string`. – Ardent Coder Apr 05 '20 at 16:56
  • @Ardent Coder what type can work with those numbers? – Shoorty FTW Apr 05 '20 at 16:58
  • @ShoortyFTW I don't know if `long double` would work, but if you are working with such high precision numbers, then C++ has many [libraries](https://stackoverflow.com/a/11114602/10251345) to offer. – Ardent Coder Apr 05 '20 at 16:58
  • Can't you set precision using: `cout.setf(ios::fixed); cout.precision(x);`? Or does that just set the console out precision and not the stored value? – Dean Apr 05 '20 at 17:08
  • My answer to another question may be helpful/insightful here: https://stackoverflow.com/a/50970282/4641116 – Eljay Apr 05 '20 at 17:08
  • 2
    @ShoortyFTW Why do you need such rediculously big precision in the first place? You don't even need Pi to that many digits in order to calculate the circumference of the observable universe to within a millimeter. For all *reasonable* intents and purposes `0.9500000000000000000000000001` *is* `0.95`. – Jesper Juhl Apr 05 '20 at 18:11
  • @JesperJuhl OP is looking to build a multiverse lol – Ardent Coder Apr 05 '20 at 19:20
  • @Jesper Juhl i need it for hashes, so any character are important – Shoorty FTW Apr 05 '20 at 19:35
  • 1
    @Shorty then truncate it to some reasonable length before hashing. A hash is a truncation *anyway*. – Jesper Juhl Apr 05 '20 at 19:39
  • @Jesper Juhl its now a way for me, so asked here. – Shoorty FTW Apr 05 '20 at 19:44
  • @ShoortyFTW this is an [XY problem](https://meta.stackexchange.com/q/66377/230282). Printing more digits is not the way to get the hash as you thought. You should said the intention earlier in the question so that the real problem can be found and solved quicker. See [Hashing floating point values](https://stackoverflow.com/q/7403210/995714), [Hash function for floats](https://stackoverflow.com/q/4238122/995714), [Hashtables/Dictionaries that use floats/doubles](https://stackoverflow.com/q/946210/995714) – phuclv Apr 06 '20 at 12:28

1 Answers1

0

It seems that the precision you request is not only beyond double, but also beyond long double (at least for my version of gcc). For long double I would suggest something like this:

#include<iostream>
#include<iomanip>
#include<limits>

int main()
{   
// let's use number 1/3 to test possible limits of long double
long double i = 1.0L/3;

// create a stream
std::ostringstream streamLong;

// set precision to the maximal limit
streamLong << std::fixed << std::setprecision(std::numeric_limits<long double>::digits10 + 1);

// read long double to stream
streamLong << i;

// convert streamLong to a string
std::string i_string = streamLong.str();

std::cout << "i_string=" << i_string << std::endl;

return 0;
}

You can find some other possible solutions here.

  • It's not just beyond `long double`, it's *beyond reason* (unless OP can provide a good reason). – Jesper Juhl Apr 05 '20 at 19:30
  • i tryed long double, but it have a mistake. 0.9500000000000000000000000001 when i try to print it, it shows me 0.949999999999121... something like that, so this is not the way to do this. Even setprecision and fixed now working, try yourself with my numbers. Just print it – Shoorty FTW Apr 05 '20 at 19:33
  • @JesperJuhl , in scientific computing one encounters these round-off problems from time to time, so the result you get is just completely wrong. This is the type of error that is relatively hard to catch. If you can use higher-precision arithmetics you can at least track that the precision is the problem and not some other bug. – quantum_dog Apr 05 '20 at 19:40
  • @ShoortyFTW works fine for me when I substitute number similar to yours ``long double i=0.9500000000000000001L;``. This is the highest precision I can get with my compiler (gcc 7.5.0). Note ``L`` in the end of the number, this is to indicate that it is long. – quantum_dog Apr 05 '20 at 19:47
  • @quantum_dog i tryed it with my VS2019 and got this: i_string=0.9500000000000000 – Shoorty FTW Apr 05 '20 at 19:55
  • @ShoortyFTW , I'm not an expert on VS, but I know that ``long double`` is implementation-dependent (i.e. it can have less number of significant digits in your system with respect to mine). What output do you get for the program in my answer? – quantum_dog Apr 05 '20 at 20:02
  • i got 0.333333333333333314829616256247 with setprecision(30) – Shoorty FTW Apr 05 '20 at 20:06
  • @ShoortyFTW you see, you have only ~16 digits of precision with your version ``long double``. So you can't expect more than that for your number as well. It doesn't make sense increasing ``setprecision``. Try reducing the number of zeros in your number one by one and you'll see when it starts to work. It's not the problem of printing, it's a problem of storing the number in the memory. – quantum_dog Apr 05 '20 at 20:32
  • @quantum_dog yes, i understand that i can use only 13 characters. But what i can do with it? i need to work with 29 characters nubmers. Is there is the way? – Shoorty FTW Apr 05 '20 at 20:37
  • @ShoortyFTW depending on the problem you solve you can use just strings or vectors of ints to represent numbers (if you don't need to perform arithmetic operations with them). If you need some arithmetics, then you need special libraries, like Boost.Multiprecision. Further info on [quadruple precision](https://en.wikipedia.org/wiki/Quadruple-precision_floating-point_format#Libraries_and_toolboxes). – quantum_dog Apr 05 '20 at 20:50
  • @ShoortyFTW obviously you can't get more than 15 digits of precision because in MSVC `long double` is exactly the same type as `double` which is IEEE-754 double precision [Why are double and long double completely the same on my 64 bit machine?](https://stackoverflow.com/q/8922216/995714), [Why did Microsoft abandon long double data type?](https://stackoverflow.com/q/7120710/995714) – phuclv Apr 06 '20 at 12:32