I am converting an array of bytes to a 32 bit floating point. Sometimes the numbers are slightly off.  Example:  10.1 becomes 10.100000381469727 when I serialize the value in RapidJSON. How can I normalize this?  I can't share that code. What I can share is this to prove it:  std::string BytesToHexString(     unsigned char* data,     size_t len ) {     std::stringstream ss;     ss &lt;&lt; std::hex &lt;&lt; std::setfill('0');     for (size_t i = len - 1; i &gt;= 0 &amp;&amp; i &lt; len; --i)         ss &lt;&lt; std::setw(2) &lt;&lt; static_cast&lt;int&gt;(data[i]);     return ss.str(); }  std::string FLOATvalueToHexString(     float value ) {     union FloatToUChar {         float f;         unsigned char  c[sizeof(float)];     };      FloatToUChar floatUnion;     floatUnion.f = value;      return BytesToHexString(         floatUnion.c,         sizeof(float)     ); }  int main() {     std::string sFloatValue = "10.100000";      float fltValue = atof(sFloatValue.c_str());      std::string strHexFloatValue = FLOATvalueToHexString(fltValue);      std::cout &lt;&lt; sFloatValue &lt;&lt; " " &lt;&lt; fltValue &lt;&lt; " " &lt;&lt; strHexFloatValue &lt;&lt; std::endl;      return 0; }   It prints: 10.100000 10.1 4121999a The debugger says fltValue is 10.1000004.  If I convert 4121999a then this confirms that the internal storage is indeed off:    10.100000381469727  How can I normalize the floating point so I can at least get the correct hexadecimal value?
27
Consider the following code:   0.1 + 0.2 == 0.3  -&gt;  false   0.1 + 0.2         -&gt;  0.30000000000000004   Why do these inaccuracies happen?
43933
#include &lt;stdio.h&gt; int main() {     unsigned long long int num = 285212672; //FYI: fits in 29 bits     int normalInt = 5;     printf("My number is %d bytes wide and its value is %ul. A normal number is %d.\n", sizeof(num), num, normalInt);     return 0; }   Output:  My number is 8 bytes wide and its value is 285212672l. A normal number is 0.   I assume this unexpected result is from printing the unsigned long long int. How do you printf() an unsigned long long int?
67282
A 64-bit double can represent integer +/- 253 exactly. Given this fact, I choose to use a double type as a single type for all my types, since my largest integer is an unsigned 32-bit number. But now I have to print these pseudo integers, but the problem is they are also mixed in with actual doubles. So how do I print these doubles nicely in Java? I have tried String.format(&quot;%f&quot;, value), which is close, except I get a lot of trailing zeros for small values. Here's an example output of of %f  232.00000000 0.18000000000 1237875192.0 4.5800000000 0.00000000 1.23450000  What I want is:  232 0.18 1237875192 4.58 0 1.2345  Sure I can write a function to trim those zeros, but that's lot of performance loss due to string manipulation. Can I do better with other format code?  The answers by Tom E. and Jeremy S. are unacceptable as they both arbitrarily rounds to two decimal places. Please understand the problem before answering.  Please note that String.format(format, args...) is locale-dependent (see answers below).
36723
I have a long sequence of hex digits in a string, such as      000000000000484240FA063DE5D0B744ADBED63A81FAEA390000C8428640A43D5005BD44   only much longer, several kilobytes.  Is there a builtin way to convert this to a bytes object in python 2.6/3?
70333
What is the fastest way you know to convert a floating-point number to an int on an x86 CPU.  Preferrably in C or assembly (that can be in-lined in C) for any combination of the following:   32/64/80-bit float -> 32/64-bit integer   I'm looking for some technique that is faster than to just let the compiler do it.
9452
In Python, how can I parse a numeric string like "545.2222" to its corresponding float value, 545.2222? Or parse the string "31" to an integer, 31?  I just want to know how to parse a float str to a float, and (separately) an int str to an int.
32361
I have some numbers of different length (like 1, 999, 76492, so on) and I want to convert them all to strings with a common length (for example, if the length is 6, then those strings will be: '000001', '000999', '076492').   In other words, I need to add correct amount of leading zeros to the number.  int n = 999; string str = some_function(n,6); //str = '000999'   Is there a function like this in C++?
40309
What is the best way to convert a double to a long without casting?  For example:  double d = 394.000; long l = (new Double(d)).longValue(); System.out.println("double=" + d + ", long=" + l);
29737
I'm writing an application which reads large arrays of floats and performs some simple operations with them. I'm using floats, because I thought it'd be faster than doubles, but after doing some research I've found that there's some confusion about this topic. Can anyone elaborate on this?
6847
How do I convert between big-endian and little-endian values in C++? For clarity, I have to translate binary data (double-precision floating point values and 32-bit and 64-bit integers) from one CPU architecture to another.  This doesn't involve networking, so ntoh() and similar functions won't work here.  Note: The answer I accepted applies directly to compilers I'm targeting (which is why I chose it).  However, there are other very good, more portable answers here.
57416
Suppose I have a LimitedValue class which holds a value, and is parameterized on int types 'min' and 'max'.  You'd use it as a container for holding values which can only be in a certain range.  You could use it such:  LimitedValue&lt; float, 0, 360 &gt; someAngle( 45.0 ); someTrigFunction( someAngle );   so that 'someTrigFunction' knows that it is guaranteed to be supplied a valid input (The constructor would throw an exception if the parameter is invalid).  Copy-construction and assignment are limited to exactly equal types, though.  I'd like to be able to do:  LimitedValue&lt; float, 0, 90 &gt; smallAngle( 45.0 ); LimitedValue&lt; float, 0, 360 &gt; anyAngle( smallAngle );   and have that operation checked at compile-time, so this next example gives an error:  LimitedValue&lt; float, -90, 0 &gt; negativeAngle( -45.0 ); LimitedValue&lt; float, 0, 360 &gt; postiveAngle( negativeAngle ); // ERROR!   Is this possible?  Is there some practical way of doing this, or any examples out there which approach this?
2031
How can you convert a byte array to a hexadecimal string, and vice versa?
4641
I have a question about the primitive type short in Java. I am using JDK 1.6.   If I have the following:   short a = 2; short b = 3; short c = a + b;   the compiler does not want to compile - it says that it "cannot convert from int to short" and suggests that I make a cast to short, so this:  short c = (short) (a + b);   really works. But my question is why do I need to cast? The values of a and b are in the range of short - the range of short values is {-32,768, 32767}. I also need to cast when I want to perform the operations -, *, / (I haven't checked for others).  If I do the same for primitive type int, I do not need to cast aa+bb to int. The following works fine:  int aa = 2; int bb = 3; int cc = aa +bb;   I discovered this while designing a class where I needed to add two variables of type short, and the compiler wanted me to make a cast. If I do this with two variables of type int, I don't need to cast.   A small remark: the same thing also happens with the primitive type byte. So, this works:  byte a = 2; byte b = 3; byte c = (byte) (a + b);   but this not:  byte a = 2; byte b = 3; byte c = a + b;   For long, float, double, and int, there is no need to cast. Only for short and byte values.
50690
I am writing an algorithm where, given a model, I compute likelihoods for a list of datasets and then need to normalize (to probability) each one of the likelihood. So something like [0.00043, 0.00004, 0.00321] might be converted to may be like [0.2, 0.03, 0.77].   My problem is that the log likelihoods, I am working with, are quite small (for instance, in log space, the values are like -269647.432, -231444.981 etc). In my C++ code, when I try to add two of them (by taking their exponent) I get an answer of "Inf". I tried to add them in log-space , but again stumbled upon the same problem.  Can anybody share his/her expert opinion on this?
34299
How do you convert between hexadecimal numbers and decimal numbers in C#?
26327
What is the easiest way to handle huge numbers in C? I need to store values in the Area 1000^900, or in more human readable form 10^2700. Does anybody know of an easy way to do that? Any help would really be appreciated!
112071
We had a discussion here at work regarding why fread() and fwrite() take a size per member and count and return the number of members read/written rather than just taking a buffer and size. The only use for it we could come up with is if you want to read/write an array of structures which aren't evenly divisible by the platform alignment and hence have been padded but that can't be so common as to warrant this choice in design. From :  The function fread() reads nmemb elements of data, each size bytes long, from the stream pointed to by stream, storing them at the location given by ptr.   The  function  fwrite()  writes  nmemb elements of data, each size bytes long, to the stream pointed to by stream, obtaining them from the location given by ptr.   fread() and fwrite() return the number of items successfully read or written (i.e., not the number of characters).  If an error occurs, or  the end-of-file is reached, the return value is a short item count (or zero).