• Post Reply Bookmark Topic Watch Topic
  • New Topic

Logarithm in java Without Math.log()  RSS feed

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ranchers,
As an assignment I have to create a method that approximates the logarithm of base two for any number.
We were given the following formula to use:


I got the following code:



Yet this does not work.

Any hints?
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you think this does not work? ItDoesntWorkIsUseless (<---click that link). You need to TellTheDetails (<---another link you should read).
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh yes, I was somewhat unclear.
Well this method does give me results.
Yet I was told that we have to multiply the terms with each other and draw the conclusion out of that.
The failure is in the Junits. Expected and actual values differ.
 
Ranch Hand
Posts: 924
9
IBM DB2 Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Von Valkenburgh wrote:Oh yes, I was somewhat unclear.
The failure is in the Junits. Expected and actual values differ.


For example?
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For Example:

java.lang.AssertionError: When entering 0,001
: A Number is wrong! Output: Approximation: -3.9840399201357926
Library: -6.907755278982137
Terms: 996010
in the beginning expected:<-6.907320519849492> but was:<-3.9840399201357926>
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.failNotEquals(Assert.java:743)
at org.junit.Assert.assertEquals(Assert.java:494)
at de.ostfalia.gdp.ss15.test.StdioHelper.assertEquals(StdioHelper.java:115)
at de.ostfalia.gdp.ss15.test.StdioHelper.checkNumbers(StdioHelper.java:92)
at de.ostfalia.gdp.ss15.test.LogarithmusTest.logarithmusTest0_001(LogarithmusTest.java:46)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.FailOnTimeout$StatementThread.run(FailOnTimeout.java:74)


java.lang.AssertionError: When entering 13,5
:A number is wrong! Output: Approximation: 3.0054532781171837
Library: 2.6026896854443837
Terms: 751363
in the Beginning expected:<2.602682523751> but was:<3.0054532781171837>
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.failNotEquals(Assert.java:743)
at org.junit.Assert.assertEquals(Assert.java:494)
at de.ostfalia.gdp.ss15.test.StdioHelper.assertEquals(StdioHelper.java:115)
at de.ostfalia.gdp.ss15.test.StdioHelper.checkNumbers(StdioHelper.java:92)
at de.ostfalia.gdp.ss15.test.LogarithmusTest.logarithmusTest13_5(LogarithmusTest.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.FailOnTimeout$StatementThread.run(FailOnTimeout.java:74)



 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're missing one operation on each term. Look at the formula carefully.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, your variables one and two don't make your program any clearer. In fact, I would argue that they mislead. Variables are expected to change their values, either from one run of the program to the next or during a run, yet these variables never change. They are, in fact, constants. You could define them as named constants but if you are thinking that using the literals 1.0 and 2 in your program is poor form, I would say that in this case, it's not. The formula clearly uses a fraction 1/n, and the power is clearly increasing consistently by 2.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't why you have to go through the gyrations of entering a comma instead of a period and then massaging your input to get a double value. Seems like a very roundabout way to do it. I just used Scanner.nextDouble() to get the number.

Using an epsilon of 1E-6, these are my results:
 
Bartender
Posts: 1840
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two things I have spotted:

What is the starting value of temp?
For your first iteration of the loop, what value are you adding? I am unable to tell because there is a 'zwischen' variable in the mix

From your loop I take the following pseudocode:

As mentioned your loop is missing a crucial operation.

Each time through the loop you add a term to your calculation to date: multi * termCalc
I can see you update multi in the loop
Should you also be updating termCalc?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!