<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java%2FData_Type%2FOverflow</id>
		<title>Java/Data Type/Overflow - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java%2FData_Type%2FOverflow"/>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java/Data_Type/Overflow&amp;action=history"/>
		<updated>2026-04-21T20:35:29Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java/Data_Type/Overflow&amp;diff=6843&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java/Data_Type/Overflow&amp;diff=6843&amp;oldid=prev"/>
				<updated>2010-06-01T06:33:00Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 06:33, 1 июня 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java/Data_Type/Overflow&amp;diff=6842&amp;oldid=prev</id>
		<title> в 18:01, 31 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java/Data_Type/Overflow&amp;diff=6842&amp;oldid=prev"/>
				<updated>2010-05-31T18:01:44Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Multiply two integers, checking for overflow. ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
import java.io.File;&lt;br /&gt;
/* &lt;br /&gt;
 * Licensed to the Apache Software Foundation (ASF) under one or more&lt;br /&gt;
 *  contributor license agreements.  See the NOTICE file distributed with&lt;br /&gt;
 *  this work for additional information regarding copyright ownership.&lt;br /&gt;
 *  The ASF licenses this file to You under the Apache License, Version 2.0&lt;br /&gt;
 *  (the &amp;quot;License&amp;quot;); you may not use this file except in compliance with&lt;br /&gt;
 *  the License.  You may obtain a copy of the License at&lt;br /&gt;
 *&lt;br /&gt;
 *      http://www.apache.org/licenses/LICENSE-2.0&lt;br /&gt;
 *&lt;br /&gt;
 *  Unless required by applicable law or agreed to in writing, software&lt;br /&gt;
 *  distributed under the License is distributed on an &amp;quot;AS IS&amp;quot; BASIS,&lt;br /&gt;
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.&lt;br /&gt;
 *  See the License for the specific language governing permissions and&lt;br /&gt;
 *  limitations under the License.&lt;br /&gt;
 *&lt;br /&gt;
 *&lt;br /&gt;
 */&lt;br /&gt;
public class Main {&lt;br /&gt;
  &lt;br /&gt;
  /**&lt;br /&gt;
   * Multiply two integers, checking for overflow.&lt;br /&gt;
   * &lt;br /&gt;
   * @param x a factor&lt;br /&gt;
   * @param y a factor&lt;br /&gt;
   * @return the product &amp;lt;code&amp;gt;x*y&amp;lt;/code&amp;gt;&lt;br /&gt;
   * @throws ArithmeticException if the result can not be represented as an&lt;br /&gt;
   *         int&lt;br /&gt;
   * @since 1.1&lt;br /&gt;
   */&lt;br /&gt;
  public static int mulAndCheck(int x, int y) {&lt;br /&gt;
      long m = ((long)x) * ((long)y);&lt;br /&gt;
      if (m &amp;lt; Integer.MIN_VALUE || m &amp;gt; Integer.MAX_VALUE) {&lt;br /&gt;
          throw new ArithmeticException(&amp;quot;overflow: mul&amp;quot;);&lt;br /&gt;
      }&lt;br /&gt;
      return (int)m;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Multiply two long integers, checking for overflow. ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
import java.io.File;&lt;br /&gt;
/* &lt;br /&gt;
 * Licensed to the Apache Software Foundation (ASF) under one or more&lt;br /&gt;
 *  contributor license agreements.  See the NOTICE file distributed with&lt;br /&gt;
 *  this work for additional information regarding copyright ownership.&lt;br /&gt;
 *  The ASF licenses this file to You under the Apache License, Version 2.0&lt;br /&gt;
 *  (the &amp;quot;License&amp;quot;); you may not use this file except in compliance with&lt;br /&gt;
 *  the License.  You may obtain a copy of the License at&lt;br /&gt;
 *&lt;br /&gt;
 *      http://www.apache.org/licenses/LICENSE-2.0&lt;br /&gt;
 *&lt;br /&gt;
 *  Unless required by applicable law or agreed to in writing, software&lt;br /&gt;
 *  distributed under the License is distributed on an &amp;quot;AS IS&amp;quot; BASIS,&lt;br /&gt;
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.&lt;br /&gt;
 *  See the License for the specific language governing permissions and&lt;br /&gt;
 *  limitations under the License.&lt;br /&gt;
 *&lt;br /&gt;
 *&lt;br /&gt;
 */&lt;br /&gt;
public class Main {&lt;br /&gt;
  /**&lt;br /&gt;
   * Multiply two long integers, checking for overflow.&lt;br /&gt;
   * &lt;br /&gt;
   * @param a first value&lt;br /&gt;
   * @param b second value&lt;br /&gt;
   * @return the product &amp;lt;code&amp;gt;a * b&amp;lt;/code&amp;gt;&lt;br /&gt;
   * @throws ArithmeticException if the result can not be represented as an&lt;br /&gt;
   *         long&lt;br /&gt;
   * @since 1.2&lt;br /&gt;
   */&lt;br /&gt;
  public static long mulAndCheck(long a, long b) {&lt;br /&gt;
      long ret;&lt;br /&gt;
      String msg = &amp;quot;overflow: multiply&amp;quot;;&lt;br /&gt;
      if (a &amp;gt; b) {&lt;br /&gt;
          // use symmetry to reduce boundry cases&lt;br /&gt;
          ret = mulAndCheck(b, a);&lt;br /&gt;
      } else {&lt;br /&gt;
          if (a &amp;lt; 0) {&lt;br /&gt;
              if (b &amp;lt; 0) {&lt;br /&gt;
                  // check for positive overflow with negative a, negative b&lt;br /&gt;
                  if (a &amp;gt;= Long.MAX_VALUE / b) {&lt;br /&gt;
                      ret = a * b;&lt;br /&gt;
                  } else {&lt;br /&gt;
                      throw new ArithmeticException(msg);&lt;br /&gt;
                  }&lt;br /&gt;
              } else if (b &amp;gt; 0) {&lt;br /&gt;
                  // check for negative overflow with negative a, positive b&lt;br /&gt;
                  if (Long.MIN_VALUE / b &amp;lt;= a) {&lt;br /&gt;
                      ret = a * b;&lt;br /&gt;
                  } else {&lt;br /&gt;
                      throw new ArithmeticException(msg);&lt;br /&gt;
                      &lt;br /&gt;
                  }&lt;br /&gt;
              } else {&lt;br /&gt;
                  // assert b == 0&lt;br /&gt;
                  ret = 0;&lt;br /&gt;
              }&lt;br /&gt;
          } else if (a &amp;gt; 0) {&lt;br /&gt;
              // assert a &amp;gt; 0&lt;br /&gt;
              // assert b &amp;gt; 0&lt;br /&gt;
              &lt;br /&gt;
              // check for positive overflow with positive a, positive b&lt;br /&gt;
              if (a &amp;lt;= Long.MAX_VALUE / b) {&lt;br /&gt;
                  ret = a * b;&lt;br /&gt;
              } else {&lt;br /&gt;
                  throw new ArithmeticException(msg);&lt;br /&gt;
              }&lt;br /&gt;
          } else {&lt;br /&gt;
              // assert a == 0&lt;br /&gt;
              ret = 0;&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
      return ret;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Subtract two integers, checking for overflow. ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
import java.math.BigDecimal;&lt;br /&gt;
/* &lt;br /&gt;
 * Licensed to the Apache Software Foundation (ASF) under one or more&lt;br /&gt;
 *  contributor license agreements.  See the NOTICE file distributed with&lt;br /&gt;
 *  this work for additional information regarding copyright ownership.&lt;br /&gt;
 *  The ASF licenses this file to You under the Apache License, Version 2.0&lt;br /&gt;
 *  (the &amp;quot;License&amp;quot;); you may not use this file except in compliance with&lt;br /&gt;
 *  the License.  You may obtain a copy of the License at&lt;br /&gt;
 *&lt;br /&gt;
 *      http://www.apache.org/licenses/LICENSE-2.0&lt;br /&gt;
 *&lt;br /&gt;
 *  Unless required by applicable law or agreed to in writing, software&lt;br /&gt;
 *  distributed under the License is distributed on an &amp;quot;AS IS&amp;quot; BASIS,&lt;br /&gt;
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.&lt;br /&gt;
 *  See the License for the specific language governing permissions and&lt;br /&gt;
 *  limitations under the License.&lt;br /&gt;
 *&lt;br /&gt;
 *&lt;br /&gt;
 */&lt;br /&gt;
public class Main {&lt;br /&gt;
  /**&lt;br /&gt;
   * Subtract two integers, checking for overflow.&lt;br /&gt;
   * &lt;br /&gt;
   * @param x the minuend&lt;br /&gt;
   * @param y the subtrahend&lt;br /&gt;
   * @return the difference &amp;lt;code&amp;gt;x-y&amp;lt;/code&amp;gt;&lt;br /&gt;
   * @throws ArithmeticException if the result can not be represented as an&lt;br /&gt;
   *         int&lt;br /&gt;
   * @since 1.1&lt;br /&gt;
   */&lt;br /&gt;
  public static int subAndCheck(int x, int y) {&lt;br /&gt;
      long s = (long)x - (long)y;&lt;br /&gt;
      if (s &amp;lt; Integer.MIN_VALUE || s &amp;gt; Integer.MAX_VALUE) {&lt;br /&gt;
          throw new ArithmeticException(&amp;quot;overflow: subtract&amp;quot;);&lt;br /&gt;
      }&lt;br /&gt;
      return (int)s;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Subtract two long integers, checking for overflow. ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
import java.math.BigDecimal;&lt;br /&gt;
/* &lt;br /&gt;
 * Licensed to the Apache Software Foundation (ASF) under one or more&lt;br /&gt;
 *  contributor license agreements.  See the NOTICE file distributed with&lt;br /&gt;
 *  this work for additional information regarding copyright ownership.&lt;br /&gt;
 *  The ASF licenses this file to You under the Apache License, Version 2.0&lt;br /&gt;
 *  (the &amp;quot;License&amp;quot;); you may not use this file except in compliance with&lt;br /&gt;
 *  the License.  You may obtain a copy of the License at&lt;br /&gt;
 *&lt;br /&gt;
 *      http://www.apache.org/licenses/LICENSE-2.0&lt;br /&gt;
 *&lt;br /&gt;
 *  Unless required by applicable law or agreed to in writing, software&lt;br /&gt;
 *  distributed under the License is distributed on an &amp;quot;AS IS&amp;quot; BASIS,&lt;br /&gt;
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.&lt;br /&gt;
 *  See the License for the specific language governing permissions and&lt;br /&gt;
 *  limitations under the License.&lt;br /&gt;
 *&lt;br /&gt;
 *&lt;br /&gt;
 */&lt;br /&gt;
public class Main {&lt;br /&gt;
  /**&lt;br /&gt;
   * Subtract two long integers, checking for overflow.&lt;br /&gt;
   * &lt;br /&gt;
   * @param a first value&lt;br /&gt;
   * @param b second value&lt;br /&gt;
   * @return the difference &amp;lt;code&amp;gt;a-b&amp;lt;/code&amp;gt;&lt;br /&gt;
   * @throws ArithmeticException if the result can not be represented as an&lt;br /&gt;
   *         long&lt;br /&gt;
   * @since 1.2&lt;br /&gt;
   */&lt;br /&gt;
  public static long subAndCheck(long a, long b) {&lt;br /&gt;
      long ret;&lt;br /&gt;
      String msg = &amp;quot;overflow: subtract&amp;quot;;&lt;br /&gt;
      if (b == Long.MIN_VALUE) {&lt;br /&gt;
          if (a &amp;lt; 0) {&lt;br /&gt;
              ret = a - b;&lt;br /&gt;
          } else {&lt;br /&gt;
              throw new ArithmeticException(msg);&lt;br /&gt;
          }&lt;br /&gt;
      } else {&lt;br /&gt;
          // use additive inverse&lt;br /&gt;
          ret = addAndCheck(a, -b, msg);&lt;br /&gt;
      }&lt;br /&gt;
      return ret;&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Add two long integers, checking for overflow.&lt;br /&gt;
   * &lt;br /&gt;
   * @param a an addend&lt;br /&gt;
   * @param b an addend&lt;br /&gt;
   * @param msg the message to use for any thrown exception.&lt;br /&gt;
   * @return the sum &amp;lt;code&amp;gt;a+b&amp;lt;/code&amp;gt;&lt;br /&gt;
   * @throws ArithmeticException if the result can not be represented as an&lt;br /&gt;
   *         long&lt;br /&gt;
   * @since 1.2&lt;br /&gt;
   */&lt;br /&gt;
  private static long addAndCheck(long a, long b, String msg) {&lt;br /&gt;
      long ret;&lt;br /&gt;
      if (a &amp;gt; b) {&lt;br /&gt;
          // use symmetry to reduce boundry cases&lt;br /&gt;
          ret = addAndCheck(b, a, msg);&lt;br /&gt;
      } else {&lt;br /&gt;
          // assert a &amp;lt;= b&lt;br /&gt;
          &lt;br /&gt;
          if (a &amp;lt; 0) {&lt;br /&gt;
              if (b &amp;lt; 0) {&lt;br /&gt;
                  // check for negative overflow&lt;br /&gt;
                  if (Long.MIN_VALUE - b &amp;lt;= a) {&lt;br /&gt;
                      ret = a + b;&lt;br /&gt;
                  } else {&lt;br /&gt;
                      throw new ArithmeticException(msg);&lt;br /&gt;
                  }&lt;br /&gt;
              } else {&lt;br /&gt;
                  // oppisite sign addition is always safe&lt;br /&gt;
                  ret = a + b;&lt;br /&gt;
              }&lt;br /&gt;
          } else {&lt;br /&gt;
              // assert a &amp;gt;= 0&lt;br /&gt;
              // assert b &amp;gt;= 0&lt;br /&gt;
              // check for positive overflow&lt;br /&gt;
              if (a &amp;lt;= Long.MAX_VALUE - b) {&lt;br /&gt;
                  ret = a + b;&lt;br /&gt;
              } else {&lt;br /&gt;
                  throw new ArithmeticException(msg);&lt;br /&gt;
              }&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
      return ret;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;/div&gt;</summary>
			</entry>

	</feed>