LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-13-2005, 10:43 AM   #1
k1ll3r_x
Member
 
Registered: Sep 2004
Location: Laredo, TX
Distribution: Debian 11
Posts: 164

Rep: Reputation: 30
Java Issue


i am totally lost on this

Code:
public class Class1{
public static void main(String args[]) {
double a[]={3.0,2.0,3.0};
int i;
double h;
                for (i=0;i<3;i++){
                    if (i %2 ==0 ){
                   h=Math.ceil(Math.exp(Math.log(a[i])*2));//this is where im lost
                                           }
                else{
                   h=Math.ceil(Math.exp(Math.log(a[i])*2));
                                            }
 
System.out.print(h);
System.out.print(" ");
} }
}
I dont know why, but i work it out on paper and i get 6.0 4.0 6.0 and i really dont know why, as far as i know i%2 is equal to 0 because i is initiated to 0.
if anyone can help me out on this, i think the math.ceil has to do something on it too, but i just cant find out what, id really appreciate
thanx

edit* [i forgot about the if statement, plz excuse the issue

Last edited by k1ll3r_x; 09-13-2005 at 03:19 PM.
 
Old 09-13-2005, 10:51 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Re: Java Issue

Your calculation isn't based on the value of i, it's based on the value of a[i]. So going through the loop,

i = 0, a[i] = 3.0
i = 1, a[i] = 2.0
i = 2, a[i] = 3.0,

as declared in your array.

Last edited by Nylex; 09-13-2005 at 10:53 AM.
 
Old 09-13-2005, 10:53 AM   #3
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
I see no "i%2" in your code.
 
Old 09-13-2005, 12:39 PM   #4
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Also, does that code compile? You have an else statement with no if..
 
Old 09-13-2005, 03:20 PM   #5
k1ll3r_x
Member
 
Registered: Sep 2004
Location: Laredo, TX
Distribution: Debian 11
Posts: 164

Original Poster
Rep: Reputation: 30
yea sorry i edited it now, i did miss the if statement
 
Old 09-13-2005, 07:34 PM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
What is this "if" statement for, as the instructions are the same in both cases ?
 
Old 09-14-2005, 10:25 AM   #7
k1ll3r_x
Member
 
Registered: Sep 2004
Location: Laredo, TX
Distribution: Debian 11
Posts: 164

Original Poster
Rep: Reputation: 30
some test stuff that i got, but we go through every single problem, and this is one that i just get, this is not the first problem i post, its been a few. But what i want to know is how it works and why i get the answer i get, and where it is that im lost
 
Old 09-14-2005, 11:59 AM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Can you clarify what piece of this code you do not understand and what is the problem you are trying to fix, if any.
 
Old 09-14-2005, 03:24 PM   #9
k1ll3r_x
Member
 
Registered: Sep 2004
Location: Laredo, TX
Distribution: Debian 11
Posts: 164

Original Poster
Rep: Reputation: 30
well i just dont understand how the first number comming out is a 9.0, not a 6.0
 
Old 09-14-2005, 03:37 PM   #10
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Go through your code :/.

The first time through the loop, i = 0. So, a[i] = 3. Then you take the natural log (ln) of a[i] and multiply by 2:

(Math.log(a[i])*2)

After that, you're finding e^x, where x = (Math.log(a[i])*2):

Math.exp((Math.log(a[i])*2)).

Now e^ab = (e^a)^b, so that is the same as if you had written

(Math.exp(Math.log(a[i]))^2

and since e^x and ln x are inverse functions, Math.exp(Math.log(a[i])) is just the same as writing

a[i] (inverse functions "cancel each other out", so to speak).

Since i = 0, you know a[i] = 3, so therefore

h = Math.ceil((a[i]^2);

and that is just 9.0.

Last edited by Nylex; 09-14-2005 at 03:40 PM.
 
Old 09-14-2005, 03:41 PM   #11
k1ll3r_x
Member
 
Registered: Sep 2004
Location: Laredo, TX
Distribution: Debian 11
Posts: 164

Original Poster
Rep: Reputation: 30
ok you just got me even more lost, all i want to know is why im getting a 9.0 as the first number when i compile, i am getting a 6.0, and i am not suppossed to, i compile and run, and my results are [9.0 ,4.0, 9.0] and if i do it on paper, i get 6.0 4.0 and 6.0, why is it that i get that on paper, and why is it that the correct answer it 9.0, 4.0, and 9.0?
 
Old 09-14-2005, 04:07 PM   #12
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
How do you get 6 when you "do it on paper"? As Nylex pointed out, the math is:
e ^ ( log(3) * 2 ) = ( e ^ log(3) ) ^ 2 = 3 ^ 2 = 9

Do it on a calculator if you don't believe us.

Last edited by spooon; 09-14-2005 at 04:15 PM.
 
Old 09-15-2005, 10:30 AM   #13
k1ll3r_x
Member
 
Registered: Sep 2004
Location: Laredo, TX
Distribution: Debian 11
Posts: 164

Original Poster
Rep: Reputation: 30
yea i got it, my bad, it was just i hadnt really noticed the Math.log, although i didnt know, but know i get, so thanx for realz, i really appreciate the help.
-Jerry V.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Java printout issue k1ll3r_x Programming 4 10-19-2005 10:28 PM
yet another LimeWire / Java issue! tykkea811 Linux - Software 1 11-30-2004 09:03 PM
Java font issue rmartine Programming 0 01-27-2004 05:38 AM
Is there an issue with Mozilla1.5 and Java? oicdn Linux - Software 8 01-25-2004 12:37 AM
Java maybe mozilla issue f1uke Linux - Newbie 5 02-11-2003 09:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration