Java modulo doesn't work with large numbers [duplicate] - java

This question already has answers here:
Modulus with doubles in Java
(4 answers)
Taking Modulo of Double NUmber
(3 answers)
Closed 5 years ago.
I am creating a program to calculate Mersenne primes in Java. While doing so, I noticed that the modulo % operator doesn't function properly when dealing with large numbers.
Here is my code:
double a = 2305843009213693951d; //2^61 -1
double b = 2;
double m = a % b; //this should be 1.0
System.out.println(m); //prints 0.0
While one would expect the result to be 1, since the original number isn't even, it actually returns 0.0.
Why is this, and how can I fix it?

Related

How do I make a randomly generated number using Math.Random() to be only two decimal places [duplicate]

This question already has answers here:
How can I truncate a double to only two decimal places in Java?
(18 answers)
How to round a number to n decimal places in Java
(39 answers)
Closed 11 months ago.
I'm trying to set random values to an array using Math.Random(), but it is printing values with 15 decimal places when I only want 2. How do I fix this?
This is what I have so far it works and gives me values like:
[9.410800457889598, 7.816378845915057, 7.4315520333161995, 7.4844070010512285, 7.6640924183174945].
public static void populateArray(double[] judge){
for(int n=0;n<=4;n++){
double score =(Math.random()*(3)+7);
judge[n] = score;
}
}

Java BigDecimal divide and get digit to N places [duplicate]

This question already has answers here:
BigDecimal setScale and round
(2 answers)
Set specific precision of a BigDecimal
(5 answers)
Closed 1 year ago.
I am trying to write a program that has a step in it where I would like to divide two numbers and get a decimal number to 60 places.
For instance, I would like to divide 1 by 17 and get 0.016393442622950819672131147540983606557377049180327868852459 without losing any precision.
I am trying to store the number in a BigDecimal but I am having trouble finding a good way to accomplish this without losing the precision after the 16th digit or so

How to get Decimal part in Double variable Division[JAVA] [duplicate]

This question already has answers here:
Beginners Java Question (int, float)
(4 answers)
Division of integers in Java [duplicate]
(7 answers)
Closed 2 years ago.
If I divide a double type variable, the Decimal part becomes zero.
a=13122/10;
System.out.println (a);
Prints
1312.0
As you can see, the Decimal part became zero when I divided it.
But I need the value
1312.2
Your problem is that while you may have stored "a" as a double, you are really dividing two "ints" and saving that. When you divide 2 ints, the number automatically gets rounded down. So, it's rounded down to 1312.0.
What you need is this,
a = (double)13122/10;
or this:
a = 13122.0/10;
You are dividing integers. You can cast the division and then you are ok
double a = (double) 13122/10;
System.out.println (a);

java double number division giving strange output [duplicate]

This question already has answers here:
Simple division in Java - is this a bug or a feature?
(5 answers)
Closed 7 years ago.
Found below output strange. Is it really?
double rate = 11/12; // outputs 0.0
double rate = 11.00/12; // outputs 0.916666667;
Why so much difference?
In the 1st case first division is done so an int divided by an int gives a integer i.e, 11/12=0 Then this integer is converted to double .ie, 0.0. In the 2nd case a double(11.00) is divided by a integer(12).The integer 12 is then automatically type casted to double as division should occur with similar types. This division gives a double value which is exact in reality(0.916666667)
See this link https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html

calculating the power in Java [duplicate]

This question already has answers here:
Raising a number to a power in Java
(10 answers)
Closed 8 years ago.
I am trying to calculate the power as below but it is giving me 'bad operands type for binary operator '^'. I am guessing that it is a precedence issue but it still doesn't fix with inserting additional brackets
double pw = ((N - (df + 1))^2);
You should use java.lang.Math.pow(x,y)
Example: java.lang.Math.pow(2,3) returns 8
See this
http://www.tutorialspoint.com/java/lang/math_pow.htm
java.lang.Math.pow(double a, double b)
You can use static import for this.

Categories

Resources