aspose file tools*
The moose likes Beginning Java and the fly likes Complications with sine, cosine, and doubles Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Complications with sine, cosine, and doubles" Watch "Complications with sine, cosine, and doubles" New topic
Author

Complications with sine, cosine, and doubles

Noah Faust
Greenhorn

Joined: Mar 07, 2009
Posts: 26
eclipse wants me to put "float" before everything, I feel like I'm using the wrong sort of variable or something, but I know the idea works because I've done it in TI-BASIC


public class Signature {
public static void main (String[] args) { System.print.ln (//insert witty phrase here) }
}
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
Please use the Code button, otherwise your code is really difficult to read.

What's TI-BASIC? Forget whatever happens in other languages; they are all different and you can get no end of confusion if you forget that. The more similar the languages (eg C++ and Java), the worse the confusion can be.

Never write == true. It's " if (mousePressed) . . ." You can get all sorts of nasty errors if you mistakenly write = for ==.
Why have you made all your fields public? That is usually very bad practice.
Where are you supposed to write float? I can't see anywhere it would be necessary. Do your method return floats or doubles? Where do you get the acos asin, etc methods from? Is it a static import which you haven't told us about?

Noah Faust
Greenhorn

Joined: Mar 07, 2009
Posts: 26
mousePressed isn't the problem, I'm trying to use sine and cosine but I get values of NaN and even when I don't it still does not draw a line.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Noah Faust wrote:mousePressed isn't the problem, I'm trying to use sine and cosine but I get values of NaN and even when I don't it still does not draw a line.

Well, what you're actually using are asin() and acos() - arcsine and arccosine. I have no idea why. But it's not surpising you're getting NaN for theses values, since these functions are undefined for any value of "time" greater than 1. Those method calls do pretty much the opposite of what you want. If you want sine and cosine, remove the letter "a" from both method calls.

Also, again, I don't really know what you are trying to do, but I suspect that ".1" is too large an increment here. The sine and cosine functions use radians, not degrees, so a change of .1 is about six degrees. Multiply by (180.0/PI) to convert radians to degrees. Or use Math.toDegrees().
Noah Faust
Greenhorn

Joined: Mar 07, 2009
Posts: 26
I changed asin and acos to sin and cos, for some reason the website I found for using sine and cosine said to put a's before it, but I'm still getting an error in Eclipse. It wants me to cast my variables as floats, I think the problem has something to do with the range of input and output allowed for sine and cosine, but I don't know what that is. I attached a picture of what it looks like in eclipse


[Thumbnail for Capture.JPG]

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18655
    
    8

You aren't writing sin() and cos() methods in that class, are you? No? Then you'll need to specify the class they do come from. Like this:
Noah Faust
Greenhorn

Joined: Mar 07, 2009
Posts: 26
That worked! Thanks! I figured out that the line() wasn't working because it only accepted whole numbers. Here's the final code and a screenshot.

[edit]Newlines to break very long line of code[/edit]



[Thumbnail for Capture.JPG]

Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Where are sin() and cos() defined, anyway? Normally we would use the ones in java.lang.Math, but I don't see a static import of those methods, and yet you're typing sin() rather than Math.sin(). Does PApplet have some methods you're inheriting? If you control-click on the sin() method (or is it F3? I forget), Eclipse should take you to where that method is defined. I suspect that you're accidentally using some strange implementation declared as sin(float x) rather than sin(double x). The solution is to use the ones in java.lang.Math. If necessary, write Math.sin(x) rather than sin(x) (assuming you can't simply delete the offending methods from PApplet or wherever they are).

Ah, I see I should have refreshed my view before posting. Oh well.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
On a different tack: beware of long lines. Depending on one's screen resolution, part of your original post was potentially very difficult to read.
Noah Faust
Greenhorn

Joined: Mar 07, 2009
Posts: 26
Ritchie,
1. TI-BASIC is a TI 84+ calculator programming language
2. I'm not going to rearrange my code so that one line is shorter
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
Noah Faust wrote:Ritchie,
1. TI-BASIC is a TI 84+ calculator programming language
Never heard of it before.
2. I'm not going to rearrange my code so that one line is shorter
So you are happy to post code which the rest of us find awkward to read?
Noah Faust
Greenhorn

Joined: Mar 07, 2009
Posts: 26
You can choose whether or not you would like to read it. If minor inconvenience deters you from helping someone solve their programming problems, then chances are you don't have much to say anyway.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Noah Faust wrote:You can choose whether or not you would like to read it. If minor inconvenience deters you from helping someone solve their programming problems, then chances are you don't have much to say anyway.


Are you sure that this is the attitude you wish to project here? Myself I've found that
a) Campbell Ritchie has been one of the most helpful posters here, and
b) I find that if I'm the one asking for free help, I usually get more help (and more respect) if I take the effort to try to make it as easy as possible for others to help me.

Best of luck.
Noah Faust
Greenhorn

Joined: Mar 07, 2009
Posts: 26
I apologize. I will try to make my code more readable in the future.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
You may not realize it, but an overlong line of code affects the entire page, not just that one line.

See what this does:

[really long line of code, now removed, so now the rest of this post is no longer true. But imagine that it was true, at the time it was written.]

The above line is just one line, right? But look at the previous posts in this thread. Not the code, but the text parts. Look at how you have to scroll horizontally to read them. Unless they put in line breaks. A moderator will probably fix the problem I've created here, pretty soon. Because they're helpful that way. However, I would ask that they hold off for a bit, so that Noah can better understand why long lines in code tags are bad.

Thank you.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Noah Faust wrote:I apologize. I will try to make my code more readable in the future.




Cool beans. I think that if you do this, you'll greatly improve your chances of folks taking to the time to read your code and help you, so it's really most helpful for you. Again, best of luck!
Noah Faust
Greenhorn

Joined: Mar 07, 2009
Posts: 26
Just curious, why no automatic wraparound?
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Well, I don't think you want code wrap inside code tags, most of the time. The indentation would be lost, and thus the code would be much harder to read. But we do want word wrapping outside code tags. Unfortunately, browsers show the whole page with a single width, and it seems no one has found a good solution that allows these different widths to elegantly coexist. Or maybe this problem has been solved elsewhere, but not in this forum's software. This might be a better question for either the HTML forum, or the Ranch Office forum.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
Apologies accepted
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Complications with sine, cosine, and doubles