1

I'm writing software that has to work on different platforms. It uses floating point numbers. On all platforms, the floating point numbers have to be the same size in memory.

For integers I could use int32_t for example. How can I do this for floating point numbers?

mskfisher
  • 3,291
  • 4
  • 35
  • 48
  • why do they have to be the same size? – Karoly Horvath Sep 24 '11 at 23:10
  • @yi_H they are stored in a file on disk. –  Sep 24 '11 at 23:11
  • 1
    compare sizeof(double) to a constant – bmargulies Sep 24 '11 at 23:12
  • 1
    @WTP: well, theoretically they could be still different representations. – Karoly Horvath Sep 24 '11 at 23:13
  • This file contains the floats in binary format? If it is ASCII whatever function you're using to read them (for e.g. `atof`) should take care of different float sizes. – Praetorian Sep 24 '11 at 23:14
  • @Praetorian the files are in binary format, otherwise this won't make any sense. –  Sep 24 '11 at 23:16
  • @WTP: Sorry, just wanted to make sure you weren't worrying about a non-issue :-) You should be able to use one the preprocessor defines in [float.h](http://www.cplusplus.com/reference/clibrary/cfloat/) to determine the size of the float. I'm not an expert on the format so I can't tell you which one(s) to use. – Praetorian Sep 24 '11 at 23:20
  • IEEE-754 has been a strong standard for over 25 years. The odds you'll run into old iron that doesn't implement are sufficient small to not lose any sleep over it. But if you like to fret about things like that, nobody will stop you. – Hans Passant Sep 24 '11 at 23:25
  • It would seem that endian issues across platforms would be more common than non-IEEE-754 issues? Have you actually seen this issue? Where? – the wolf Sep 24 '11 at 23:43
  • There are subtle differences in IEEE-754, though, aren't there? For example some hardware supports denorms, and others don't. If you transfer a file from an implementation that does, to one that doesn't, and the data contains denorms, then the reader won't like it. – Steve Jessop Sep 25 '11 at 00:15
  • +1 to offset the downvote and the "too localized" vote to close. Representing information in a portable form is as important as ever, now that we expect to access our "cloud" data from PCs, Macs, tablets, smart phones, and the implants that the aliens put in the backs of our necks. – Adam Liss Sep 25 '11 at 04:10

2 Answers2

4

If you need to force the values to the same size, you can design a representation that uses only integers of known sizes. You'd convert your floats to that format to save them, and you'd convert them back to floats when you read them.

See how can I extract the mantissa of a double for AProgrammer's explanation of the frexp() function, which will decompose a float into its (integer) exponent and a (double) mantissa. The comments following that answer will explain how to convert the mantissa to an integer.

You can save the integer mantissa and exponent, which will have fixed lengths. Then you can read them back and use the ldexp() function to re-create the original float (with some small error, of course).

Community
  • 1
  • 1
Adam Liss
  • 47,594
  • 12
  • 108
  • 150
3

You can't do it portably in C; you have to take what the systems provide.

That said, on all systems I know of, sizeof(float) == 4 and sizeof(double) == 8, but relying on that absolutely is dangerous.

Different machines can store the same value differently. They might use different floating-point formats, or they might all use IEEE 754. Even if they all use IEEE 754, they might store them in big-endian or little-endian order.

You must decide why you think they must all be the same size. The chances are, you are trying to take some unwarranted short cuts in relaying information between different machines. Don't take the short cuts; they will lead you into problems at some point. If you feel you must, you have to assess what your portability goals really are, and validate whether you can meet them with the design you're proposing. But be very cautious!

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 2
    Note that `sizeof(float) == 4` doesn't mean 32bits either. – K-ballo Sep 24 '11 at 23:31
  • 1
    True, but when was the last time you worked on a machine where CHAR_BIT was not 8? Usually, if you have to deal with such machines, you probably already know about these issues and don't need to ask the question in the first place. – Jonathan Leffler Sep 24 '11 at 23:54