0% found this document useful (0 votes)
3 views8 pages

BigInteger Operations in Java

The document discusses various mathematical operations that can be performed with BigInteger and BigDecimal in Java. It provides examples of how to add, subtract, multiply, divide, find the remainder, exponentiate, and determine the maximum and minimum of BigInteger and BigDecimal objects. It also discusses moving the decimal point of a BigDecimal.

Uploaded by

Limbert Lipiri
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views8 pages

BigInteger Operations in Java

The document discusses various mathematical operations that can be performed with BigInteger and BigDecimal in Java. It provides examples of how to add, subtract, multiply, divide, find the remainder, exponentiate, and determine the maximum and minimum of BigInteger and BigDecimal objects. It also discusses moving the decimal point of a BigDecimal.

Uploaded by

Limbert Lipiri
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java LanguageEjemplos

de operaciones
matemáticas de BigInteger

Ejemplo#
BigInteger se encuentra en un objeto inmutable, por lo que debe asignar los
resultados de cualquier operación matemática a una nueva instancia de
BigInteger.

Adición: 10 + 10 = 20

BigInteger value1 = new BigInteger("10");


BigInteger value2 = new BigInteger("10");

BigInteger sum = [Link](value2);


[Link](sum);
salida: 20

Sustracción: 10 - 9 = 1

BigInteger value1 = new BigInteger("10");


BigInteger value2 = new BigInteger("9");

BigInteger sub = [Link](value2);


[Link](sub);
salida: 1

División: 10/5 = 2

BigInteger value1 = new BigInteger("10");


BigInteger value2 = new BigInteger("5");

BigInteger div = [Link](value2);


[Link](div);
salida: 2

División: 17/4 = 4

BigInteger value1 = new BigInteger("17");


BigInteger value2 = new BigInteger("4");

BigInteger div = [Link](value2);


[Link](div);
salida: 4

Multiplicación: 10 * 5 = 50

BigInteger value1 = new BigInteger("10");


BigInteger value2 = new BigInteger("5");

BigInteger mul = [Link](value2);


[Link](mul);
salida: 50

Potencia: 10 ^ 3 = 1000

BigInteger value1 = new BigInteger("10");


BigInteger power = [Link](3);
[Link](power);
salida: 1000

Resto: 10% 6 = 4

BigInteger value1 = new BigInteger("10");


BigInteger value2 = new BigInteger("6");

BigInteger power = [Link](value2);


[Link](power);
salida: 4

GCD: el divisor común más grande (GCD) para  12  y  18  es  6  .

BigInteger value1 = new BigInteger("12");


BigInteger value2 = new BigInteger("18");

[Link]([Link](value2));
Salida: 6

Máximo de dos BigIntegers:

BigInteger value1 = new BigInteger("10");


BigInteger value2 = new BigInteger("11");

[Link]([Link](value2));
Salida: 11

Mínimo de dos BigIntegers:

BigInteger value1 = new BigInteger("10");


BigInteger value2 = new BigInteger("11");

[Link]([Link](value2));
Salida: 10

 PDF - Download Java Language for free


Facebook313TwitterMás...2.8K

 PreviousNext 
Related Tags
 algorithm

 Android

 HTML

 javafx

 JavaScript

 MySQL

 Regular Expressions

 spring

 spring-boot

 SQL

This modified text is an extract of the original Stack Overflow Documentation created by
following contributors and released under CC BY-SA 3.0
This website is not affiliated with Stack Overflow
Email: tutorialpedia@[Link]

 Español (es) 
Want to advertise on this website?

Java LanguageOperaciones matemáticas con


BigDecimal.

Ejemplo#
Este ejemplo muestra cómo realizar operaciones matemáticas básicas con
BigDecimals.

[Link]ón
BigDecimal a = new BigDecimal("5");
BigDecimal b = new BigDecimal("7");

//Equivalent to result = a + b
BigDecimal result = [Link](b);
[Link](result);
Resultado: 12

[Link]
BigDecimal a = new BigDecimal("5");
BigDecimal b = new BigDecimal("7");

//Equivalent to result = a - b
BigDecimal result = [Link](b);
[Link](result);
Resultado: -2

[Link]
Al multiplicar dos  BigDecimal  s, el resultado tendrá una escala igual a la suma
de las escalas de los operandos.

BigDecimal a = new BigDecimal("5.11");


BigDecimal b = new BigDecimal("7.221");

//Equivalent to result = a * b
BigDecimal result = [Link](b);
[Link](result);
Resultado: 36.89931
Para cambiar la escala del resultado, utilice el método de multiplicación
sobrecargada que permite pasar  MathContext  , un objeto que describe las
reglas para los operadores, en particular la precisión y el modo de redondeo del
resultado. Para obtener más información sobre los modos de redondeo
disponibles, consulte la documentación de Oracle.

BigDecimal a = new BigDecimal("5.11");


BigDecimal b = new BigDecimal("7.221");

MathContext returnRules = new MathContext(4, RoundingMode.HALF_DOWN);

//Equivalent to result = a * b
BigDecimal result = [Link](b, returnRules);
[Link](result);
Resultado: 36.90

[Link]
La división es un poco más complicada que las otras operaciones aritméticas,
por ejemplo, considere el siguiente ejemplo:

BigDecimal a = new BigDecimal("5");


BigDecimal b = new BigDecimal("7");

BigDecimal result = [Link](b);


[Link](result);
Esperamos que esto dé algo similar a: 0.7142857142857143, pero
obtendríamos:

Resultado: [Link]: Expansión decimal sin


terminación; No hay un resultado decimal representable exacto.

Esto funcionaría perfectamente bien cuando el resultado fuera un decimal de


terminación, por ejemplo, si quisiera dividir 5 por 2, pero para aquellos números
que al dividir darían un resultado no final, obtendríamos
una  ArithmeticException  . En el escenario del mundo real, uno no puede
predecir los valores que se encontrarían durante la división, por lo que
necesitamos especificar la Escala y el Modo de redondeo para la división
BigDecimal. Para obtener más información sobre la escala y el modo de
redondeo, consulte la documentación de Oracle .

Por ejemplo, podría hacer:

BigDecimal a = new BigDecimal("5");


BigDecimal b = new BigDecimal("7");

//Equivalent to result = a / b (Upto 10 Decimal places and Round HALF_UP)


BigDecimal result = [Link](b,10,RoundingMode.HALF_UP);
[Link](result);
Resultado: 0.7142857143
[Link] resto o módulo
BigDecimal a = new BigDecimal("5");
BigDecimal b = new BigDecimal("7");

//Equivalent to result = a % b
BigDecimal result = [Link](b);
[Link](result);
Resultado: 5

6. Poder
BigDecimal a = new BigDecimal("5");

//Equivalent to result = a^10


BigDecimal result = [Link](10);
[Link](result);
Resultado: 9765625

[Link]
BigDecimal a = new BigDecimal("5");
BigDecimal b = new BigDecimal("7");

//Equivalent to result = MAX(a,b)


BigDecimal result = [Link](b);
[Link](result);
Resultado: 7

[Link]
BigDecimal a = new BigDecimal("5");
BigDecimal b = new BigDecimal("7");

//Equivalent to result = MIN(a,b)


BigDecimal result = [Link](b);
[Link](result);
Resultado: 5

9. Mover punto a la izquierda


BigDecimal a = new BigDecimal("5234.49843776");

//Moves the decimal point to 2 places left of current position


BigDecimal result = [Link](2);
[Link](result);
Resultado: 52.3449843776
10. Mover punto a la derecha
BigDecimal a = new BigDecimal("5234.49843776");

//Moves the decimal point to 3 places right of current position


BigDecimal result = [Link](3);
[Link](result);
Resultado: 5234498.43776

Existen muchas más opciones y una combinación de parámetros para los


ejemplos mencionados anteriormente (por ejemplo, hay 6 variaciones del
método de división), este conjunto no es una lista exhaustiva y cubre algunos
ejemplos básicos.

 PDF - Download Java Language for free

Facebook313TwitterMás...2.8K

 PreviousNext 
Related Tags
 algorithm

 Android

 HTML

 javafx

 JavaScript

 MySQL

 
 Regular Expressions

 spring

 spring-boot

 SQL

This modified text is an extract of the original Stack Overflow Documentation created by
following contributors and released under CC BY-SA 3.0
This website is not affiliated with Stack Overflow
Email: tutorialpedia@[Link]

 Español (es) 

Want to advertise on this website?

You might also like