I get that this is a dumb question but I couldn't figure this out on google so this is my last resort.   Why is it that when I try to compare two doubles that are variables that I know are equal, it returns false? Here's an example that I hope is more clear:  double x = 323.23 double y = 323.23 System.out.println(x==y)   Output:  false   Why does this happen? Also, why does it print true if you just compare two doubles that are not variables or a double that is a variable and a double that is not? example:  double x = 323.23 System.out.println(x==323.23) System.out.println(323.23==323.23)   output:  true true
5169
In my program I have one array with 25 double values 0.04 When I try to sum these values in a loop I get following results:  0.0 + 0.04 = 0.04 0.04 + 0.04 = 0.08 0.08 + 0.04 = 0.12 0.12 + 0.04 = 0.16 0.16 + 0.04 = 0.2 0.2 + 0.04 = 0.24000000000000002 0.24000000000000002 + 0.04 = 0.28 0.28 + 0.04 = 0.32 0.32 + 0.04 = 0.36 0.36 + 0.04 = 0.39999999999999997 0.39999999999999997 + 0.04 = 0.43999999999999995 0.43999999999999995 + 0.04 = 0.4799999999999999 0.4799999999999999 + 0.04 = 0.5199999999999999 0.5199999999999999 + 0.04 = 0.5599999999999999 0.5599999999999999 + 0.04 = 0.6 0.6 + 0.04 = 0.64 0.64 + 0.04 = 0.68 0.68 + 0.04 = 0.7200000000000001 0.7200000000000001 + 0.04 = 0.7600000000000001 0.7600000000000001 + 0.04 = 0.8000000000000002 0.8000000000000002 + 0.04 = 0.8400000000000002 0.8400000000000002 + 0.04 = 0.8800000000000002 0.8800000000000002 + 0.04 = 0.9200000000000003 0.9200000000000003 + 0.04 = 0.9600000000000003   Why on earth could that happen?!
112631
int a = 1 &lt;&lt; 32; int b = 1 &lt;&lt; 31 &lt;&lt; 1;   Why does a == 1? b is 0 as I expected.
8867
When should I use -eq vs = vs ==  e.g.    [[ $num -eq 0 ]]  [[ $num = 'zzz' ]]   I've observed a pattern of using -eq (and -ne, etc.) for numbers and = for strings.  Is there a reason for this and when should I use ==
31237
How can I do this fast?  Sure I can do this:  static bool ByteArrayCompare(byte[] a1, byte[] a2) {     if (a1.Length != a2.Length)         return false;      for (int i=0; i&lt;a1.Length; i++)         if (a1[i]!=a2[i])             return false;      return true; }   But I'm looking for either a  function or some highly optimized proven way to do this.  java.util.Arrays.equals((sbyte[])(Array)a1, (sbyte[])(Array)a2);   works nicely, but it doesn't look like that would work for x64.  Note my super-fast answer .
106126
I was debugging something and discovered some strangeness in JavaScript:  alert(1=='') ==&gt; false alert(0=='') ==&gt; true alert(-1=='') ==&gt; false   It would make sense that an implied string comparison that 0 should = '0'. This is true for all non-zero values, but why not for zero?
70051
How can I format Floats in Java so that the float component is displayed only if it's not zero? For example:   123.45 -> 123.45 99.0   -> 99 23.2   -> 23.2 45.0   -> 45   Edit: I forgot to mention - I'm still on Java 1.4 - sorry!
26228
What is the difference between == and === in php. I am unsure when to use both.  Updated note: So that it shows up in StackOverflow search, the difference between == and === is the same as the difference between != and !==.
25759
I have encountered comparisions of variables to string literals multiple times over the years which had one character prefixing the variable and the literal, e.g.  if [ "x$A" = "xtrue" ]; then   in order to check whether $A is "true".  I assume this is done to achieve shell compatibility or to work around a longterm bug, an unintuitive behavior, etc. Nothing obvious comes to mind.  Today I figured I want to know the reason, but my research didn't turn up anything. Or maybe it's just me making something out of a rather frequent exposure to rare occurances.  Is this practice still useful, maybe even best?
19919
I ran into an interesting behavior recently.  It seems that if I override .equals() to take a parameter other than Object, it doesn't get called.  Can anyone explain to me why this is happening?  It seems to violate my understanding of polymorphism in OOP, but maybe I'm missing something.  Here's much simpler code that shows what I'm seeing:  public class MyClass {   private int x;   public MyClass(int n) { x = n; }   public boolean equals(Object o) { return false; }   public boolean equals(MyClass mc) { return x == mc.x; }   public static void main(String[] args) {     List&lt;MyClass&gt; list = new ArrayList&lt;MyClass&gt;();     list.add(new MyClass(3));     System.out.println("Contains 3? " + list.contains(new MyClass(3)));   } }   When this is run, it prints "Contains 3? false".  It looks like the equals(Object) function is called, even though there is another that would work.  By contrast, if I write equals like this the code works as expected:  public boolean equals(Object o) {   if(!(o instanceof MyClass))     return false;   MyClass mc = (MyClass)o;   return x == mc.x; }   Why isn't it figuring out which version of the function to call based on the type of the parameter?
7810
For any $\epsilon$, if $\epsilon&gt;0$ and $|x|&lt;\epsilon$, then $x=0$.  I understand that supposing  $\epsilon=\frac{x}{2}$ will lead to a contradiction, but let’s take a correct case: Let $\epsilon=3$, then $x$ would have a whole set of values. Can you explain what is going on?
80972
I've ended up in a debate on a proof for .999˜ being equal to 1 (in the real number system.)  I think the arguments have boiled down to whether or not we can preform variable substitution on one side of the equation, while keeping that variable rather than substituting it on the other side of the equation.  What we are looking at:  Given: x = 0.999˜ (Using the ˜ symbol to represent repeating)     .. A     10x - x     = 9.999˜ - x     9x          = 9.999˜ - x     9(.999˜)    = 9.999˜ - (.999˜)     8.999˜      = 9     ..     OR     .. B     10x - x     = 9.999˜ - x     9x          = 9.999˜ - (0.999˜) // Preform a substitution on ONLY the RHS     9x          = 9     x           = 1     ..  What's so confusing is that both should be right...since 8.999˜ IS equal to 9.  But, since this is a proof you should expect the same representation of the number at the most simplified step (I believe?)  So for simplicity sake my questions are...:   Is the algebra valid in both examples A and B? Does example A act as proof regardless of giving different representations of the same number at the most simplified step?
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.
57093
I think there's a lot for me to learn about data types. Why this happens  double result = ((3/8)*100).ToString();   it gives zero .. should be 37,5 ... :(
37282
This seems to be a basic issue, but I just realized that I actually don't know how to test equality of coefficients from two different regressions. Can anyone shed some light on this?   More formally, suppose I ran the following two regressions:  $$ y_1 = X_1\beta_1 + \epsilon_1 $$ and  $$ y_2 = X_2\beta_2 + \epsilon_2 $$ where $X_i$ refers to the design matrix of regression $i$, and $\beta_i$ to the vector of coefficients in regression $i$. Note that $X_1$ and $X_2$ are potentially very different, with different dimensions etc. I am interested in for instance whether or not $\hat\beta_{11} \neq \hat\beta_{21}$.   If these came from the same regression, this would be trivial. But since they come from different ones, I am not quite sure how to do it. Does anyone have an idea or can give me some pointers?   My problem in detail: My first intuition was to look at the confidence intervals, and if they overlap, then I would say they are essentially the same. This procedure does not come with the correct size of the test, though (i.e. each individual confidence interval has $\alpha=0.05$, say, but looking at them jointly will not have the same probability). My "second" intuition was to conduct a normal t-test. That is, take   $$ \frac{\beta_{11}-\beta_{21}}{sd(\beta_{11})} $$  where $\beta_{21}$ is taken as the value of my null hypothesis. This does not take into account the estimation uncertainty of $\beta_{21}$, though, and the answer may depend on the order of the regressions (which one I call 1 and 2).   My third idea was to do it as in a standard test for equality of two coefficients from the same regression, that is take  $$ \frac{\beta_{11}-\beta_{21}}{sd(\beta_{11}-\beta_{21})} $$   The complication arises due to the fact that both come from different regressions. Note that   $$ Var(\beta_{11}-\beta_{21}) = Var(\beta_{11}) + Var(\beta_{21}) -2 Cov(\beta_{11},\beta_{21}) $$ but since they are from different regressions, how would I get $Cov(\beta_{11},\beta_{21})$?  This led me to ask this question here. This must be a standard procedure / standard test, but I cound not find anything that was sufficiently similar to this problem. So, if anyone can point me to the correct procedure, I would be very grateful!
25826
How do I get whole and fractional parts from double in JSP/Java ? If the value is 3.25 then I want to get fractional =.25, whole = 3  How can we do this in Java?
9683
Hey all I asked this over  =) and I got a good answer but I still have no idea whats going on. I want to know how the expression got to the answer and maybe the math behind how it was reasoned.. the steps if you would be so kind. The likelihood is I don't understand the equation.   I'm not a big maths person please be gentle :p     For all those playing along at home I'm reading Absolute Java 5th   Edition by Walter Savitch (Chapter 2 Page 66).      The Expression  double d = 12345.123456789; System.out.printf("START%12.5e END %n", d);       The Answer  START 1.23451e+04END       I understand the basic principles of this printf method's arguments   for example I know the '%' represents the start of the parameters.   '12' is the number of spacing, '.5' is the times the decimal point   will move.... I see the decimal point has moved 4 places towards the   left... can someone explain the principles of e-notation. Also how   this expression came to this answer =).      As far as e-notation go's its meant to be scientific notation;      So like 5.89e-4 would mean 0.000589 (move the decimal place if minus   left if not move the decimal place right).
35135
I know $y=2f(x)$ doubles the $y$ value in vertical shifting, and I know that $y=(1/2)f(x)$ halves the $y$ value. But, why does $y=f(2x)$ halve the $x$ value, and why does $y=f((1/2)x)$ double the $x$ value in horizontal shifting? I looked at the graphs, and I see the difference, but I want to know the math behind why it halves instead of doubling the $x$ value for $y=f(2x)$. Maybe this is a stupid question, but could someone help me out?  Thank you.