<p>In follow up to <a href="https://stackoverflow.com/questions/56820/round-in-python-doesnt-seem-to-be-rounding-properly">this question</a>, it appears that some numbers cannot be represented by floating point at all, and instead are approximated.</p>

<p>How are floating point numbers stored?</p>

<p>Is there a common standard for the different sizes?</p>

<p>What kind of gotchas do I need to watch out for if I use floating point?</p>

<p>Are they cross-language compatible (ie, what conversions do I need to deal with to send a floating point number from a python program to a C program over TCP/IP)?</p>

[{'answer_id': 56955, 'author': 'Rik', 'author_id': 5409, 'author_profile': 'https://Stackoverflow.com/users/5409', 'pm_score': 0, 'selected': False, 'text': "<p>What I remember is a 32 bit floating point is stored using 24 bits for a actual number, and the remain 8 bits are used as a power of 10, determining where the decimal point is.</p>\n\n<p>I'm a bit rusty on the subject tho...</p>\n"}, {'answer_id': 56962, 'author': 'erickson', 'author_id': 3474, 'author_profile': 'https://Stackoverflow.com/users/3474', 'pm_score': 3, 'selected': False, 'text': '<p>The standard is <a href="http://en.wikipedia.org/wiki/IEEE_754" rel="nofollow noreferrer">IEEE 754</a>.</p>\n\n<p>Of course, there are other means to store numbers when IEE754 isn\'t good enough. Libraries like Java\'s <code>BigDecimal</code> are available for most platforms and map well to SQL\'s number type. Symbols can be used for irrational numbers, and ratios that can\'t be accurately represented in binary or decimal floating point can be stored as a ratio.</p>\n'}, {'answer_id': 56963, 'author': 'Onorio Catenacci', 'author_id': 2820, 'author_profile': 'https://Stackoverflow.com/users/2820', 'pm_score': 2, 'selected': False, 'text': '<p>This <a href="http://steve.hollasch.net/cgindex/coding/ieeefloat.html" rel="nofollow noreferrer">article</a> entitled "IEEE Standard 754 Floating Point Numbers" may be helpful.  To be honest I\'m not completely sure I\'m understanding your question so I\'m not sure that this is going to be helpful but I hope it will be.</p>\n'}, {'answer_id': 56969, 'author': 'stukelly', 'author_id': 5891, 'author_profile': 'https://Stackoverflow.com/users/5891', 'pm_score': 2, 'selected': False, 'text': '<p>Yes there is the <a href="http://en.wikipedia.org/wiki/IEEE_754-1985" rel="nofollow noreferrer">IEEE Standard for Binary Floating-Point Arithmetic (IEEE 754)</a></p>\n\n<p>The number is split into three parts, sign, exponent and fraction, when stored in binary.</p>\n'}, {'answer_id': 56979, 'author': 'Craig H', 'author_id': 2328, 'author_profile': 'https://Stackoverflow.com/users/2328', 'pm_score': 2, 'selected': False, 'text': '<p>Basically what you need to worry about in floating point numbers is that there is a limited number of digits of precision.  This can cause problems when testing for equality, or if your program actually needs more digits of precision than what that data type give you.</p>\n\n<p>In C++, a good rule of thumb is to think that a float gives you 7 digits of precision, while a double gives you 15.  Also, if you are interested in knowing how to test for equality, you can look at <a href="https://stackoverflow.com/questions/21265/comparing-ieee-floats-and-doubles-for-equality">this</a> question thread.</p>\n'}, {'answer_id': 56982, 'author': 'Knox', 'author_id': 4873, 'author_profile': 'https://Stackoverflow.com/users/4873', 'pm_score': 3, 'selected': False, 'text': '<p>As to the second part of your question, unless performance and efficiency are important for your project, then I suggest you transfer the floating point data as a string over TCP/IP.  This lets you avoid issues such as byte alignment and will ease debugging.</p>\n'}, {'answer_id': 57012, 'author': 'Kibbee', 'author_id': 1862, 'author_profile': 'https://Stackoverflow.com/users/1862', 'pm_score': 2, 'selected': False, 'text': "<p>If you're really worried about floating point rounding errors, most languages offer data types that don't have floating point errors.  SQL Server has the Decimal and Money data types.  .Net has the Decimal data type.  They aren't infinite precision like BigDecimal in Java, but they are precise down to the number of decimal points they are defined for.  So you don't have to worry about a dollar value you type in as $4.58 getting saved as a floating point value of 4.579999999999997</p>\n"}, {'answer_id': 57031, 'author': 'Rob Pilkington', 'author_id': 1623, 'author_profile': 'https://Stackoverflow.com/users/1623', 'pm_score': 6, 'selected': True, 'text': '<p>As mentioned, the <a href="http://en.wikipedia.org/wiki/IEEE_754-1985" rel="noreferrer">Wikipedia article on IEEE 754</a> does a good job of showing how floating point numbers are stored on most systems.</p>\n\n<p>Now, here are some common gotchas:</p>\n\n<ul>\n<li>The biggest is that you almost never want to compare two floating point numbers for equality (or inequality). You\'ll want to use greater than/less than comparisons instead.</li>\n<li>The more operations you do on a floating point number, the more significant rounding errors can become.</li>\n<li>Precision is limited by the size of the fraction, so you may not be able to correctly add numbers that are separated by several orders of magnitude. (For example, you won\'t be able to add 1E-30 to 1E30.)</li>\n</ul>\n'}, {'answer_id': 1863117, 'author': 'Thorsten S.', 'author_id': 224774, 'author_profile': 'https://Stackoverflow.com/users/224774', 'pm_score': 2, 'selected': False, 'text': "<blockquote>\n  <p>In follow up to this question, it\n  appears that some numbers cannot be\n  represented by floating point at all,\n  and instead are approximated.</p>\n</blockquote>\n\n<p>Correct.</p>\n\n<blockquote>\n  <p>How are floating point numbers stored?\n  Is there a common standard for the different sizes?</p>\n</blockquote>\n\n<p>As the other posters already mentioned, almost exclusively IEEE754 and its successor\nIEEE754R. Googling it gives you thousand explanations together with bit patterns and their explanation.\nIf you still have problems to get it, there are two still common FP formats: IBM and DEC-VAX. For some esoteric machines and compilers (BlitzBasic, TurboPascal) there are some\nodd formats.</p>\n\n<blockquote>\n  <p>What kind of gotchas do I need to watch out for if I use floating point?\n  Are they cross-language compatible (ie, what conversions do I need to deal with to\n  send a floating point number from a python program to a C program over TCP/IP)?</p>\n</blockquote>\n\n<p>Practically none, they are cross-language compatible.</p>\n\n<p>Very rare occuring quirks:</p>\n\n<ul>\n<li><p>IEEE754 defines sNaNs (signalling NaNs) and qNaNs (quiet NaNs). The former ones cause a trap which forces the processor to call a handler routine if loaded. The latter ones don't do this. Because language designers hated the possibility that sNaNs interrupt their workflow and supporting them enforce support for handler routines, sNaNs are almost always silently converted into qNaNs.\nSo don't rely on a 1:1 raw conversion. But again: This is very rare and occurs only if NaNs\nare present.</p></li>\n<li><p>You can have problems with endianness (the bytes are in the wrong order) if files between different computers are shared. It is easily detectable because you are getting NaNs for numbers.</p></li>\n</ul>\n"}]
['https://Stackoverflow.com/questions/56947', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2915/']