python big integer multiplication doesn't calculate most significant digits [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have a python program which should multiply 2 big integers.
The problem is I get a different result in java with BigInteger class with same input.
I have tried DecInt library for python but gives the same answer as using pure python.
Here are my variables:
d = 372049305848826709205673800090501485720867768816
r = 5452188953055713107393819158892374332916586527736541041226026450382
Result I get in python from d * r:
2028483115341019294875069650745272851135156323450218238187883716036516369477015140871224045070868977706272670887712
Result I get in java with BigInteger class:
9530687378863294988874153740700860249994095546182028483115341019294875069650745272851135156323450218238187883716036516369477015140871224045070868977706272670887712
Here is my java code:
BigInteger d = new BigInteger("372049305848826709205673800090501485720867768816");
BigInteger r = new BigInteger("5452188953055713107393819158892374332916586527736541041226026450382");
BigInteger tmp1 = d.multiply(r);
System.out.println(tmp1);
As you can see, there are some most significant digits that are missed in python's result.
Is there any solution for that?

Both Java and Python give the same answer:
2028483115341019294875069650745272851135156323450218238187883716036516369477015140871224045070868977706272670887712
The problem must be in your Java code then. Make sure you preform multiplication correctly and verify your input.
Also, check if there is anything that might print digits just before the result. The simplest way I can think of is:
System.out.println("the result is: " + int1.multiply(int2));
That will separate the output you are interested in from everything that is already printed.

Related

Current incoming MBPS in python [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
for a project of mine i need to detect the current incoming megabytes per second on linux coded in python.
i found someone elses code that does the correct thing but its coded in java and i dont exactly understand it. any pointers?
TOTAL_INCOMING_BYTES_FILE = "/sys/class/net/%s/statistics/rx_bytes",
final double current_mbps =
((current_total_bytes - Long.parseLong(Files.readAllLines(Paths.get(new File(String.format(TOTAL_INCOMING_BYTES_FILE, ETH_INTERFACE)).toURI())).get(0))) / 125000) * (-1);
i found someone elses code that does the correct thing but its coded in java and i dont exactly understand it.
Indeed Long.parseLong(Files.readAllLines(Paths.get(new File(String.format(TOTAL_INCOMING_BYTES_FILE, ETH_INTERFACE)).toURI())).get(0)) is an incredibly convoluted way to read a number from a file, and to take the abstruseness a bit further the fraction of a difference is multiplied by −1 instead of exchanging the subtrahend and the minuend.
With the constant 125000 the expression computes the current_mbps if current_total_bytes has been fetched one eighth of a second ago (since 125000 is one eighth of a million). The (re-)initialization of current_total_bytes is missing from the code.
Here's a Python snippet for the computation of e. g. the eth0 rate:
import time
earlier_total_bytes = None
while 0 <= (current_total_bytes := int(open("/sys/class/net/eth0/statistics/rx_bytes").read())) \
and earlier_total_bytes is None: earlier_total_bytes = current_total_bytes; time.sleep(.125)
current_mbps = (current_total_bytes - earlier_total_bytes) / 125000
Of course this can be adapted for other sampling intervals, also repeated and variable.

Java - Gamma algorithm [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am trying to code an algorithm to implement Gamma Correction but I am unable todo so with below code, because of a high power base. Wondering if anyone could fix below code to raise color values between 0,1 Thanks
The problem is in your math.
int / int will cause a truncation.
If red = 9 and you execute 9 / 255 the result is 0.
Try making all your literal values floating point
(for example 255.0 instead of 255).

Value assigning from text file variables [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Got a problem and I don't know how to approach this one.
From the text file I get a line like this: "x = 45 + 3". (Variables are only single lowercase/uppercase letters). Output should be 48, like normal calculation.
Now I need to calculate that equation and use the 'x' in another equation like
"y = x + 15 - 7".
So far I've come up an idea to use substring to get the calculation part, i.e '45+3'. But I can't think any good idea how to keep variable and use it in another equation since next equation is a string form a text file also.
Any ideas are welcome!
use replace all x with 45 do the technique of arithmetic you had done before for 45+3
Hope my help works.
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("JavaScript");
engine.eval("x = 45 + 3");
engine.eval("y = x + 15 - 7");
double y = (Double)engine.get("y");
System.out.println(y);

Checking data string fits a 6 digit number format from input file [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I got some data.txt file with N-string. Each string consists of 6 digits separating by space. I need to read this but before I need to check data format (I mean that each line must consist only 6 and only digits).
Do I need to use regexp?
Yes, you can do it with a regex.
One way to do this is to check if for each row you read, you have digits only, and add a constraint for the number of digits. The digits part can be done pretty easily, using a "/d" while the number of characters to be used is constrained by "{desiredNumber}".
To better understand regex in Java, use this link :D
If you still cannot solve it, this is the magic line:
if (!newLine.matches("\\d{6}")) {
return false;
}

Digital (number) length in Java [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
How to get the length of some number in Java?
Length of string is string.length(), but what is it for an integer?
I tried this:
int lengthNumber = (String.valueOf(maxNumber)).length();
But it always returns 1, for every number.
Try
Integer#toString().length();
For Example
Integer a=222;
int length=a.toString().length();
Output
3
When I ran this:
int Number = 100003;
int lengthNumber = (String.valueOf(Number)).length();
System.out.println(lengthNumber);
My output was 6, indicating that it works correctly.
Just make sure that your variables are declared properly.
This method will work if the above isn't working.
int x = 100003;
String y = "" + x;
Now you can use y.length(). Printing y gives 100003, and printing y.length() gives 6.
System.out.println(y);
System.out.println(y.length());

Categories