views:

158

answers:

2

The problem is that I want to output Mathematica compatible floating point numbers. The only difference with the standard IOStream or printf output format is that the exponential e is replaced by *^:

Standard C/C++ output format: 1.23e-4
Mathematica format: 1.23*^-4

Is there a way to manipulate streams to achieve this effect? My original idea was just to use istringstream and dump it to a string and then replace all the e's. I would also be okay if someone posted code to parse through the bits of the floating point number and output it directly (i.e. a printf("%e") replacement).

A: 

Your idea should work out easily:

std::string FloatToStringMathematica(float x) {
  std::stringstream a;
  a << x;
  std::string u = a.str();
  size_t pos = u.find("e");
  if(pos != std::string::npos) {
    u.replace(p, 1, "*^"); 
  }
  return u;
}
Danvil
Did this answered your question?
Danvil
Yeah, I was waiting for something better, but I guess that's not possible.
Victor Liu
A: 

printf with %f shouldn't use scientific notation at all. Is there some reason you must use this notation and not simply output a long enough string to fit all significant figures?

frankc