I came to interesting problem when dealing with Java primitive type float  I try to subtract 1.0 from Float.MAX_VALUE, strangely it returns the value equals Float.MAX_VALUE. Here is an example:    float floatMaxValue = Float.MAX_VALUE;   float subtractedMaxValue = Float.MAX_VALUE - 1.0f;    System.out.println(floatMaxValue);   System.out.println(subtractedMaxValue);   Assert.assertTrue(floatMaxValue == subtractedMaxValue); // return true even false expected   Could someone explain me why this happens? I guess is some magic with floating point.
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?
78703
If have this in the setter of a property:  decimal? temp = value as decimal?;   value = "90"  But after the cast, temp is null...  What is the proper way to do this cast?
119912
An integer's max value in Java is 2147483647, since Java integers are signed, right?  0xff000000 has a numeric value of 4278190080.  Yet I see Java code like this:  int ALPHA_MASK = 0xff000000;   Can anyone enlighten me please?
102992
I  have come up with the following expression to convert 5.1234   to   5° 7`24.24"   toint ($x) || '° ' || toint((($x) - toint ($x)) * 60) ||'`' || substr( (tostring(((($x) - toint ($x)) * 60) - toint((($x) - toint ($x)) * 60)) * 60),1,5) || '"'  Here’s the problem : for some  points such as 5.1234, it works. but for others,  it doesn’t.  I suspect the problem is the integer conversion which round up the decimal figures instead of truncating it.  Is there any other option? thanks.
66729
I want to compare two floating point numbers in a shell script. The following code is not working:  #!/bin/bash    min=12.45 val=10.35     if (( $val &lt; $min )) ; then       min=$val fi echo $min
63628
Here's an example:  Double d = (1/3); System.out.println(d);   This returns 0, not 0.33333... as it should.  Does anyone know?
28516
How do I format a number in Java? What are the &quot;Best Practices&quot;? Will I need to round a number before I format it?  32.302342342342343 =&gt; 32.30 .7323 =&gt; 0.73  etc.
67645
I know that a float isn't appropriate to store currency values because of rounding errors. Is there a standard way to represent money in C++?   I've looked in the boost library and found nothing about it. In java, it seems that BigInteger is the way but I couldn't find an equivalent in C++. I could write my own money class, but prefer not to do so if there is something tested.
10027
What would be the most efficient way to compare two double or two float values?  Simply doing this is not correct:  bool CompareDoubles1 (double A, double B) {    return A == B; }   But something like:  bool CompareDoubles2 (double A, double B)  {    diff = A - B;    return (diff &lt; EPSILON) &amp;&amp; (-diff &lt; EPSILON); }   Seems to waste processing.  Does anyone know a smarter float comparer?
3011
Ok I hope I am mising something here as I really don't want this to be a bug.   The following piece of code returns 2. Per my understanding of the ceiling functionality, the result should be 3 because 7/3 is roughly 2.33333 and the ceiling should return 3.   Is this a bug or am I missing something? Thanks.   Integer pageNumber = (Integer) Math.ceil(7/3);   This returns 2 and not 3 as expected.   It works fine on other input. For example:  Integer pageNumber = (Integer) Math.ceil(8/5);   This returns 2 as expected.
4177
Is there a way to step between 0 and 1 by 0.1?   I thought I could do it like the following, but it failed:  for i in range(0, 1, 0.1):     print i   Instead, it says that the step argument cannot be zero, which I did not expect.
11789
I'd like to convert a float to a whole number in JavaScript. Actually, I'd like to know how to do BOTH of the standard conversions: by truncating and by rounding. And efficiently, not via converting to a string and parsing.
115153
I know you can't rely on equality between double or decimal type values normally, but I'm wondering if 0 is a special case.  While I can understand imprecisions between 0.00000000000001 and 0.00000000000002, 0 itself seems pretty hard to mess up since it's just nothing.  If you're imprecise on nothing, it's not nothing anymore.  But I don't know much about this topic so it's not for me to say.  double x = 0.0; return (x == 0.0) ? true : false;   Will that always return true?
73325
Recently I tried understanding the use of java.math.MathContext but failed to understand properly. Is it used for rounding in java.math.BigDecimal. If yes why does not it round the decimal digits but even mantissa part.  From API docs, I came to know that it follows the standard specified in ANSI X3.274-1996 and ANSI X3.274-1996/AM 1-2000 specifications but I did not get them to read online.  Please let me know if you have any idea on this.
33306
Hey! I was looking at this code at   I noticed that in some situations they used hex numbers, like in line 134:  for (j = 1; val &amp;&amp; j &lt;= 0x80; j &lt;&lt;= 1, q++)   Now why would they use the 0x80? I am not that good with hex but I found an online hex to decimal and it gave me 128 for 0x80.  Also before line 134, on line 114 they have this:  small_n = (n &amp; 0xffff0000) == 0;   The hex to decimal gave me 4294901760 for that hex number. So here in this line they are making a bit AND and comparing the result to 0??  Why not just use the number? Can anyone please explain and please do give examples of other situations.  Also I have seen large lines of code where it's just hex numbers and never really understood why :(
14488
I saw this in , in reference to shortcomings of the Java spec:  There are more shortcomings and this is a subtle topic. Check  out: public class methodOverloading{      public static void hello(Integer x){           System.out.println(&quot;Integer&quot;);      }       public static void hello(long x){           System.out.println(&quot;long&quot;);      }       public static void main(String[] args){          int i = 5;          hello(i);      } }  Here &quot;long&quot; would be printed (haven't checked it myself), because the compiler chooses widening over auto-boxing. Be careful when using auto-boxing or don't use it at all!  Are we sure that this is actually an example of widening instead of autoboxing, or is it something else entirely? On my initial scanning, I would agree with the statement that the output would be &quot;long&quot; on the basis of i being declared as a primitive and not an object.  However, if you changed hello(long x)  to hello(Long x)  the output would print &quot;Integer&quot; What's really going on here?  I know nothing about the compilers/bytecode interpreters for java...
79818
This is probably pretty basic, but to save me an hour or so of grief can anyone tell me how you can work out the number of bits required to represent a given positive integer in Java?  e.g. I get a decimal 11, (1011). I need to get the answer, 4.  I figured if I could work out how to set all the bits other than the most significant bit to 0, and then >>> it, I'd get my answer. But... I can't.