views:

503

answers:

6

I am doing some input/output between a c++ and a python program (only floating point values) python has a nice feature of converting floating point values to hex-numbers and back as you can see in this link:

http://docs.python.org/library/stdtypes.html#additional-methods-on-float

Is there an easy way in C++ to to something similar? and convert the python output back to C++ double/float? This way I would not have the problem of rounding errors when exchanging data between the two processes...

thx for the answers!

+1  A: 

The docs there say that %a does this in C.

Ned Batchelder
+1  A: 

You're sorely mistaken if you believe that this will solve all your problems. The hex notation is exactly equivalent to the original float. It just uses a different representation.

Ignacio Vazquez-Abrams
He's exchanging strings. The hex notation is exactly equivalent to the original float, which is why he wants it. A regular decimal string may include rounding errors.
Daniel Stutzbach
@Daniel: In that case he might be better off using something like `gmp` if the string representation is that important.
Ignacio Vazquez-Abrams
Thx for the hint to gmp I did not know that library either. Its an interesting alternativ. But for my simple problem its a probably a little overkill.
+1  A: 

C++ does not have any library routines for converting hex to floating point. One reason is that the internal representation of floating point is not standardized (although many compilers do use an IEEE standard).

I recommend storing in ASCII format, which is portable across platforms. You could write your own library (or find another) which will convert between an IEEE format (in hex) to the internal representation.

Thomas Matthews
+2  A: 

Can you send raw binary data between the two instead of strings? The struct package of Python's standard library can unpack the raw data into a Python float object.

Daniel Stutzbach
hey cool... I did not know about that python feature. I will definitly look into it.
+4  A: 

From the link you provided in your question (Additional Methods on Float):

This syntax is similar to the syntax specified in section 6.4.4.2 of the C99 standard, and also to the syntax used in Java 1.5 onwards. In particular, the output of float.hex() is usable as a hexadecimal floating-point literal in C or Java code, and hexadecimal strings produced by C’s %a format character or Java’s Double.toHexString are accepted by float.fromhex().

Example:

#include <cstdio>

int main() {
  float f = 42.79;
  printf("%.2f == %a\n", f, f);  
  fscanf(stdin, "%a", &f);
  printf("%.2f == %a\n", f, f);  
}

Run it:

$ g++ *.cpp && (python -c'print 12.34.hex()' | ./a.out )

Output:

42.79 == 0x1.5651ecp+5
12.34 == 0x1.8ae148p+3
J.F. Sebastian
+1  A: 

If you're on a platform whose system C library supports C99, you can just use printf( ) and scanf( ) with the %a format specifier. For reading in such values, you can also use the C99 strtod( ), strtof( ), and strtold( ) functions:

float value = strtof(string, NULL, 0);

You can optionally replace NULL with a char ** to get back a pointer to the end of the sequence of converted characters, and 0 with a known base for the string representation (if you set the base to be zero, it will infer the base from the format; it will parse the python hex floats just fine, and also handle normal decimal-formatted floating point)

If your compiler/library vendor has chosen not to care about C99 (MSVC, basically), you're probably out of luck until these function are incorporated into the C++ standard (I believe that they're in the draft C++0x, but I'm not completely sure).

Stephen Canon