Using logarithms [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to to an equation to solve for the amount of time left on a loan based on a specific payment amount (above the normal amount).
The equation is:
nRemaining = ((-log(1-(interestRate / 12) * value2 / value3)) / (log (1+ (interestRate / 12))));
Now obviously this does not work, because I am unsure how to input logarithms.

Your code will work fine, provided a few conditions are met:
If you have to use log that way, place this statement above your class:
import static java.lang.Math.log;
Otherwise, use Math.log() everywhere else you see log.
Technically you don't have to import anything in java.lang, but this is known as a static import - something that should only be done on occasion, and allows you to write your statement a lot cleaner.
Make sure that all of your values are of type double. Otherwise, you'll get integer division, which can lead to NaN for some otherwise inexplicable reason.

Several built-in logarithm methods you might want to use: Math.log, Math.log10, Math.log1p.

Related

What does the Rounding Mode Unnecessary do in Java? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
If rounding is required what exactly happens? I was looking in the documentation and it says it just throws an exception, yet in my application it seems to be rounding with half up.
RoundingMode.UNNECESSARY mandates that a BigDecimal does not need to be rounded to fit the scale specified by it.
Here's a sample:
System.out.println(new BigDecimal("1.1").setScale(1, RoundingMode.UNNECESSARY));
1.1 is an exact result, and when the new BigDecimal is created as a result of setScale, it does not need to round the result to get that precise value.
It would also work if you blew out the scale:
System.out.println(new BigDecimal("1.1").setScale(1_000, RoundingMode.UNNECESSARY));
...but, it would break if you tried something like this:
System.out.println(new BigDecimal("1.12").setScale(1, RoundingMode.UNNECESSARY));
The reason for that: you have to round your BigDecimal now in order for you to represent the appropriate scale (1 number after the decimal).
You wouldn't see the behavior if you had your scale larger than the amount of digits after your decimal, but may see another rounding behavior if it was already established on that instance of your BigDecimal. You have complete control over the rounding, so use that judiciously.

parse string containing number with minus on the right [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a problem - is there any possibility to recognize in the text the negative number, that has a minus sign on the right?
E.g. I'm thinking about number like that: 1500.0- (instead of -1500.0).
Thank you in advance for any help.
Not only is it possible, it is actually easier to handle a trailing sign. You just convert the number as usual, continuing while you keep getting digits, then if it ends with a minus sign just negate it. Easier than having to remember a leading sign.
Despite the names of the methods provided in the JDK, this is not 'parsing', it is radix conversion.
You could use charAt or a regular Expression, and many more...
String s = "1500.00-";
if (s.charAt(s.length()-2) == '-'){
//minus on the right.
}
First move the minus (if any) from the end to the start:
num = num.replaceAll("(.*)(-)?$", "$2$1");
The good thing here is that if there's no minus sign at the end, or yjr minus us alteady at the start, nothing change it made.
Then parse it as normal, eg:
double d = Double.parseDouble(num);

multiply large no. in array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
multiply any 2 numbers. The numbers can be extremely large (i.e. run into hundreds of digits) and are provided as strings.
The expected output is a string which represents the product of the two numbers.
example-
multiply("268435456","524288")="140737488355328"
multiply("12321412423524534534543","0")="0"
Use BigDecimal, which has a multiply method and a constructor which takes a String. It also contains corresponding toString() and toPlainString() methods to get your result as a string.
(If the numbers are always whole numbers, then use BigInteger instead.)

How does Base32 encoding work in Java? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have this code to generate a random key.
new BigInteger(130, random).toString(32);
From what I understand, the first part of the code will create a 130 bit, random, integer. Then, the .toString(int radix) method will be called on it and the number will be converted to an alpha-numeric string.
What I can't understand is which alphabet is used for the characters; in other words, which key is mapped to which character?
Note: I looked for how a Base 32 conversion can happen, but I couldn't come up with anything useful since it doesn't seem to be a unique method to do that.
The javadoc says:
" The digit-to-character mapping provided by Character.forDigit is used, and a minus sign is prepended if appropriate.".
And the latter javadoc says:
" If the digit is less than 10, then '0' + digit is returned. Otherwise, the value 'a' + digit - 10 is returned."

How to create random cell number for testing purpose [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have to create a random cellnumber 07939393914 for automation testing purpose.
Last 079393(5 digits) digits should change randamly.. each time test runs..
Can any one suggest JAVA code for this ? or else Selenium Java code ?
Thanks
Is this a number or a String? I ask as it has a leading zero.
Take you initial number as 7939300000
then add to it Math.round(Math.Random()*10000)
If you want it as a String, take your string as "079393" and use Integer.toString on the result above, then concatentate them
Use RandomStringUtils class.
String randomNumbers = RandomStringUtils.randomNumeric(5);
String phNo = 079393+randomNumbers;
var num=Math.ceil(Math.Random()*100000)
for random first 5 digit numbers
and add
var your_last_5digits; //as string to your last 5 digits
cellNumber='0'+num+your_last_5digits;

Categories