1

This code, which should return the smaller of the two numbers, returns a negative number similar to the greater number:

Math.min(15, 21474836477) --> returns -2147483642

I suspected this had something to do with the range of int's, so I changed the values to long and the program worked fine.

I don't quite understand the seemingly random number it returns- why is it -2147483643 and not the actual number I put in, -21474836477? Is the difference caused by the amount it overflowed, or is it influenced by the other parameter of Math.min in some way?

bmargulies
  • 97,814
  • 39
  • 186
  • 310
user2415992
  • 481
  • 7
  • 22
  • 1
    Please read up on int and check with the maximum int is: 2147483647. Look [here](http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html). Also read up on overflow. Your question is a possible duplicate of [why Integer.MAX\_VALUE + 1 == Integer.MIN\_VALUE?](http://stackoverflow.com/questions/9397475/why-integer-max-value-1-integer-min-value). Check out the answers. Also read up on [two's complement](http://en.wikipedia.org/wiki/Two%27s_complement). – Hovercraft Full Of Eels Sep 18 '13 at 03:46
  • 1
    Please post an [SSCCE](http://sscce.org/). I am having trouble reproducing your result. – Patricia Shanahan Sep 18 '13 at 03:53
  • What is happening with the variables your using before calling `Math.min`? – Tim Sep 18 '13 at 03:56

3 Answers3

2

The stated result, -2147483642, is 5-Integer.MAX_VALUE and also 7+Integer.MAX_VALUE. I suspect that the Math.min argument is actually that value, possibly resulting from evaluation of one of those expressions. Being very negative, it is definitely less than 15.

This program:

public class Test {
  public static void main(String[] args) {
    System.out.println(Math.min(15, Integer.MAX_VALUE));
    System.out.println(Math.min(15, 7+Integer.MAX_VALUE));
    System.out.println(Math.min(15, 5-Integer.MAX_VALUE));
  }
}

outputs:

15
-2147483642
-2147483642
Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75
0

This

Math.min(15, 21474836477) 

cannot possibly have returned

-2147483642

The code itself would not have compiled because the integer literal 21474836477 is outside the int value range.

It's possible you ran

Math.min(15L, 21474836477L) 

in which case you would be running the overloaded Math.min(long, long) method.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
0

Math.min is overloaded as shown below.

static double min(double a, double b) Returns the smaller of two double values. static float min(float a, float b) Returns the smaller of two float values. static int min(int a, int b) Returns the smaller of two int values. static long min(long a, long b) Returns the smaller of two long values.

therefore you can even use it with long values for example

          long x = 98759765l;
      long y = 15428764l;

      // print the smaller number between x and y
      System.out.println("Math.min(" + x + "," + y + ")=" + Math.min(x, y));

However, your long range is also out of range

poohdedoo
  • 1,258
  • 1
  • 18
  • 42