I am trying to find the result of log(10^k) , where k is big number like 10000. For example :
BigDecimal first = BigDecimal.TEN.pow(10000);
double result = Math.log(first.doubleValue());
However "result" becomes Infinity , however on wolphram approximates it to 23025.85.Any suggestion how to find the result? As a result the number with the first two digits after the decimal point are enough for me.
Use the fact that
log(10^k) = k*log(10)
So:
System.out.println(10000 * Math.log(10));
Prints:
23025.850929940458
The problem you are likely having, is that Wolphram is able to either hold the powered value or it is doing the log operation first.
When running this like your example, you will have an extremely large number that goes past the maximum value for a BigDecimal, which should result in an error or an "infinity", because it overflows the capability of the data type, I would suggest doing the operation the other way arround, perhaphs process the log first on a base 1 value for example and only then multiply it by whatever powered number you are tying to use.
See, there is a simple property of logarithms that you can use:
log(x^y) = y*log(x)
So what you can do is:
double y = y*log(x);
System.out.println(Math.round(y));
Hope this helps!
Related
Im working on a homework assignment for my intro to computer science class and we are are inputting basic commands to get the percentage of people who drink a certain kind of energy drink. We used JOptionPane to make text boxes and you can input the amount of people and the computer has a set percentage to get the output. My problem is i set up my variables as doubles and my answers are very long decimals. I want to convert the answers to Ints so i can get whole numbers. I have tried to do this through casting but i keep getting the error message" EnergyDrink.java:14: error: variable citrusEnergyDrinkers might not have been initialized". What can i do?
This can't be solved without code. The error is not due to any problem with the conversion, but simply as the compiler-error says:
variable citrusEnergyDrinkers might not have been initialized
This means that the variable might not hold a value at the time you attempt to convert it, which results in undefined behaviour, which java-designers didn't allow for a reason.
The problem is as the error-message tells: citrusEnergyDrinkers gets its value inside some try-catch-block or a block that is only run under certain conditions, like if. One way to work around this would be to simply initialize citrusEnergyDrinkers as 0:
double citrusEnergyDrinkers = 0;.
Note though that this might produce incorrect results depending upon what happens when the value isn't set in case the above mentioned block of code isn't entered/breaks off before setting a value.
For the conversion:
Math.round(citrusEnergyDrinkers) is most likely preferable to a simple cast to int, since double most of the time has some imprecision due to the way it's stored in memory and round will actually round the value, while a cast will simply remove the frictional part. For example:
(int) 0.75 //produces 0
Math.round(0.75) //produces 1
You could multiply the double by 100 and then cast to an int:
double d = .77583495;
int perc = (int) Math.round( d );
I prefer to not cast like that, but it works.
Good luck.
I have a big number in a database; in this case, 10,000,000,000. Whenever I use that information for something, like sending a message with it, instead of 10,000,000,000, it says 1E10, and I really do not want that.
Can I avoid that in any way?
If I go to the database, the value is 10,000,000,000.
It's the same number, just represented in scientific notation.
Since you don't describe how you are storing the value, you can use DecimalFormat#getNumberInstance to help format it to one that doesn't contain the scientific notation.
double foo = 10000000000L;
System.out.println(foo);
System.out.println(DecimalFormat.getIntegerInstance().format(foo));
This outputs:
1.0E10
10,000,000,000
I want to find a remainder of very long numbers .I am writing a program for this and as I cannot find the remainder directly due to the fact that they are large numbers (in c) .How can I do this?the limit for the number from which I have to divide the bigger number to find remainder is 500.i.e 1 to 500
I thought of dividing the number like this:
1234567=1*10^6+2*10^5+...
1234567%x=1modx*10^6modx+2modx*10^5modx...
I need a better way than this.
Hint:
Use a linked list. Store the number as a group of numbers dynamically.
For eg:
112233445566778899001122 => 11223344 55667788 99001122
Now consider the individual unit and start from left to right. Find the reminder and manipulate it to add to the next group and go on.
Now implementation is very easy :)
Edit:
112233445566778899001122/6 => 11223344 55667788 99001122/6
11223344/6 =>2
2*100000000 + 55667788 = 255667788
255667788/6 => 0
0*100000000 + 99001122 = 99001122
99001122/6=>0
So the reminder is 0.
Remember, the individual unit after manipulation should be under the maximum range int can support.
If your question regards using very long or large numbers try using something a long long. The problem could be that the data type that you are using is too small to hold the values that you require.
You could try using a bignum library like GMP or another kind of ugly way in comparison would be to use arrays or lists, somewhat similar to this.
Other than that, the modulo operation % will calculate the remainder for you.
I'm trying to write a Java program that can take values and put them into a formula involving log 1/3.
How can I calculate log 1/3 in Java?
When you want to calculate the logarithm you need to know the base. Once you know the base you can perform the calculation:
log_b(x) = ln(x) / ln(b)
http://en.wikipedia.org/wiki/Logarithm#Change_of_base
In Java the Math#log(double) function calculates the natural logarithm. So you can use this to calculate the logarithm to a given base b:
double result = Math.log(x) / Math.log(b);
You can use Math.log(value) to get log of specific value where value is considered to be in double.You can also use Math.log10(value) to get base 10 log.
So you can just use
Math.log(1/3.0)
1/3 yields the result as integer type value. So, the result will be 0, not 0.3333.
Therefore, Math.log(1/3) = Math.log(0), which results in infinity.
So, you need to write Math.log(1/3.0) to get the desired result.
You can calculate the natural logarithm using the Java.lang.Math.log() method:
System.out.println("Math.log(1/3.0)=" + Math.log(1/3.0));
See http://www.tutorialspoint.com/java/lang/math_log.htm and http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#log%28double%29
In order to get the log_10 you can do it as follows:
System.out.println("log_10(1/3.0)=" + (Math.log(1/3.0)/Math.log(10)));
You don't need a new implementation there is inbuilt function for this
Read about Math.log()
But in this case log(1/3) will give you value as infinity,If you use Math.log(1/3).
You can use log rule as follows.
log(1/3) =log(1)-log(3)
Now
Math.log(1/3)=Math.log(1)-Math.log(3)
Eg:
System.out.println(Math.log(1)-Math.log(3));
Out put:
-1.0986122886681098
This is basically what I am trying to do
// ... some code, calculations, what have you ...
long timeToAdd = returnTimeToAddInLongFormat();
// lets output the long type now, and yes i need the width and precision.
System.out.printf("Time to add: %13.10ld", timeToAdd);
I've read most of the google searches around the topic and think I understand how to do it conceptually, but the JRE keeps throwing me a UnknownFormatConversionException and telling me my input size modifier l doesnt work.
Is there another way to do this, or did I miss something small?
Java treats all integer values as d, there is no ld. Even byte and BigInteger is a d type. It also assumes integers have no decimal places. If you want to show 10 zeros, you can convert to double first and use f