BigInteger returning negative numbers [duplicate] - java

This question already has answers here:
Why do these two multiplication operations give different results?
(2 answers)
Closed 8 years ago.
why this math return negative numbers for some numbers:
int x = 351;
String bigValue= ((50*x*x*x-150*x*x+400*x)/3) + "";
BigInteger resultInteger = new BigInteger(bigValue);
System.out.println(resultInteger);
result -> 714612600
but if i use 352
result -> -710900565
for x=500 -> 639244234
WHY?

This line here:
(50*x*x*x-150*x*x+400*x)/3
Is using integers, which can overflow. If an integer hits the max (2^31-1), it will overflow to -2^31.
You need to use BigIntegers here, something like this:
Biginteger bx = new BigInteger(x);
BigInteger new BigInteger(50).multiply(bx.pow(3)).multiply(new BigInteger(-150))
.multiply(bx.pow(2)).multiply(new BigInteger(400)).multiply(bx).divide(3);

Related

Java: Converting absolute value of BigInteger into intValue() resulting negative value [duplicate]

This question already has answers here:
How does Java handle integer underflows and overflows and how would you check for it?
(12 answers)
BigInteger.pow(BigInteger)?
(9 answers)
Closed 3 years ago.
I'm trying to do a BigInteger exponent calculation. For my calculation, I am trying to subtract m1 from m2 (both are BigInteger) and get a positive value, but if I convert this positive number to an int number, I end up getting a negative value.
BigInteger m1 = new BigInteger("2905012217");
BigInteger m2 = new BigInteger("534500746");
int exp = m2.subtract(m1).abs().intValue();
System.out.println(exp);
BigInteger g1 = new BigInteger("2");
BigInteger output_test_g1 = g1.pow(exp);
output:
m1:2905012217
m2:534500746
exp:-1924455825
Error:
Exception in thread "main" java.lang.ArithmeticException: Negative exponent
at java.base/java.math.BigInteger.pow(BigInteger.java:2401)
at FHEv1.homomorphicEqualityTest(FHEv1.java:195)
at FHEv1.main(FHEv1.java:323)
Integer overflow. How does Java handle integer underflows and overflows and how would you check for it?
Too high and overflow to Integer.MIN_VALUE and continue counting if nessecary. Too low and go to Integer.MAX_VALUE and continue counting if nessecary.
For example,
int A = Integer.MAX_VALUE;
A++;
//A should be Integer.MIN_VALUE, correct me if I am wrong

how to set a variable to 0.001 in JAVA (android) [duplicate]

This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 3 years ago.
when i want to get the result of 100/100000..
i got only 0.
Example
int one = 100;
int two = 100000;
int result = one/two;
toast(result); //Result is 0
Hey there "int" data type only stores integer values and not the decimals.
So if you divide 3 with 2 you would get 1 as answer instead of 1.5 .
Int just ignores the decimals .
You need to choose float or double data type for this to work.
Your variable named result must be declared and casted to float data type.
Appreciate the effort and mark this as answer if it helps you.....

Are mathematical operations different for different data types? [duplicate]

This question already has answers here:
Integer division: How do you produce a double?
(11 answers)
Division of integers in Java [duplicate]
(7 answers)
Closed 5 years ago.
Why is it that in the first line sevenTwelfths will evaluate to the expected answer (0.5833), but threeTwentySixths will evaluate to zero? I assumed that since the data type is a double, the operation of dividing 3 by 26 would be a decimal, but it appears as if it does the operation as an integer operation and then coverts that answer to a double and stores it in threeTwentySixths.
double sevenTwelfths = ((double) 7 / 12);
double threeTwentySixths = 3 / 26;
double sevenTwelfths = ((double) 7 / 12);
In First line this called typecasting meaning you are converting your answer in double for example
int sevenTwelfths=((int)7/12));
this mean after dividing 7/12 whatever is the anser convert into int or Type cast into int

Dividing numbers in java [duplicate]

This question already has answers here:
Simple division operation returning zero?
(5 answers)
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 5 years ago.
Hey people of Stackoverflow! I just have a question about an error that I came across while doing this lesson on Java online. So this is the code:
import java.util.Scanner;
public class GradesAndPoints {
public static void main(String[] args) {
System.out.print("Type in your score between (0-27): ");
Scanner ask = new Scanner(System.in);
int num = ask.nextInt();
int result = (num/27);
System.out.println(result);
The error is: whenever I run the code with the variable "num" being any int value, it prints out to be 0. Can someone explain to me why this error occurs and a solution I can implement to solve this?
The way you're doing this, you're diving integers. This, by definition, will get you an integer that is truncated.
5 / 10 = 0
If you turn one of them into a float (by adding a . at the end), you will get floating point division, which is what you're looking for.
5. / 10 = 0.5
5 / 10. = 0.5
5.0 / 10.0 = 0.5

how to eliminate any number after the floating point? [duplicate]

This question already has answers here:
How to cast a double to an int in Java by rounding it down?
(9 answers)
Closed 8 years ago.
Is there any function returns only the real number without the floating point? For an example,
func(1.xxx)->1
func(52.xx)->52
func(0.xx)->0
Is there any function does so?
Simply casting to int would truncate everything past the decimal point.
float f1 = 10.345;
int i1 = (int) f1; // Gives 10
float f2 = 10.897;
int i2 = (int) f2; // Also gives 10
You can do :
double d = 100.675;
System.out.println((int) d);
this gives you 100.
System.out.println(Math.round(d));
gives you 101.
You can also use :
new java.text.DecimalFormat("#").format(10.0); // => "10"
now the choice is yours that how you want to do and main thing depend on that what is your expected output is.

Categories

Resources